-
Display highlighted data from Listbox in Textboxes
I have an address book program completely done, except for one thing...
When a name is highlighted in the listbox, I need all of their information displayed in the related textboxes. All the contact information is stored in an array. I was playing with the code, and I know how to display just the information in the listbox, which is the first and last name, but I need ALL of the contact info displayed in the textboxes. I have included my code, and a screenshot of what should happen when a name is selected from the listbox.
http://carlmartin10.webs.com/pics/Screen.JPG
Code:
public partial class frmAddressBook : Form
{
// Create Array to hold contact information
string[] Contacts = new string[20];
int i = 0;
public frmAddressBook()
{
InitializeComponent();
}
private void AddressBook_Load(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close program
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Declare variables
string lastName = tbxLastName.Text;
string firstName = tbxFirstName.Text;
string street = tbxStreet.Text;
string city = tbxCity.Text;
string state = tbxState.Text;
string zip = tbxZip.Text;
// Add contact information for New contact to array
if (i < 20)
{
Contacts[i] = lastName + ", " + firstName;
i++;
}
else
{
MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
}
//Clear TextBoxes
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
// Display list of contact names from array in the listbox
lstContacts.Items.Clear();
for (int j = 0; j < 20; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
// Delete selected contact
for (int k = 0; k < Contacts.Length; k++)
{
try
{
if (Contacts[k].Contains(lstContacts.SelectedItem.ToString()))
{
Contacts[k] = null;
}
}
catch
{
}
}
//Display Refreshed Records in ListBox
lstContacts.Items.Clear();
for (int j = 0; j < 10; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
tbxFirstName.Text = lstContacts.SelectedItem.ToString();
}
}
}
-
Re: Display highlighted data from Listbox in Textboxes
Instead of using raw arrays to store information you should create a class to mirror a "contact". This was you simply store "Contact" objects, override "ToString()" so that they display properly in the list box, and when you retrieve them you have all of the information you need to populate your UI. I would also consider making those textboxes into a single UserControl, but that is not completely necessary as this looks like a school project. An example:
Code:
class Contact
{
// these may have different accessibility modifiers if needed
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public Contact( string name, string phoneNumber, string address )
{
this.Name = name;
this.PhoneNumber = phoneNumber;
this.Address = address;
}
}
// snip
Contact c = new Contact( "Ed", "123-4567", "123 Fake Street" );
myListView.Items.Add( c );
This is obviously a very simple example, so change things as you see fit.
-
Re: Display highlighted data from Listbox in Textboxes
I agree that that would be better, unfortunately, I am in the process of teaching myself C#, and I am using a book. The book calls for me to use only an array. I want to complete the book so I can get up to speed in C#. Thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
I agree that that would be better, unfortunately, I am in the process of teaching myself C#, and I am using a book. The book calls for me to use only an array. I want to complete the book so I can get up to speed in C#. Thanks.
Yeah, I never liked that crap. Either learn it the right way or why bother? I never saw "classes" as an advanced subject, and most "Learn X Language!" books are garbage.
You are not going to store all of this information into an array and have easy access to it. I actually do not see where you actually assign any info to the array aside from the first and last name. If you want to keep the other info you will need to store it somewhere before you blank out the textboxes.
-
Re: Display highlighted data from Listbox in Textboxes
I realize that I am only assigning those 2 elements to the array, I know how to do that part, but I got distracted trying to figure out how to do what I posted about: displaying the info from the array, for the highlighted item, into the appropriate textboxes. I am totally lost on that part.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
I realize that I am only assigning those 2 elements to the array, I know how to do that part, but I got distracted trying to figure out how to do what I posted about: displaying the info from the array, for the highlighted item, into the appropriate textboxes. I am totally lost on that part.
You are only saving the name, so that is all you will be able to access. Like I said before, this is a really ugly way to do this. You can always just grab the text using the ListBox's "SelectedItem" property.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
BigEd781
You are only saving the name, so that is all you will be able to access. Like I said before, this is a really ugly way to do this. You can always just grab the text using the ListBox's "SelectedItem" property.
Yeah, I will try that. The only problem I can see is that I can only list the first and last name in the actual listbox, but all of the data is stored into the array. Then if I grab what is in the listbox, it is only the first and last name, but I need all of the information, then I need to be able to put each piece of info into the appropriate textbox.
Thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
Yeah, I will try that. The only problem I can see is that I can only list the first and last name in the actual listbox, but all of the data is stored into the array.
No. it's not; you are only storing the name in the array.
Here is the problem; a beginner's book is proposing a problem for you that should be solved by creating your own data type but is instructing you to simply "use an array". Well, that is going to9 be a cludge. To be honest, it is very seldom that you even use an array in C#, you will be using collection classes instead. So, you can keep trying this way and simple create some hackish scheme to keep your array data in some type of order, or you can learn this the right way and start studying object oriented programming principles and how they apply to C#.
Hell, even if you were writing strict C you would create a struct to hold the data, no one would ever do it this way. Why learn how to do something the wrong way? I don't get it.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
BigEd781
No. it's not; you are only storing the name in the array.
Here is the problem; a beginner's book is proposing a problem for you that should be solved by creating your own data type but is instructing you to simply "use an array". Well, that is going to9 be a cludge. To be honest, it is very seldom that you even use an array in C#, you will be using collection classes instead. So, you can keep trying this way and simple create some hackish scheme to keep your array data in some type of order, or you can learn this the right way and start studying object oriented programming principles and how they apply to C#.
Hell, even if you were writing strict C you would create a struct to hold the data, no one would ever do it this way. Why learn how to do something the wrong way? I don't get it.
I agree that this is not the best, or even practical way to do this. If I had a choice, I would never choose this method, it is pretty ridiculous, in my opinion. But, I want to do the problems as they are designed. The object is to learn the concepts of the lesson, and that is what this problem aims to do. I really think this is overkill, but I will not throw in the towel just because there is an easier way. That way will not illustrate the lesson like this problem is designed to do. What is the point of trying to learn and do the problems int he book, if I am just gonna disregard the instructions and do it my own way.
-
Re: Display highlighted data from Listbox in Textboxes
Here is the exact problem:
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
-
Re: Display highlighted data from Listbox in Textboxes
Ok, if you are dead set on doing it this way, you need an array of arrays. each element of the array is another array which holds all of the info.
Code:
string[][] contacts = new string[20][];
for ( int i = 0; i < contacts.Length; ++i )
{
contacts[ i ] = new string[ ] { "John Doe", "123 Fake Street", "Vista", "CA", "92084" };
}
Now you have an array of arrays, and it is just terrible. So, each entry in the array is another array which has all of the information regarding a "contact".
-
Re: Display highlighted data from Listbox in Textboxes
Wow, that is going to make it complicated, lol. Thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
Wow, that is going to make it complicated, lol. Thanks.
Yes, which is why C# provides mechanisms for encapsulating data.
On a side note, I see no where in your requirements a statement that says you cannot use classes and structures. It says that you must use arrays to "store your data". Your data should take the form of a class.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
I agree that this is not the best, or even practical way to do this. If I had a choice, I would never choose this method, it is pretty ridiculous, in my opinion. But, I want to do the problems as they are designed.
Why? Are you getting graded? Is this a homework assignment?
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
Arjay
Why? Are you getting graded? Is this a homework assignment?
If you read my post, I am trying to learn C# on my own, so I bought a book, and I am trying to do the problems in the book as I go through it. I need to learn C#, so I thought I would do it this way.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
If you read my post, I am trying to learn C# on my own, so I bought a book, and I am trying to do the problems in the book as I go through it. I need to learn C#, so I thought I would do it this way.
I did read your post. Just checking to make sure it isn't homework. :)
Since it isn't homework, I'd like to suggest that you try to do it using BigEd's approach. (or try it BigEd's way and then the array way or vice versa).
BigEd is really correct that you wouldn't want to track items in a list using an array. He's giving a little insight about how it would really be done.
-
Re: Display highlighted data from Listbox in Textboxes
I understand that. I did do it his way after reading the post, but I will not be able to let it go without doing it the original way, just to be able to do it. I need to prove to myself that it can be done, lol. Thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
I understand that. I did do it his way after reading the post, but I will not be able to let it go without doing it the original way, just to be able to do it. I need to prove to myself that it can be done, lol. Thanks.
Great. That's the attitude. Best of luck. :thumb:
-
Re: Display highlighted data from Listbox in Textboxes
Check to see if the next chapter of the book asks...
Now change your Address Book program, so that it uses Objects rather than an Array ;)
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
BigEd781
Instead of using raw arrays to store information you should create a class to mirror a "contact". This was you simply store "Contact" objects, override "ToString()" so that they display properly in the list box, and when you retrieve them you have all of the information you need to populate your UI. I would also consider making those textboxes into a single UserControl, but that is not completely necessary as this looks like a school project. An example:
Code:
class Contact
{
// these may have different accessibility modifiers if needed
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public Contact( string name, string phoneNumber, string address )
{
this.Name = name;
this.PhoneNumber = phoneNumber;
this.Address = address;
}
}
// snip
Contact c = new Contact( "Ed", "123-4567", "123 Fake Street" );
myListView.Items.Add( c );
This is obviously a very simple example, so change things as you see fit.
I think that, if I can use classes, but use an array to store the data (per the requirement), then thic will still technically be correct. It does say simply that an array must be used to "store" the date and that is it. Time to figure this all out. Thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
I think that, if I can use classes, but use an array to store the data (per the requirement), then thic will still technically be correct. It does say simply that an array must be used to "store" the date and that is it. Time to figure this all out. Thanks.
Yup, that is exactly what I suggested a few responses ago. ;)
I don't mean to belabor the point, but there is no sense in learning how to do something the wrong way first. If you need to learn how arrays are used then do something that is suited to that task, not something like this.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
BigEd781
Yup, that is exactly what I suggested a few responses ago. ;)
I don't mean to belabor the point, but there is no sense in learning how to do something the wrong way first. If you need to learn how arrays are used then do something that is suited to that task, not something like this.
I agree with you there, I just hate to not follow directions. It's just me. Anyway, let me ask you a question. Do you think the better way to do this, and still use an array to store the data, is to use a class as you suggested, or to use an array of structures? I can see both ways working, but I am not sure which is the preferred and easiest method...Remember, I am still trying to learn all this :). Thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Structures are rarely used in C#. They have value-type semantics and are generally used to express very simple data stores. Beyond that you should always be using classes.
Also, I dropped out of college because they had me doing nonsense work which didn't even make sense and was flat out wrong. When I did things the proper way teachers would literally take points away because I didn't "follow the directions exactly to the letter" instead of rewarding me for going above and beyond what they were looking for. So yeah, I don't put much stock into "directions" which make no sense.
-
Re: Display highlighted data from Listbox in Textboxes
Thanks for the advice. I was trying to avoid going to class by teaching myself, as you can see, for some of the same reasons. I like how the directions can still be followed, but it can still be done a better way with this solution. Thanks. I will work on this, and if I can't figure something out, I will be back.
-
Re: Display highlighted data from Listbox in Textboxes
Put it this way; if you're assignment is actually preventing you from learning how this stuff works in the real world, something is wrong ;)
-
Re: Display highlighted data from Listbox in Textboxes
Yeah, there have been many lessons in this book that are designed to illustrate the material, but are way more complicated to use than other methods...many lessons! I guess it is good to learn how to use all of the tools available, even if they will not be used very often in real life...just in case, I guess. Thanks. I will let you know how this progresses. I have not gotten to using classes yet, so I do not know what to expect. I will skip forward in this book and see what I can come up with. Thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Wow, this is super complicated. After reading the chapter in the book on Classes, I am super confused. What I do not understand is how does the class get the data input into the textboxes by the user? I do not understand how to do that part at all.
-
Re: Display highlighted data from Listbox in Textboxes
The following is an example of a Contact class, which is just a way to encapsulate some data about a Person.
Code:
using System.Collections.ObjectModel;
class Program
{
static void Main(string[] args)
{
Collection<Contact> contacts = new Collection<Contact>();
Contact person1 = new Contact("Mario", "Catch", "123 NillyWilly Ln", "555-555-5555");
Contact person2 = new Contact("Luigi", "IHadTo", "124 NillyWilly Ln", "555-555-5556");
contacts.Add(person1);
contacts.Add(person2);
foreach (var contact in contacts)
{
contact.Show();
}
Console.ReadLine();
}
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public Contact() { }
public Contact(string firstName, string lastName, string address, string phoneNumber)
{
FirstName = firstName;
LastName = lastName;
Address = address;
PhoneNumber = phoneNumber;
}
public void Show()
{
Console.WriteLine(
"Person Information:\n-Name: {0}\n-Address: {1}\n-PhoneNumber: {2}\n\n",
FirstName + ' ' + LastName,
Address,
PhoneNumber);
}
}
With that, you now have access to any Contact in the contacts Collection. You can edit their properties, you can also set your form's textbox's to their properties.
ie:
Code:
textBoxFirstName.Text = contacts[0].FirstName;
-
Re: Display highlighted data from Listbox in Textboxes
My issue is adding the contact info INTO the Class FROM a textbox, not getting the info out and displaying it into a textbox. And I need to use an array to store the data, also.
-
Re: Display highlighted data from Listbox in Textboxes
In the example the data is fed to the new object through its constructor:
Code:
Contact person1 = new Contact("Mario", "Catch", "123 NillyWilly Ln", "555-555-5555");
Inside of the constructor the data is assigned to class level variables/properties for future access.
-
Re: Display highlighted data from Listbox in Textboxes
The class I created was striking similar to yours, but I tried to substitute the "text" you have here with variable names, and the names of the textboxes, and I got errors. I don't know how to get the data entered into a textbox into the class. thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
And I need to use an array to store the data, also.
I really wish you didn't have to ;) but i guess back to the basics...
Code:
static void Main(string[] args)
{
// Define number of contacts and contact properties (ie: name/address/number).
const int NumContacts = 2;
const int NumContactProperties = 3;
// Create a 2D array of contacts/contact properties.
/*
* contacts
* |
* > contact
* |
* > Name
* > Address
* > Number
* > contact
* |
* > Name
* > Address
* > Number
*
*/
string[][] contacts = new string[NumContacts][]; // 2D array declaration.
contacts[0] = new string[NumContactProperties]; // Initialize first contact array indices.
contacts[0][0] = "Mario Catch";
contacts[0][1] = "123 WillyNilly Ln";
contacts[0][2] = "555-555-5555";
contacts[1] = new string[NumContactProperties]; // Initialize second contact array indices.
contacts[1][0] = "Luigi IHadTo";
contacts[1][1] = "124 WillyNilly Ln";
contacts[1][2] = "555-555-5556";
foreach (string[] contact in contacts)
{
if (contact != null)
{
Console.WriteLine("Contact:");
Console.WriteLine("-Name: {0}\n-Address: {1}\n-PhoneNumber: {2}\n\n",
contact[0],
contact[1],
contact[2]);
}
}
Console.ReadLine();
}
Then you could do:
Code:
contacts[0][0] = textBoxName.Text;
contacts[0][1] = textBoxAddress.Text;
contacts[0][2] = textBoxPhoneNumber.Text;
You gain so much more by using a Collection<T> combined with encapsulation and object oriented programming.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
The class I created was striking similar to yours, but I tried to substitute the "text" you have here with variable names, and the names of the textboxes, and I got errors. I don't know how to get the data entered into a textbox into the class. thanks.
BigEd went over this briefly.
The class has a constructor that sets its properties to the parameter values of the constructor. The class also has public properties, so you don't have to use the constructor/
So, you could do:
Code:
Contact contact = new Contact();
contact.FirstName = textBoxFirstName.Text;
// and so on...
Or,
Code:
Contact contact = new Contact(textBoxFirstName.Text, textBoxLastName.Text, ...);
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
mariocatch
BigEd went over this briefly.
The class has a constructor that sets its properties to the parameter values of the constructor. The class also has public properties, so you don't have to use the constructor/
So, you could do:
Code:
Contact contact = new Contact();
contact.FirstName = textBoxFirstName.Text;
// and so on...
Or,
Code:
Contact contact = new Contact(textBoxFirstName.Text, textBoxLastName.Text, ...);
I will give this a shot. I guess I can then try to store each contact (instance of the class) into an array. Maybe an array of structures would be easier. I don't know. I will see. This problem really sucks because I have to use arrays to store the data, I have to be able to pull the information related to a contact when it is highlighted in the listbox, then send that data to individual textboxes. Also, there can only be a MAX of 20 entries. This is turning out to be a huge pain in the a$$.
-
Re: Display highlighted data from Listbox in Textboxes
You can store any type of 'object' in an array.
Code:
static void Main(string[] args)
{
const int NumContacts = 2;
Contact[] contacts = new Contact[NumContacts];
contacts[0] = new Contact("Mario", "Catch", "123 ASDF", "555-555-5555");
contacts[1] = new Contact("Luigi", "IHadTo", "123 FDSA", "555-555-5556");
foreach (Contact contact in contacts)
{
contact.Show();
}
Console.ReadLine();
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public Contact() { }
public Contact(string firstName, string lastName, string address, string phoneNumber)
{
FirstName = firstName;
LastName = lastName;
Address = address;
PhoneNumber = phoneNumber;
}
public void Show()
{
Console.WriteLine(
"Person Information:\n-Name: {0}\n-Address: {1}\n-PhoneNumber: {2}\n\n",
FirstName + ' ' + LastName,
Address,
PhoneNumber);
}
}
This is using Contact[], which is an array of Contacts.
You can set the properties of the Contact to the values of your TextBox's instead of hardcoded strings.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
mariocatch
I really wish you didn't have to ;) but i guess back to the basics...
Code:
static void Main(string[] args)
{
// Define number of contacts and contact properties (ie: name/address/number).
const int NumContacts = 2;
const int NumContactProperties = 3;
// Create a 2D array of contacts/contact properties.
/*
* contacts
* |
* > contact
* |
* > Name
* > Address
* > Number
* > contact
* |
* > Name
* > Address
* > Number
*
*/
string[][] contacts = new string[NumContacts][]; // 2D array declaration.
contacts[0] = new string[NumContactProperties]; // Initialize first contact array indices.
contacts[0][0] = "Mario Catch";
contacts[0][1] = "123 WillyNilly Ln";
contacts[0][2] = "555-555-5555";
contacts[1] = new string[NumContactProperties]; // Initialize second contact array indices.
contacts[1][0] = "Luigi IHadTo";
contacts[1][1] = "124 WillyNilly Ln";
contacts[1][2] = "555-555-5556";
foreach (string[] contact in contacts)
{
if (contact != null)
{
Console.WriteLine("Contact:");
Console.WriteLine("-Name: {0}\n-Address: {1}\n-PhoneNumber: {2}\n\n",
contact[0],
contact[1],
contact[2]);
}
}
Console.ReadLine();
}
Then you could do:
Code:
contacts[0][0] = textBoxName.Text;
contacts[0][1] = textBoxAddress.Text;
contacts[0][2] = textBoxPhoneNumber.Text;
You gain so much more by using a Collection<T> combined with encapsulation and object oriented programming.
I have a question on this method you have here. I see where you declare the number of contacts. In this problem, I need to have a MAXIMUM of 20 contacts, but it may be any number between 1 and 20. The user will enter the contact info, then click the "Add" button to add the contact information. How can I tell this to have a maximum of 20 entries without defining each entry from the beginning? This is the stupidest problem I have ever seen. ***.
-
Re: Display highlighted data from Listbox in Textboxes
Change the constant int NumContacts from 2 to 20.
Only initialize the indexes you need. The rest will be null, which is fine.
(You can't expand arrays in C# at runtime... what you set it to is what it's set to... which is why we are all saying use a Collection or List).
If you add a new contact to this array, you set the next index to a new string[NumContactProperties]. This will initialize a new index in your contacts array, with another array of properties for that contact.
You can remove a contact by setting an index to null.
-
Re: Display highlighted data from Listbox in Textboxes
wow, ok, thanks. I will let you know how it goes. This is one I should just skip, lol.
-
Re: Display highlighted data from Listbox in Textboxes
I just started working on this again. I created a structure, as you can see in the code. When I change the Array data type to the structure name, I get like 5 errors in the code, even if I change all the instances of the old Array name to the new Array name. I do not get this at all.
Code:
public partial class frmAddressBook : Form
{
public struct AddressBookEntries
{
public string firstName;
public string lastName;
public string address;
public string city;
public string state;
public string zipCode;
}
// Create Array to hold contact information
string[] Contacts = new string[20];
int i = 0;
public frmAddressBook()
{
InitializeComponent();
}
private void AddressBook_Load(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close program
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Declare variables
string lastName = tbxLastName.Text;
string firstName = tbxFirstName.Text;
string street = tbxStreet.Text;
string city = tbxCity.Text;
string state = tbxState.Text;
string zip = tbxZip.Text;
// Add contact information for New contact to array
if (i < 20)
{
Contacts[i] = lastName + ", " + firstName;
i++;
}
else
{
MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
}
//Clear TextBoxes
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
// Display list of contact names from array in the listbox
lstContacts.Items.Clear();
for (int j = 0; j < 20; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
// Delete selected contact
for (int k = 0; k < Contacts.Length; k++)
{
try
{
if (Contacts[k].Contains(lstContacts.SelectedItem.ToString()))
{
Contacts[k] = null;
}
}
catch
{
}
}
//Display Refreshed Records in ListBox
lstContacts.Items.Clear();
for (int j = 0; j < 10; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
tbxFirstName.Text = lstContacts.SelectedItem.ToString();
}
}
}
Here is the changed code that gives me all the errors:
Code:
{
public partial class frmAddressBook : Form
{
public struct AddressBookEntries
{
public string firstName;
public string lastName;
public string address;
public string city;
public string state;
public string zipCode;
}
// Create Array to hold contact information
AddressBookEntries[] Contacts = new AddressBookEntries[20];
int i = 0;
public frmAddressBook()
{
InitializeComponent();
}
private void AddressBook_Load(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close program
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Declare variables
string lastName = tbxLastName.Text;
string firstName = tbxFirstName.Text;
string street = tbxStreet.Text;
string city = tbxCity.Text;
string state = tbxState.Text;
string zip = tbxZip.Text;
// Add contact information for New contact to array
if (i < 20)
{
Contacts[i] = lastName + firstName + street + city + state + zip;
i++;
}
else
{
MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
}
//Clear TextBoxes
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
// Display list of contact names from array in the listbox
lstContacts.Items.Clear();
for (int j = 0; j < 20; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
// Delete selected contact
for (int k = 0; k < Contacts.Length; k++)
{
try
{
if (Contacts[k].Contains(lstContacts.SelectedItem.ToString()))
{
Contacts[k] = null;
}
}
catch
{
}
}
//Display Refreshed Records in ListBox
lstContacts.Items.Clear();
for (int j = 0; j < 10; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
tbxFirstName.Text = lstContacts.SelectedItem.ToString();
}
}
-
Re: Display highlighted data from Listbox in Textboxes
Could you post the errors you are getting? I assume they are compilation errors...
-
Re: Display highlighted data from Listbox in Textboxes
This looks like it would give an error:
Quote:
Contacts[i] = lastName + firstName + street + city + state + zip;
You can't set the Contacts[i] to a series of strings. You need to set each property from the AddressBookEntries (Contacts[i]) to the values you want.
ie:
Code:
Contacts[i].firstName = firstName;
Contacts[i].lastName = lastName;
...
Also, btw...
You should rename some of your variables, and follow some coding standards/guidelines as well..
Public properties should be PascalCase, not camelCase.
ie:
Code:
public struct AddressBookEntries
{
public string FirstName;
public string LastNAme;
public string Address;
public string City;
public string State;
public string ZipCode;
}
Local variables should be camelCase
Code:
AddressBookEntries[] contacts = new AddressBookEntries[20];
And, you should name collection variables after the name of the Type, with an 's' at the end to show plural.
ie:
Code:
AddressBookEntry[] addressBookEntries = new AddressBookEntry[20];
// or better yet...
Contact[] contacts = new Contact[20];
Also, using int i = 0; as a class member is very bad practice, as 'i' is usually reserved as an iterator for local for loops. Use a class member with a more specific name, such as:
Code:
private int mNumContacts = 0;
Just some advice, because I hate seeing un-standardized/non-uniformed code :)
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
nelo
Could you post the errors you are getting? I assume they are compilation errors...
There are lots of errors, too many to post. I am going to start all over on this and use an array of structures, then go from there.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
There are lots of errors, too many to post. I am going to start all over on this and use an array of structures, then go from there.
Why don't you zip up the project and attach it here?
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
Arjay
Why don't you zip up the project and attach it here?
I guess I could do that.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
Originally Posted by
CarlMartin
There are lots of errors, too many to post. I am going to start all over on this and use an array of structures, then go from there.
Are you new to the C#/Visual Studio compiler? What tends to happen is that there are errors that occur because of other errors. If you tackle them one by one from the beginning you might be able to sort them out. The ones you're not able to resolve you can post them here.
-
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.
Thanks.
-
Re: Display highlighted data from Listbox in Textboxes
Quote:
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
Thanks for the advice, I am working on this right now, and I will update if I get confused, have an issue, or get it done. We shall see. Thanks.
-
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?