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

Thread: Display highlighted data from Listbox in Textboxes

  1. #31
    Join Date
    Jan 2010
    Posts
    32

    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.

  2. #32
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Display highlighted data from Listbox in Textboxes

    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.

  3. #33
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Display highlighted data from Listbox in Textboxes

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

  4. #34
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

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

  5. #35
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    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.
    Last edited by mariocatch; January 22nd, 2010 at 02:25 PM. Reason: Description.

  6. #36
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

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

  7. #37
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    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.

  8. #38
    Join Date
    Jan 2010
    Posts
    32

    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.

  9. #39
    Join Date
    Jan 2010
    Posts
    32

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

  10. #40
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Display highlighted data from Listbox in Textboxes

    Could you post the errors you are getting? I assume they are compilation errors...

  11. #41
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Display highlighted data from Listbox in Textboxes

    This looks like it would give an error:

    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

  12. #42
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

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

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

    Re: Display highlighted data from Listbox in Textboxes

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

  14. #44
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by Arjay View Post
    Why don't you zip up the project and attach it here?

    I guess I could do that.

  15. #45
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Display highlighted data from Listbox in Textboxes

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

Page 3 of 4 FirstFirst 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