Creating an indexed control array?

Does C # have indexed control arrays or not? I would like to add a “button array”, for example, using 5 buttons that use only one event handler that processes the index of all these 5 controls (for example, VB6). I also have to write one additional event handler for each of these 5 buttons. And if I have 100 buttons, do I need 100 event handlers? I mean something like this:

TextBox1[i].Text="Example"; 

This can make coding definitely easier for me to work with control arrays. Now I have seen that C # at least has no visible array functions for user controls and no “index” property on user controls. Therefore, I assume that C # does not have control arrays, or I must call each element by a known name.

Instead of giving 100 TextBoxes in for 100 loops, I should write:

 TextBox1.Text = Value1; TextBox2.Text = Value2; ... ... TextBox100.Text = Value100; 

A lot more work + all of these 100 event handlers for one additional optional TextBox element.

+4
source share
6 answers

As I mentioned in the commentary on the HatSoft solution, C # Winforms does not allow you to create control arrays similar to the old VB6. The closest I think we can get to what HatSoft and Bert Evans showed in their posts.

One thing that I hope will satisfy your requirement is an event handler, you get a common event handler and in the event handler, when you write out the "sender", you get the control directly, as in VB6

WITH#

 TextBox textBox = sender as TextBox; 

Vb6

 TextBox textBox = TextBox1[i]; 

Thus, the only problem you may encounter is to connect these 100 text fields to one event handler, if you do not create the controls dynamically through the code, but create it manually during development, then all you can offer is group them into a container, for example, say Panel. Then in Form Load, push them all down to one event handler as follows:

 foreach (Control control in myTextBoxPanel.Controls) { if(control is TextBox) control.TextChanged += new EventHandler(control_TextChanged); } 
+4
source

I know I'm a little late for this party, but this solution will work:

Make a global array:

  TextBox[] myTextBox; 

Then in your object constructor after the call

  InitializeComponent(); 

initialize the array:

  myTextBox = new TextBox[] {TextBox1, TextBox2, ... }; 

Now you can iterate over the array of controls:

  for(int i = 0; i < myTextBox.Length; i++) myTextBox[i].Text = "OMG IT WORKS!!!"; 

Hope this helps!

Pete

+5
source

Just create one handler and put all the buttons on it.

 var ButtonHandler = (sender, args) => { var clicked = (Button)sender; if (clicked.Text == "whatever") //do stuff else //do other stuff }; button1.Click += ButtonHandler; button2.Click += ButtonHandler; 

Alternatively, if you create controls in your code, you can use one of the methods mentioned in this answer .

+2
source

Instead of giving 100 TextBoxes in 100 for loop cycles, I need to write:

 for(int i = 0; i <100; i++) { TextBox t = new TextBox(){ Id = "txt_" + i, Value = "txt_" + i}; t.TextChanged += new System.EventHandler(this.textBox_Textchanged); Page.Controls.Add(t); } //and for event on TextChanged private void textBox_Textchanged(object sender, EventArgs e) { TextBox textBox = sender as TextBox; if (textBox != null) { //// } } 
+2
source

If you are working with web forms and not with MVC, you can get a set of controls on the page, as shown in Using ASP controls. NET web page . In essence, a collection of controls is a tree with a page on which the first level of child controls are placed, and some elements have their own children. See How to find web form controls on a page by going through a collection of controls for an example of how to follow a tree.

Also see Practical Guide. Adding controls to an ASP.NET web page with software .

You can use the same event handler for multiple elements if the required signature is the same.

For Windows Forms, this is almost identical, since they are based on similar architectural models, but you will want the Control.Controls Property and How to add controls to Windows Forms .

0
source

One more note: if you really need to edit 100 lines in one form, you probably should consider whether 100 text fields are really the best way to do this. Perhaps best would be to use a ListView, DataGridView, or PropertyGrid.

This applies almost anytime you think you need a huge array of controls.

0
source

All Articles