Hi
I want to create text box array at desing time.It means i want to create text box like Text box1[1],TextBox[2],TextBox[3].How do u create it in desing time in C#.
Thanks
Tank50
Hi
I want to create text box array at desing time.It means i want to create text box like Text box1[1],TextBox[2],TextBox[3].How do u create it in desing time in C#.
Thanks
Tank50
Short answer: there isn’t a built‑in “control array” in the C# designer the way VB6 had one. The Visual Studio designer creates individual named controls (textBox1, textBox2, …). To get the behavior you want you either create the controls dynamically (as showed) or place the controls in the designer and then treat the designer-created controls as an indexed collection at runtime.
A practical, safe pattern for WinForms is to name the design-time TextBoxes with a consistent prefix (eg. txt0, txt1, …), then build an ordered array/list after InitializeComponent() by enumerating the form’s controls (including nested containers) and sorting by the numeric suffix. The snippet below shows a small recursive helper and one way to build that ordered array:
IEnumerable<Control> AllControls(Control parent)
{
foreach (Control c in parent.Controls)
{
yield return c;
foreach (var child in AllControls(c)) yield return child;
}
}
// after InitializeComponent()
var boxes = AllControls(this)
.OfType<TextBox>()
.Where(tb => tb.Name.StartsWith("txt"))
.OrderBy(tb => {
var suffix = tb.Name.Substring(3);
return int.TryParse(suffix, out var n) ? n : int.MaxValue;
})
.ToArray(); Alternatives: use a TableLayoutPanel or FlowLayoutPanel to design a grid/row visually and iterate its Controls collection; create a small UserControl that exposes an indexer and drop that on the form; or (for data-driven UIs) use ItemsControl/WPF data templates or ASP.NET templated/data controls (Repeater/GridView) so the engine creates the set for you.
For data binding (answering ): in WinForms you can bind a TextBox via DataBindings.Add("Text", bindingSource, "PropertyName", true, DataSourceUpdateMode.OnPropertyChanged"). In WebForms use templated controls with Bind/Eval. Finally, avoid manually placing large numbers of identical boxes in the designer — prefer a data-driven or programmatic approach when the count is variable.
Jump to Post— ddanbe 2,724This keeps to be a recurring question. Try the search function on this site first before posting. I tried to search for array, textbox, sudoku in the search string, the first hit was : http://www.daniweb.com/forums/thread180533.html
Jump to Post— kvprajapati 1,826Hi
I want to create text box array at desing time.It means i want to create text box like Text box1[1],TextBox[2],TextBox[3]. How do u create it in desing time in C#.
Thanks
Tank50You may create an array of TextBox at runtime (dynamically).
..... .... TextBox []t=new …
This keeps to be a recurring question. Try the search function on this site first before posting. I tried to search for array, textbox, sudoku in the search string, the first hit was : http://www.daniweb.com/forums/thread180533.html
Hi
I want to create text box array at desing time.It means i want to create text box like Text box1[1],TextBox[2],TextBox[3]. How do u create it in desing time in C#.
Thanks
Tank50
You may create an array of TextBox at runtime (dynamically).
.....
....
TextBox []t=new TextBox[5]; // Array of Five TextBox object variables.
// Create an instance of each TextBox
for(int i=0;i<t.Length;i++) {
t[i]=new TextBox();
.....
}
.....
..... May I ask why there is a need to add
t[i] = new TextBox(); when there is already a declaration
TextBox []t=new TextBox[5]; ?
Thanks.
TextBox[] t = new TextBox[5] The declaration above is to make the array itself, and;
t[i] = new TextBox(); This one is for creating instances of each members in the array. Actually, the explanations are included in the Syntax made by the poster above you. Just gotta pay a little more attention. :P
How to do the data binding for the text box?
Yes you are right.
Sorry about it. Thans for the explanation :)
TextBox[] t = new TextBox[5]The declaration above is to make the array itself, and;
t[i] = new TextBox();This one is for creating instances of each members in the array. Actually, the explanations are included in the Syntax made by the poster above you. Just gotta pay a little more attention. :P
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.