Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one textbox in form in which i enter number.According to the number entered in textbox,combobox and textbox should be added in panel vertically.
Posted
Updated 25-Aug-12 1:38am
v2
Comments
[no name] 25-Aug-12 6:42am    
Your question does not make any sense, "number enter in textbox combobox" makes no sense at all. But the title of your question suggests that you have not even bothered to do the least bit of research on adding controls dynamically to a form.
[no name] 25-Aug-12 6:59am    
Yeah, he/she couldn't make a right English sentence.
ridoy 25-Aug-12 7:36am    
i couldn't understand what you have said!

Hi,

You can Add multiple control by creating a new instance of the control.

C#
var p1 = new Panel()
var controlCount = Convert.ToInt(textbox1.Text);
for(i=0;i<controlcount;i++)>
{
  var c1 = new ComboBox();
  // set all your required property of c1 here.
  p1.Controls.Add(c1);
}


This is how you can add multiple controls in your panel. And for your second issue regarding adding your control vertically, you can use StackPanel instead of Panel control to add your dynamically created controls.

Thanks
-Amit Gajjar
 
Share this answer
 
Comments
NandaKumer 25-Aug-12 12:12pm    
good answer. fived.
AmitGajjar 25-Aug-12 22:22pm    
Thank you NandaKumar :)
I hope you are referring Windows Form Application. If so follow the the stuff below:

C#
this.SuspendLayout();
TextBox txtbx = new TextBox();
txtbx.Text = existingTextBox.Text;
this.YourPanel.Controls.Add(txtbx);
//do similar stuff for combobox here.
this.ResumeLayout();
 
Share this answer
 
C#
int gap=10;
for(i=0;i<convert.toint32(txtnum.text);i++)>
{
    combobox c = new Combobox();
    textbox t = new textbox();
    pnl.controls.add(c);
    pnl.controls.add(t);
//for dynamically set position of textbox & combobox perform some calculations like below
    t.location=new point(txtNum.Location.x,txtNum.Location.Y + (txtNum.height * i) + gap);
    c.location=new point(txtNum.Location.x+ txtNum.width + gap,txtNum.Location.Y + (txtNum.height * i) + gap);
}

Happy Coding!
:)
 
Share this answer
 
 
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