Re: Display highlighted data from Listbox in Textboxes
thanks. Yes, the code has no errors, it compiles, and it works pretty much. The reason I am storing the structures into an array is because that's what the directions ask for. Basically, I want each contact to be a structure that is then stored into an array with a maximum number of contacts of 20. Here is the exact problem in the book:
Write a Windows Application that maintains an address book. This address book should hold up to 20 entries. You must store your data with arrays. Each address entry will have the following:
•Last Name, First Name
•Street Address
•City
•State
•Zip Code
Your program must have the following functionality:
•Display the address book (names only in alphabetical order by last name)
•Display all information about an individual entry (allow the user to select this)
•Add address entry
•Delete address entry
Here is the updated code:
Code:
public struct Contacts
{
public string FirstName;
public string LastName;
public string Street;
public string City;
public string State;
public string Zip;
} Contacts [] arrayOfContacts = new Contacts [20];
private void btnExit_Click(object sender, EventArgs e)
{
// Close program
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Declare variables
Contacts entry;
entry.FirstName = tbxFirstName.Text;
entry.LastName = tbxLastName.Text;
entry.Street = tbxStreet.Text;
entry.City = tbxCity.Text;
entry.State = tbxState.Text;
entry.Zip = tbxZip.Text;
// Add contact to array
arrayOfContacts[0] = entry;
// Display Contacts (First and Last name only) in Listbox
lstContacts.Items.Add(entry.LastName + ", " + entry.FirstName);
// Clear Text Boxes
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
}
}
}
So, now this structure created by clicking the "Add" button is added to the array as element "0", correct?
Re: Display highlighted data from Listbox in Textboxes
That's correct. You're making good progress. What I recommend now is that you define an integer field to act as counter/index for your array. This will ensure that you don't ovewrite existing entries and that you don't exceed the bounds of the array. So define the counter/index and initialise it to '0'. Replact the arrayOfContacts[0] with arrayOfContents[index] and then increment the index. You can actually do it all in one step like this:
Code:
arrayOfContacts[index++] = entry;
Note that 'index' has to be a field (i.e. a class wide variable) as opposed to a local variable otherwise it wont work.
You can also put a check in the end to reset the index if it reaches 20. I'll let you work out how to do that for yourself. There are a couple ways of doing it. Go for the simplest way you can think of.
Re: Display highlighted data from Listbox in Textboxes
ok, thanks, be back when I get some progress happening.
Re: Display highlighted data from Listbox in Textboxes
How about this:
Code:
public struct Contacts
{
public string FirstName;
public string LastName;
public string Street;
public string City;
public string State;
public string Zip;
} Contacts [] arrayOfContacts = new Contacts [20];
int index = 0;
private void btnExit_Click(object sender, EventArgs e)
{
// Close program
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Declare variables
Contacts entry;
entry.FirstName = tbxFirstName.Text;
entry.LastName = tbxLastName.Text;
entry.Street = tbxStreet.Text;
entry.City = tbxCity.Text;
entry.State = tbxState.Text;
entry.Zip = tbxZip.Text;
// Add contact to array
if (index < 20)
{
arrayOfContacts[index++] = entry;
}
else
{
MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
}
// Display Contacts (First and Last name only) in Listbox
lstContacts.Items.Add(entry.LastName + ", " + entry.FirstName);
// Return message when Array reaches maximum of 20 entries
// Clear Text Boxes
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
}
}
}
Ok, so that seems to work fine. I was trying to code the "Delete" button which will delete the entry in the array that corresponds to the name selected in the listbox. I tried to do this with a for loop seen below, but I am getting a lot of errors. Should I be doing this a different way? This works fine in another program I have that uses an array rather than an array of structures.
Code:
private void btnDelete_Click(object sender, EventArgs e)
{
for (int k = 0; k < arrayOfContacts.Length; k++)
try
{
if (arrayOfContacts[k] = lstContacts.SelectedItem.ToString())
{
arrayOfContacts[k] = null;
}
}
catch
{
}
}
//Display Refreshed Records in ListBox
lstContacts.Items.Clear();
for (int j = 0; j < index; j++)
{
if (arrayOfContacts[j] != null)
{
lstContacts.Items.Add(arrayOfContacts[j].ToString().Trim());
}
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
How about this:
How about zipping the project up and attaching it?
That way we can load it up, compile it and the compiler can show me your errors.
Then we can post back and tell you how to identify the specific error and what to do about it.
1 Attachment(s)
Re: Display highlighted data from Listbox in Textboxes
here you go. File attached.
Re: Display highlighted data from Listbox in Textboxes
The first thing to do is get rid of the compile errors and then add functionality back in.
Comment out the following lines of code:
1)
Code:
if (arrayOfContacts[k] = lstContacts.SelectedItem.ToString())
{
arrayOfContacts[k] = null;
}
2)
Code:
lstContacts.Items.Clear();
3)
Code:
for (int j = 0; j < index; j++)
{
if (arrayOfContacts[j] != null)
{
lstContacts.Items.Add(arrayOfContacts[j].ToString().Trim());
}
NOTE: Comment out exactly what I have shown. Don't comment out anything more (even an extra curly brace).
If you comment out what I've coded then your app should compile.
From there we can look at the other errors.
Re: Display highlighted data from Listbox in Textboxes
Did that, and it does compile. I knew where the errors were, I am just not proficient in C# to do what I need to do. That was my attempt at whet I thought would be the proper code, but I was incorrect. There are no other errors with that all commented out.
I hope I can get this crazy program finished by tomorrow night because I have been working on it for awhile and I have to go out of town for a week on Wednesday, lol.