locked
Getting Values from multiple textboxes RRS feed

  • Question

  • Hello to All,

    I am pretty sure I am missing something basic and hence I am looking for some guidance. 

    I am trying to get the values from textboxes. 

    The only complication is that I am getting confused since I am creating the textboxes through a loop. 

    The number of textbox created depends on the index selected in a combobox as follows:

     private void numberOfKits_SelectedIndexChanged(object sender, EventArgs e)
            {
     int x = 9;
     int y = 55;
     for (int index = 0; index < Convert.ToInt16(numberOfKits.Text); index++)
                {
                    TextBox txtbox = new TextBox();
                    txtbox.Name = "txtbox" + index.ToString();
                    txtbox.Location = new Point(x,y);
                    txtbox.Size = new Size(161, 20);
                   this.groupBox2.Controls.Add(txtbox);
                    y = y + 30;
                   
                 }
    }
    

    so if the selected item in combobox is 2 then 2 textboxes are created. 

    Now the user will input some values into the textboxes for example:

    first textbox : B124C

    second txtbox: C546D

    What I am looking for is to read these values after the user inputs them. 

    Any suggestion is greatly appreciated.

    Thank You

    Tuesday, December 3, 2013 9:07 PM

Answers

  • Something like this

            List<TextBox> boxes = new List<TextBox>();
            private void numberOfKits_SelectedIndexChanged(object sender, EventArgs e)
            {
                int x = 9;
                int y = 55;
                for (int index = 0; index < Convert.ToInt16(numberOfKits.Text); index++)
                {
                    TextBox txtbox = new TextBox();
                    txtbox.Name = "txtbox" + index.ToString();
                    txtbox.Location = new Point(x, y);
                    txtbox.Size = new Size(161, 20);
                    boxes.Add(txtbox);
                    this.groupBox2.Controls.Add(txtbox);
                    y = y + 30;
                }
            private void OK_Click(object sender, EventArgs e)
            {
                foreach(TextBox bx in boxes)
                {
                    Console.WriteLine(" {0} : {1}", bx.Name, bx.Text);
                }
            }


    jdweng

    Wednesday, December 4, 2013 6:07 PM
  • try this code

    foreach (Control cc in GroupBox)
                    {
                        if (cc is TextBox)
                        {
                            MessageBox.Show(cc.Name);
                        }
                    }


    Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

    Thursday, December 5, 2013 7:25 AM

All replies

  • You need oto add the event handler for the TextChange Event.  You only need one handler for all your textboxes.  The sender will indicate which text box has changed.

           private void numberOfKits_SelectedIndexChanged(object sender, EventArgs e)
            {
                int x = 9;
                int y = 55;
                for (int index = 0; index < Convert.ToInt16(numberOfKits.Text); index++)
                {
                    TextBox txtbox = new TextBox();
                    txtbox.TextChanged += new EventHandler(txtbox_TextChanged);
                    txtbox.Name = "txtbox" + index.ToString();
                    txtbox.Location = new Point(x, y);
                    txtbox.Size = new Size(161, 20);
                    this.groupBox2.Controls.Add(txtbox);
                    y = y + 30;
                }
            }
            private void txtbox_TextChanged(object sender, EventArgs e)
            {
            }


    jdweng

    Tuesday, December 3, 2013 9:31 PM
  • Thank you for replying back.

    I tried this solution. The problem is it takes the value of only first textbox always. 

    The reason is, both textboxes are created at the same time and the user inputs the value for both at the same time. I need a way it can store the values individually. The end aim is to create folders with name same as the ones user inputs into the textboxes. That is why I need to have the values individually. 


    Tuesday, December 3, 2013 10:29 PM
  • You need to store the references to these textboxes somewhere, otherwise you cannot access them after the loop closes.

    private TextBox[] Boxes;
    
     private void numberOfKits_SelectedIndexChanged(object sender, EventArgs e)
    {
     int x = 9;
     int y = 55;
     int NumberOfBoxes = Convert.ToInt16(numberOfKits.Text);
     Boxes = new TextBox[NumberOfBoxes]();
     for (int index = 0; index < numberOfBoxes; index++)
                {
                    TextBox txtbox = new TextBox();
                    txtbox.Name = "txtbox" + index.ToString();
                    txtbox.Location = new Point(x,y);
                    txtbox.Size = new Size(161, 20);
                    Boxes[index] = txtbox;
                   this.groupBox2.Controls.Add(txtbox);
                    y = y + 30;
                   
                 }
    }
    
    Your code that later uses those textbose can jsut iterate over the Boxes Array, taking one at a time.

    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


    Tuesday, December 3, 2013 10:54 PM
  • You need to uniquely identify each textbox.  Depending on which text box changes will determine what you program does with the data.  suppose you had textbox1 as the person first name and textbox2 as the last name.  So when textbox1 change you are going to store the text into an object that contains the persons first name and when textbox2 changes you are gong to store the data into an object that contains the persons last name.

    So in the code below I am getting the textbox name.

            private void txtbox_TextChanged(object sender, EventArgs e)
            {
                TextBox box = sender as TextBox;
                string name =  box.Name;
            }

      Now you have a 2nd issue of when does the data entry completes.  The change event will occur after every character is typed. So you need a button for the user to press when all the data is entered.  You may want to create a List<> of textboxes like in the code below so you can retrieve data from the boxes without an event.

    See changes below

           List<TextBox> boxes = new List<TextBox>();
            private void numberOfKits_SelectedIndexChanged(object sender, EventArgs e)
            {
                int x = 9;
                int y = 55;
                for (int index = 0; index < Convert.ToInt16(numberOfKits.Text); index++)
                {
                    TextBox txtbox = new TextBox();
                    txtbox.TextChanged += new EventHandler(txtbox_TextChanged);
                    txtbox.Name = "txtbox" + index.ToString();
                    txtbox.Location = new Point(x, y);
                    txtbox.Size = new Size(161, 20);
                    boxes.Add(txtbox);
                    this.groupBox2.Controls.Add(txtbox);
                    y = y + 30;
                }


    jdweng

    Wednesday, December 4, 2013 3:01 AM
  • I am still not being able to find the solution.

    In this case, if I check the list boxes , the first field [0] is empty and it reads only the second one [1]. 

    These textboxes are dynamically created on a form but these values are passed on to another form. 

    The form on which the textbox are created also has a OK button. So basically I want to read the values in the list once the OK button is pressed. 

    Thank you for replying back :-)

    Wednesday, December 4, 2013 5:43 PM
  • Something like this

            List<TextBox> boxes = new List<TextBox>();
            private void numberOfKits_SelectedIndexChanged(object sender, EventArgs e)
            {
                int x = 9;
                int y = 55;
                for (int index = 0; index < Convert.ToInt16(numberOfKits.Text); index++)
                {
                    TextBox txtbox = new TextBox();
                    txtbox.Name = "txtbox" + index.ToString();
                    txtbox.Location = new Point(x, y);
                    txtbox.Size = new Size(161, 20);
                    boxes.Add(txtbox);
                    this.groupBox2.Controls.Add(txtbox);
                    y = y + 30;
                }
            private void OK_Click(object sender, EventArgs e)
            {
                foreach(TextBox bx in boxes)
                {
                    Console.WriteLine(" {0} : {1}", bx.Name, bx.Text);
                }
            }


    jdweng

    Wednesday, December 4, 2013 6:07 PM
  • try this code

    foreach (Control cc in GroupBox)
                    {
                        if (cc is TextBox)
                        {
                            MessageBox.Show(cc.Name);
                        }
                    }


    Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

    Thursday, December 5, 2013 7:25 AM