Re: Display highlighted data from Listbox in Textboxes
I am new to C# as a language. I am more familiar with VB, but by no means an expert. I am working on this as we speak, so I will keep you all updated. I wish this book explained how to do some of this stuff a little better.
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by CarlMartin
I am new to C# as a language. I am more familiar with VB, but by no means an expert. I am working on this as we speak, so I will keep you all updated. I wish this book explained how to do some of this stuff a little better.
I've never come across a bad C# book. You will probably have to be patient or try a different book. A lot of books tend to be the same but some teach things in different orders. Whilst it is good you want to do things the best way I think it is better to take things step by step. Try not to put the cart before the horse. Get something working and understand what's going on. Then you can work on optimising and understanding why it is an optimal solution. In order to do that you've got be able master the basic elements of the C# language and object oriented programming. I don't think VB fully supported object oriented programming to its full extent...I might be wrong on this point. I've not done a lot of VB...
Re: Display highlighted data from Listbox in Textboxes
OK, I started all over. Here is what I have so far:
Code:
public partial class frmAddressBook : Form
{
public frmAddressBook()
{
InitializeComponent();
}
private void frmAddressBook_Load(object sender, EventArgs e)
{
// Create Array of Structures to hold contact info
}
public struct Contacts
{
public string FirstName;
public string LastName;
public string Street;
public string City;
public string State;
public string Zip;
} Contacts [] entry = 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;
// 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 = "";
}
}
}
I see how the contacts are loaded into the structure, but I do not understand how these new entries are supposed to go into the array. What I have so far makes sense.
Re: Display highlighted data from Listbox in Textboxes
Is there a purpose to storing them in an array? Anyway it is straightforward towards the end of the btnAdd_Click you would do something like this:
Code:
<name of array>[0] = entry;
I put here <name of array> as you appear to have a conflict between the name of the field that is used for the array and the local variable within the method. I think you need be aware of how you name your variables. If you can't come up with good names for the variables think again about what you're trying to achieve. In this the easiest thing to is call a spade a spade I.e. You could name the array something like 'arrayOfContacts' and you could name the local variable 'contact'. By the way did the code compile?
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?
Last edited by CarlMartin; January 25th, 2010 at 08:54 PM.
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
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());
}
Last edited by CarlMartin; January 25th, 2010 at 09:38 PM.
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.
Last edited by CarlMartin; January 25th, 2010 at 11:12 PM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.