views:

67

answers:

2

In VB6 you have the ability to name your controls with an index.

i.e.: cmdStartInterval(1), cmdStartInterval(2), ... .

Then you have a method head like this:

Private Sub cmdStartInterval_Click(Index As Integer)
...
End Sub

Is this also possible in a similary way in C#?

+2  A: 

The equivalent is to use arrays of controls. You will have to add the Index in the event manually. If you don't need the index, you can just assign the events to the controls.

A downside is, both for C# and VB.NET: you cannot create indexed controls with the designer, you have to create them by hand.

The upside is: this gives you more freedom.

EDIT:
This is how that looks:

// in your Form_Init:

Button [] buttons = new Button[10];    // create the array of button controls

for(int i = 0; i < buttons.Length; i++)
{

    buttons[i].Click += new EventHandler(btn_Click);
    buttons[i].Tag = i;               // Tag is an object, not a string as it was in VB6

    // a step often forgotten: add them to the form
    this.Controls.Add(buttons[i]);    // don't forget to give it a location and visibility
}

// the event:
void btn_Click(object sender, EventArgs e)
{
    // do your thing here, use the Tag as index:
    int index = (int) ((Button)sender).Tag;
    throw new NotImplementedException();
}

PS: if you use the form designer, each button will have its own name. If you do as another user suggested, i.e., use the same handler for all of them (as you could do in VB6 too), you cannot easily distinguish the controls by index, as you used to do. To overcome this, simply use the Tag field. It's usually better not to use the Name field for this, as this creates a dependency you don't want.

Abel
I did read your edited answer and ya I think you are right. The cause I marked it was the right answer was the fact, that I still can use the helpful IDE, instead of coding all in the code behind. Is there a way of still using the IDE + your "Array-Approach"?
Rookian
@Rookian: no, not directly. Microsoft's suggestion here would be to use the designer and give your buttons proper names. Create one container (array-like collection component) that you initialize in `form_init`, it'll automatically find your controls if you code it so. Whatever approach you take, giving the objects the same name is not possible anymore, so regardless, it's more work (and, strange as it may seem, gives you more flexibility). Here's [Microsoft's suggestion, plus code](http://msdn.microsoft.com/en-us/library/aa289500%28VS.71%29.aspx).
Abel
The approach I used, is delegating the events in one event, and writing a generic control finder class that is using a naming convention ControlName_ProviderName. I have not only button control that have the same meaning I also have Textboxes, Comboboxes and so on.
Rookian
@Rookian, that's a very scary approach, which can break without the compiler catching it, plus it basically pushes the [Law of Demeter](http://en.wikipedia.org/wiki/Law_of_Demeter) to infinity. Microsoft invented [Attributes](http://msdn.microsoft.com/en-us/library/system.attribute.aspx) for this purpose instead which is type-safe, intellisense aware and errors can be caught at compile time. Or use the straightforward, not perfect tag-approach (each control has a `Tag` property) I mentioned earlier, but that's only slightly better than the scary name-binding.
Abel
+3  A: 
MinhNguyen
this sounds good, I will try this =)
Rookian
That's basically what happened in VB6 too: all buttons that were indexed got one event handle automatically.
Abel
I added a little note for later visitors of this thread to clarify the excellence of the ease, but also the limitations of this answer. Hope you don't mind (you can always switch it back).
Abel