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

Thread: Display highlighted data from Listbox in Textboxes

  1. #46
    Join Date
    Jan 2010
    Posts
    32

    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.

  2. #47
    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
    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...

  3. #48
    Join Date
    Jan 2010
    Posts
    32

    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.

  4. #49
    Join Date
    Jan 2010
    Posts
    32

    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.

  5. #50
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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?

  6. #51
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    thanks. Yes, the code has no errors, it compiles, and it works pretty much. The reason I am storing the structures into an array is because that's what the directions ask for. Basically, I want each contact to be a structure that is then stored into an array with a maximum number of contacts of 20. Here is the exact problem in the book:

    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


    Here is the updated code:

    Code:
    public struct Contacts
    {
                public string FirstName;
                public string LastName;
                public string Street;
                public string City;
                public string State;
                public string Zip;
    
    }           Contacts [] arrayOfContacts = 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;
    
                    // Add contact to array
                    arrayOfContacts[0] = entry;
                    
                    // 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 = "";
    
                } 
    
           
        }
    }
    So, now this structure created by clicking the "Add" button is added to the array as element "0", correct?
    Last edited by CarlMartin; January 25th, 2010 at 08:54 PM.

  7. #52
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Display highlighted data from Listbox in Textboxes

    That's correct. You're making good progress. What I recommend now is that you define an integer field to act as counter/index for your array. This will ensure that you don't ovewrite existing entries and that you don't exceed the bounds of the array. So define the counter/index and initialise it to '0'. Replact the arrayOfContacts[0] with arrayOfContents[index] and then increment the index. You can actually do it all in one step like this:
    Code:
    arrayOfContacts[index++] = entry;
    Note that 'index' has to be a field (i.e. a class wide variable) as opposed to a local variable otherwise it wont work.

    You can also put a check in the end to reset the index if it reaches 20. I'll let you work out how to do that for yourself. There are a couple ways of doing it. Go for the simplest way you can think of.

  8. #53
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    ok, thanks, be back when I get some progress happening.

  9. #54
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    How about this:

    Code:
    public struct Contacts
    {
                public string FirstName;
                public string LastName;
                public string Street;
                public string City;
                public string State;
                public string Zip;
    
    }           Contacts [] arrayOfContacts = new Contacts [20];
                int index = 0;
    
                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;
    
                    // Add contact to array
                    if (index < 20)
                    {
                        arrayOfContacts[index++] = entry;                  
    
                    }
    
                    else
                    {
                        MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
                    }
    
                    
                    // Display Contacts (First and Last name only) in Listbox
                    lstContacts.Items.Add(entry.LastName + ", " + entry.FirstName);
    
                    // Return message when Array reaches maximum of 20 entries
    
                    // Clear Text Boxes
                    tbxFirstName.Text = "";
                    tbxLastName.Text = "";
                    tbxStreet.Text = "";
                    tbxCity.Text = "";
                    tbxState.Text = "";
                    tbxZip.Text = "";
    
                } 
    
           
        }
    }
    Ok, so that seems to work fine. I was trying to code the "Delete" button which will delete the entry in the array that corresponds to the name selected in the listbox. I tried to do this with a for loop seen below, but I am getting a lot of errors. Should I be doing this a different way? This works fine in another program I have that uses an array rather than an array of structures.

    Code:
    private void btnDelete_Click(object sender, EventArgs e)
                {
                    for (int k = 0; k < arrayOfContacts.Length; k++)
                
    
                    try
                    {
    
                        if (arrayOfContacts[k] = lstContacts.SelectedItem.ToString())
                        {
    
                            arrayOfContacts[k] = null;
    
                        }
    
                    }
    
                    catch
                    {
    
                    }
    
    
    
                }
    
                //Display Refreshed Records in ListBox
                lstContacts.Items.Clear();
    
                for (int j = 0; j < index; j++)
                {
    
                    if (arrayOfContacts[j] != null)
                    {
    
                        lstContacts.Items.Add(arrayOfContacts[j].ToString().Trim());
    
                    }
    Last edited by CarlMartin; January 25th, 2010 at 09:38 PM.

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

    Re: Display highlighted data from Listbox in Textboxes

    Quote Originally Posted by CarlMartin View Post
    How about this:
    How about zipping the project up and attaching it?

    That way we can load it up, compile it and the compiler can show me your errors.

    Then we can post back and tell you how to identify the specific error and what to do about it.

  11. #56
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    here you go. File attached.
    Attached Files Attached Files

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

    Re: Display highlighted data from Listbox in Textboxes

    The first thing to do is get rid of the compile errors and then add functionality back in.

    Comment out the following lines of code:

    1)

    Code:
     
    if (arrayOfContacts[k] = lstContacts.SelectedItem.ToString())
    {
      arrayOfContacts[k] = null;
    }
    2)
    Code:
     
    lstContacts.Items.Clear();
    3)
    Code:
     
    for (int j = 0; j < index; j++)
    {
      if (arrayOfContacts[j] != null)
      {
        lstContacts.Items.Add(arrayOfContacts[j].ToString().Trim());
    
      }
    NOTE: Comment out exactly what I have shown. Don't comment out anything more (even an extra curly brace).

    If you comment out what I've coded then your app should compile.

    From there we can look at the other errors.

  13. #58
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    Did that, and it does compile. I knew where the errors were, I am just not proficient in C# to do what I need to do. That was my attempt at whet I thought would be the proper code, but I was incorrect. There are no other errors with that all commented out.

    I hope I can get this crazy program finished by tomorrow night because I have been working on it for awhile and I have to go out of town for a week on Wednesday, lol.
    Last edited by CarlMartin; January 25th, 2010 at 11:12 PM.

Page 4 of 4 FirstFirst 1234

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