Display highlighted data from Listbox in Textboxes
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 58

Thread: Display highlighted data from Listbox in Textboxes

  1. #1
    Join Date
    Jan 2010
    Posts
    32

    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.



    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();
    
            }
        }
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.
    Last edited by BigEd781; January 20th, 2010 at 07:29 PM.

  3. #3
    Join Date
    Jan 2010
    Posts
    32

    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.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by CarlMartin View Post
    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.
    Last edited by BigEd781; January 20th, 2010 at 07:32 PM.

  5. #5
    Join Date
    Jan 2010
    Posts
    32

    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.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by CarlMartin View Post
    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.

  7. #7
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by BigEd781 View Post
    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.

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by CarlMartin View Post
    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.

  9. #9
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by BigEd781 View Post
    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.

  10. #10
    Join Date
    Jan 2010
    Posts
    32

    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

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    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".

  12. #12
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    Wow, that is going to make it complicated, lol. Thanks.

  13. #13
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by CarlMartin View Post
    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.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,441

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by CarlMartin View Post
    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?

  15. #15
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by Arjay View Post
    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.

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center