If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

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:

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

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

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

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.

Re: Display highlighted data from Listbox in Textboxes

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

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

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

Re: Display highlighted data from Listbox in Textboxes

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

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.