CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Posts
    16

    problem with this code

    Code:
     int itemCount;
            public struct addressBook
            {
                public string lastName;
                public string firstName;
                public string street;
                public string city;
                public string state;
                public int zip;
            }
            addressBook[] book = new addressBook[20];
     
            public MainForm()
            {
                InitializeComponent();
            }
    
    private void btnAdd_Click(object sender, EventArgs e)
            {
               
                    
                    string wholeName = "";
                    try
                    {
                       // if (lstDisplay.SelectedIndex != 0)
                       // {
                            book[itemCount].lastName = txtLast.Text;
                            book[itemCount].firstName = txtFirst.Text;
                            book[itemCount].street = txtStreet.Text;
                            book[itemCount].city = txtCity.Text;
                            book[itemCount].state = txtState.Text;
                            book[itemCount].zip = int.Parse(mTxtZip.Text);
    
                            itemCount++;
                            clear();
                          //  lbltest.Text = book[itemCount].ToString();
                        //}
                         
                       // else
                       // {
                        //    MessageBox.Show("ERROR!!\n" + "Check your input.");
                       // }
                    }
                    catch (FormatException)
                    {
                        MessageBox.Show("ERROR!!\n" + "Check your input.");
                    }
            
                    for (int i = 0; i < book.Length; i++)
                    {
                        wholeName = book[i] + "\n" + wholeName  ;
                       
                    }
                    lstDisplay.Text = wholeName; //book[itemCount].lastName + " , " + book[itemCount].firstName;
                    lbltest.Text = wholeName;
            }
    what i want is when the user enters the info in text boxes for that info to be stored into an array then the last name, first name to be shown up in the listtextbox but all i get now is nothing. Any help would be appreciated. lstDisplay.Text is my list text box

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

    Re: problem with this code

    So, a couple things...

    1. What does the "clear" method do?
    2. The loop that constructs "wholeName" is odd. You add "book[i]", which is a struct, to the front of it. That will call "book[i].ToString()" and will not return what you want. You then append an i9ncorrect newline. Newlines in Windows are \r\n, not \n, and you should be using Environment.NewLine instead anyway. You then proceed to append "wholeName" to itself at the end. I don't see how that could ever produce something useful. It is going to be a huge string full of garbage. Try this:

    Code:
    addressBook entry = book[itemCount];
    wholeName = entry.firstName + " " + entry.lastName;

  3. #3
    Join Date
    Jan 2010
    Posts
    16

    Re: problem with this code

    clear clears my text boxes-- i just didn't include that in my showcase for you
    i tried using your code and nothing happens
    i am also adding to a list box not a text box
    Last edited by Darenr; January 11th, 2010 at 08:34 PM.

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

    Re: problem with this code

    I don't know what you mean by "nothing happens". Why don't you use the debugger to see what is actually going on, i.e., what is the real content of "wholeName"?

  5. #5
    Join Date
    Jan 2010
    Posts
    16

    Re: problem with this code

    i mean the list box remains empty.
    nothing shows up in there.---
    do you think using an array and not a struct would be easiest for this application?

  6. #6
    Join Date
    Jan 2010
    Posts
    16

    Re: problem with this code

    here is the whole code




    Code:
    namespace Unit_10_Project
    {
        public partial class MainForm : Form
        {
            int itemCount;
            public struct addressBook
            {
                public string lastName;
                public string firstName;
                public string street;
                public string city;
                public string state;
                public int zip;
            }
            addressBook[] book = new addressBook[20];
            
            public MainForm()
            {
                InitializeComponent();
            }
            
    
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            private void clearBoxes()
            {
                txtCity.Clear();
                txtFirst.Clear();
                txtLast.Clear();
                txtState.Clear();
                txtStreet.Clear();
                mTxtZip.Clear();
            }
           
            private void btnAdd_Click(object sender, EventArgs e)
            {
    
                addressBook entry = book[itemCount];
                    
                    string wholeName = "";
                    try
                    {
                        
                        
                            book[itemCount].lastName = txtLast.Text;
                            book[itemCount].firstName = txtFirst.Text;
                            book[itemCount].street = txtStreet.Text;
                            book[itemCount].city = txtCity.Text;
                            book[itemCount].state = txtState.Text;
                            book[itemCount].zip = int.Parse(mTxtZip.Text);
    
                            itemCount++;
                          
                         
                    
                         
                     
    
                          //  lBxDisplay.Items.Add(txtLast.Text + "" + txtFirst.Text);
                    } 
                    catch (FormatException)
                    {
                        MessageBox.Show("ERROR!!\n" + "Check your input.");
                    }
    
                   
    
            
                    for (int i = 0; i < book.Length; i++)
                    {
                        
                        //wholeName = entry.firstName + " " + entry.lastName;
    
                        wholeName = book[i]+ "\n" + wholeName  ;
                       
                    }
                    lBxDisplay.Text = wholeName; //book[itemCount].lastName + " , " +                 book[itemCount].firstName;
                  
                   
                    
            }
    
            private void btnDel_Click(object sender, EventArgs e)
            {
    
            }
    
            private void btnClear_Click(object sender, EventArgs e)
            {
                clearBoxes();
            }
        
    
          
        }
    }

  7. #7
    Join Date
    Jan 2010
    Posts
    4

    Re: problem with this code

    Assuming you deleted everything after your "catch" block, I think uncommenting the following line would work:

    Code:
    lBxDisplay.Items.Add(txtLast.Text + "" + txtFirst.Text);

    I agree with BigEd781. You don't need the "for" loop to construct your wholeName variable. You don't want to loop over your entire address book each time a user enters their information. You just want to take the last name and first name that the user entered, construct a string using the last name and first name, and add that string as a list box item to the list box.

    The following code worked for me:

    Code:
            int itemCount;
            public struct addressBook
            {
                public string lastName;
                public string firstName;
                public string street;
                public string city;
                public string state;
                public int zip;
            }
            addressBook[] book = new addressBook[20];
    
            public Form1()
            {
                InitializeComponent();
                itemCount = 0;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {          
                    book[itemCount].lastName = txtLast.Text;
                    book[itemCount].firstName = txtFirst.Text;
                    book[itemCount].street = txtStreet.Text;
                    book[itemCount].city = txtCity.Text;
                    book[itemCount].state = txtState.Text;
                    book[itemCount].zip = int.Parse(mTxtZip.Text);
                    lstDisplay.Items.Add(txtLast.Text + " , " + txtFirst.Text);
                    itemCount++;
                }
                catch (FormatException)
                {
                    MessageBox.Show("ERROR!!\n" + "Check your input.");
                }            
            }

  8. #8
    Join Date
    Jan 2010
    Posts
    16

    Re: problem with this code

    ok i have most of it working now thanx to you peeps. Now how about if i have lets say a , b , c ,f in the list box and then someone inputs d . When i tried it that way as a sorted by last name array the new input goes on the end instead of the sorted location. How can i make it go to the sorted location as well as delete any items that i choose from the list box and have it sort back .



    Code:
    namespace Unit_10_Project
    {
        public partial class MainForm : Form
        {
            int itemCount;
            public struct addressBook
            {
                public string lastName;
                public string firstName;
                public string street;
                public string city;
                public string state;
                public int zip;
            }
            addressBook[] book = new addressBook[20];
            
            public MainForm()
            {
                InitializeComponent();
            }
            
    
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            private void clearBoxes()
            {
                txtCity.Clear();
                txtFirst.Clear();
                txtLast.Clear();
                txtState.Clear();
                txtStreet.Clear();
                mTxtZip.Clear();
            }
           
            private void btnAdd_Click(object sender, EventArgs e)
            {
                
                    try
                    {
                       
                        
                            book[itemCount].lastName = txtLast.Text ;
                            book[itemCount].firstName = txtFirst.Text ;
                            book[itemCount].street = txtStreet.Text;
                            book[itemCount].city = txtCity.Text;
                            book[itemCount].state = txtState.Text;
                            book[itemCount].zip = int.Parse(mTxtZip.Text);
                            lBxDisplay.Items.Add(book[itemCount].lastName + " , " + book[itemCount].firstName);
                            itemCount++;
                           
                            clearBoxes();
                            txtFirst.Focus();
                        
                    } 
                    catch (FormatException)
                    {
                        MessageBox.Show("ERROR!!\n" + "Check your input.");
                    }
    
    
                    
                    
                    
            }
    
            private void btnDel_Click(object sender, EventArgs e)
            {
                
                    if (lBxDisplay.SelectedIndex == -1)
                    {
                        MessageBox.Show("Please select an item first.", "No item selected");
                    }
                    else
                    {
                        lBxDisplay.Items.RemoveAt(lBxDisplay.SelectedIndex);
                        
                    } 
                
            }
    
            private void lBxDisplay_SelectedIndexChanged(object sender, EventArgs e)
            {
                int listIndex = lBxDisplay.SelectedIndex;
    
               
                if(lBxDisplay.SelectedItem != null)
                {
                    txtLast.Text = book[listIndex].lastName.ToString();
                    txtFirst.Text = book[listIndex].firstName.ToString();
                    txtCity.Text = book[listIndex].city.ToString();
                    txtState.Text = book[listIndex].state.ToString();
                    txtStreet.Text = book[listIndex].street.ToString();
                    mTxtZip.Text = book[listIndex].zip.ToString();
    
                }
                
                listIndex++;
            }
    
           
        
    
          
        }
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured