Hello everyone,
I am working on a project that when a user select an item in the listbox data will display in the textboxes. I saw a thread named "Experiment with ListBox in C#", this thread does cover what I am trying to implement on my project. I copied and pasted that code into Visual C# 2008 and it does not fire. That is the problem I have been having ever since the school project. The school project was submitted for grading already done deal. LOL
School project:
This is an address book with arrays. single dimension.
firstname, lastname, address, city, state, zip
Six textboxes, listbox, add btn, deletebtn, exitbtn.
The user is to enter data into textboxes, display only names to listbox sorted in alphabetical order
The user select a name in listbox, display address information to textbox.
I am having the problem that when I select a name to display addresses to the textbox it does nothing to display.
//this suppose to fire when user select an item in listbox to textbox
//I think this is where i am having the problem
private void lstNames_SelectedIndexChanged(object sender, EventArgs e)
{
// Displays address into the text boxes.
// Sets the array count variable makes sure index is not -1.
if (lstNames.SelectedIndex > 0)
{
string arrayName;
arrayName = lstNames.SelectedItem.ToString();
// Loop through the arrayString to display the address of each person.
for (int index = 0; index > 20; index++)
{
if (lstNames.ToString() == arrayName)
{
// if names match then set the text box text to the array location elements.
txtlastname.Text = arrayString[0];
txtfirstname.Text = arrayString[1];
txtAddress.Text = arrayString[2];
txtCity.Text = arrayString[3];
txtState.Text = arrayString[4];
txtZip.Text = arrayString[5];
}
else
{
//did not find the data show the error messagebox
MessageBox.Show("Address not found!", "Data Error Entry",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
//this is my class-level
public partial class Form1 : Form
{
public int Counter = 0;
//declaring array with 20 elements
public string[] arrayString = new string[20];
public Form1()
{
InitializeComponent();
}
//my add button with stored array store up to 20 entries
//I can add entries without a problem
//also clears textbox for next entry and sort alphabetical order
//no problem with this
private void btnAdd_Click(object sender, EventArgs e)
{
//execute the counter as incrementor
Counter++;
//declare variable as integer
int arrayCounter;
//compare the counter less than 20
if (Counter <= 20)
{
//then arrayCounter will either add or equal to 1
arrayCounter = Counter += 1;
//initializing and store array to display to textbox
arrayString[0] = txtlastname.Text;
arrayString[1] = txtfirstname.Text;
arrayString[2] = txtAddress.Text;
arrayString[3] = txtCity.Text;
arrayString[4] = txtState.Text;
arrayString[5] = txtZip.Text;
//add a list of names by lastname with separator as format
lstNames.Items.Add(arrayString[0] + ", " + arrayString[1]);
//alphabetical order
lstNames.Sorted = true;
//string empty and brings back the focus to lastname textbox
//making room for more information
txtfirstname.Text = String.Empty;
txtlastname.Text = String.Empty;
txtAddress.Text = String.Empty;
txtCity.Text = String.Empty;
txtState.Text = String.Empty;
txtZip.Text = String.Empty;
txtfirstname.Focus();
}
else
{
//otherwise an error messagebox will appear
//alerting the user that only 20 entries are permitted
MessageBox.Show("You've reached 20 Addresses...permitted", "Error");
}
}
//this delete entry from the listbox when user select an item
//no problem is does delete an entry in listbox
private void btnDelete_Click(object sender, EventArgs e)
{
//decision to delete a record from listbox
if (lstNames.SelectedIndex > -1)
{
lstNames.Items.RemoveAt(lstNames.SelectedIndex);
}
}
I have been working on this project for over a week.
This is the introduction to C# class, not an advanced class.
But I am also curious, if the I enter data in the textboxes and click on add the names will only show in the listbox, but then since this is an array it suppose to be stored in the array memory, so when I select a name in the listbox the addresses suppose to display in the textboxes?
I greatly appreciate for your assistance, I really need to learn this.
Thanks!
Regards,
Desi Bravo