Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the below code to add the textbox to the panel dynamically:

C#
{
      TextBox[] textbox_item_array = new TextBox[6]; item_textbox.Add(textbox_item_array);
      textbox_item_array[0] = new TextBox();
      textbox_item_array[0].Width = label_bill_item_code.Width;
      textbox_item_array[0].Height = 26;
      textbox_item_array[0].Font = print_font_default;
      textbox_item_array[0].Location = new Point(label_bill_item_code.Location.X, 45 + (20 * row_count));
      textbox_item_array[0].Name = string.Concat("item_code", row_count.ToString());
      panel_bill_item_collections.Controls.Add(textbox_item_array[0]);
      textbox_item_array[0].KeyDown += new KeyEventHandler(dynamic_textbox_keydown);
      textbox_item_array[0].Leave += new EventHandler(dynamic_text_item_code_Leave);
      textbox_item_array[0].DoubleClick += new EventHandler(bill_new_Form_DoubleClick);
      textbox_item_array[1] = new TextBox();
      textbox_item_array[1].Width = label_bill_item_details.Width;
      textbox_item_array[1].Font = textbox_item_array[0].Font;
      textbox_item_array[1].Location = new Point(label_bill_item_details.Location.X, textbox_item_array[0].Location.Y);
      textbox_item_array[1].Name = string.Concat("item_description", row_count.ToString());
      textbox_item_array[1].KeyDown += new KeyEventHandler(dynamic_textbox_keydown);
      panel_bill_item_collections.Controls.Add(textbox_item_array[1]);
      textbox_item_array[2] = new TextBox();
      textbox_item_array[2].Width = label_bill_item_unit.Width;
      textbox_item_array[2].Font = textbox_item_array[0].Font;
      textbox_item_array[2].Location = new Point(label_bill_item_unit.Location.X, textbox_item_array[0].Location.Y);
      textbox_item_array[2].Name = string.Concat("item_unit", row_count.ToString());
      textbox_item_array[2].KeyDown += new KeyEventHandler(dynamic_textbox_keydown);
      panel_bill_item_collections.Controls.Add(textbox_item_array[2]);
      textbox_item_array[3] = new TextBox();
      textbox_item_array[3].Width = label_bill_item_price.Width;
      textbox_item_array[3].Font = textbox_item_array[0].Font;
      textbox_item_array[3].Location = new Point(label_bill_item_price.Location.X, textbox_item_array[0].Location.Y);
      textbox_item_array[3].Name = string.Concat("item_price", row_count.ToString());
      textbox_item_array[3].KeyDown += new KeyEventHandler(dynamic_textbox_keydown);
      panel_bill_item_collections.Controls.Add(textbox_item_array[3]);
      textbox_item_array[4] = new TextBox();
      textbox_item_array[4].Width = label_bill_item_quantity.Width;
      textbox_item_array[4].Font = textbox_item_array[0].Font;
      textbox_item_array[4].Location = new Point(label_bill_item_quantity.Location.X, textbox_item_array[0].Location.Y);
      textbox_item_array[4].Name = string.Concat("item_quantity", row_count.ToString());
      textbox_item_array[4].KeyDown += new KeyEventHandler(dynamic_textbox_keydown);
      textbox_item_array[4].Leave += new EventHandler(bill_new_Form_Leave);
      panel_bill_item_collections.Controls.Add(textbox_item_array[4]);
      textbox_item_array[5] = new TextBox();
      textbox_item_array[5].Width = label_bill_item_total.Width;
      textbox_item_array[5].Font = textbox_item_array[0].Font;
      textbox_item_array[5].Location = new Point(label_bill_item_total.Location.X, textbox_item_array[0].Location.Y);
      textbox_item_array[5].Name = string.Concat("item_total", row_count.ToString());
      textbox_item_array[5].KeyDown += new KeyEventHandler(dynamic_textbox_keydown);
      panel_bill_item_collections.Controls.Add(textbox_item_array[5]);
      row_count++;
      textbox_item_array[0].Focus();
  }


The code works fine while the size of the panel is not changed but if the size get changed, the panel Malfunction. What I can see is that if I increase the size of the panel it works fine till it approaches its size then it has problem again after it get bigger. Can you please help me with this problem? If the panel is not appropriate for this use, can you guide me which container is fine.
Posted
Updated 2-Jun-13 22:37pm
v2
Comments
CHill60 3-Jun-13 4:52am    
In what way does it "malfunction"?
Member 9646334 3-Jun-13 4:54am    
It malfunction in the way that the index start not to become the real index of the textbox. More over, instead of the textbox coming below each other when the panel is in good size, when it is resized, they start to become far from each other.
CHill60 3-Jun-13 5:05am    
Do the answers and comments here problem in adding combobox in runtime[^] help at all (there's a lot of them I'm afraid)
Sunasara Imdadhusen 3-Jun-13 8:31am    
Are you usinf WinForms application? Do you want to create dynamic control inside container?

1 solution

When your size becomes bigger, it exceeds the viewport size of the panel. Wrap the panel in a Scrollable control and size the panel accordingly to fit it's children. Applies both for WPF and WinForms controls.

Good Luck!
 
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