Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have ListBox and it has some numbers (random items between 0 to 10 items in listbox)
and now i want to write these items into 10 textbox when i click the button
what should i do?
With Respect "Spaceman"
Posted
Comments
[no name] 29-Aug-14 10:21am    
What should you do? Get yourself a decent book on basic programming and work through it. Then figure out the difference between "Improve question" and "Add solution" that are not solutions at all.

Hi,
Place a Panel as panel1,listbox and a button in your form.
in button1_click Event write the code.

C#
private void button1_Click(object sender, EventArgs e)
       {
           int locx = 0;
           int locy = 2;
           foreach (var listBoxItem in listBox1.Items)
           {
               TextBox ctrl = new TextBox();
               ctrl.Name = listBoxItem.ToString();
               ctrl.Size = new System.Drawing.Size(50, 20);
               ctrl.Location = new Point(locx, locy);
               ctrl.Text = listBoxItem.ToString();
               locx = locx + 50;
               panel1.Controls.Add(ctrl);
           }
       }


In Button Click using loop i read all the listbox item and genearate textbox at runtime and add to Panel Control.
Hope this code will help you.
 
Share this answer
 
Comments
Ashi0891 29-Aug-14 2:32am    
Perfect solution.
I have given you sample now you have to customize to your requirement.
You 1) Question :
How to find the textbox Name :
You have to do some modification you can declare the Textbox Control as textbox Array as global 2) insted of lox you can increment locy. by this you can display textbox in Column.
Check this code hope this will help you.
C#
TextBox[] ctrl;
      private void button1_Click(object sender, EventArgs e)
      {
          int locx = 0;
          int locy = 2;
          ctrl = new TextBox[listBox1.Items.Count];
          int i = 0;
          foreach (var listBoxItem in listBox1.Items)
          {
              ctrl[i] = new TextBox();
              ctrl[i].Name = i.ToString();
              ctrl[i].Size = new System.Drawing.Size(50, 20);
              ctrl[i].Location = new Point(locx, locy);
              ctrl[i].Text = listBoxItem.ToString();
              locy = locy + 20;

              panel1.Controls.Add(ctrl[i]);
              i = i + 1;
          }
      }
 
Share this answer
 
C#
// set all text name in array 1,2,3,....
            TextBox[] txtArray = new TextBox[3] { textBox1, textBox2, textBox3 };
//push all list in text 0 to n-1
           for(int i=0;i<=txtArray.Length-1;i++)
            {
                txtArray[i].Text=listBox1.Items[i].ToString();
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900