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

Thread: Display highlighted data from Listbox in Textboxes

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

    Re: Display highlighted data from Listbox in Textboxes

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

  2. #17
    Join Date
    Jan 2010
    Posts
    32

    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.

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

    Re: Display highlighted data from Listbox in Textboxes

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

  4. #19
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #20
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

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

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

    Re: Display highlighted data from Listbox in Textboxes

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

  7. #22
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

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

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

    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.

  9. #24
    Join Date
    Jan 2010
    Posts
    32

    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.

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

    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

  11. #26
    Join Date
    Jan 2010
    Posts
    32

    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.

  12. #27
    Join Date
    Jan 2010
    Posts
    32

    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.

  13. #28
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    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;

  14. #29
    Join Date
    Jan 2010
    Posts
    32

    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.

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

    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.

Page 2 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