3

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#?

Rookian
  • 19,841
  • 28
  • 110
  • 180
  • I am guessing you are using winforms. Can you confirm? – Oded Jul 23 '10 at 08:58
  • VB6 would always translate into WinForms as its equivalent in .NET, it cannot be used for asp.net, as far as I know (that would be VBScript in ASP). – Abel Jul 23 '10 at 09:01
  • possible duplicate of [What's the simplest .NET equivalent of a VB6 control array?](http://stackoverflow.com/questions/39541/whats-the-simplest-net-equivalent-of-a-vb6-control-array) – raven Jul 23 '10 at 12:25
  • While I agree that the questions are about equal, the answer in that thread does not take indexes or indexed events into account. – Abel Jul 23 '10 at 15:51

2 Answers2

3

In c# you can assign all buttons to 1 event handle

protected void cmdButtons_Click(object sender, System.EventArgs e)

when a button clicked this event was called and instance of this button passed to this event by sender parameter.

Note (added later)
While the above is a valid answer to a subset of the problem and an excellent workaround if the index itself is not needed, it should be noted that it is not an equivalent of indexed controls, as the title suggests, but an alternative. VB6's indexed controls are technically arrays. Hence an equivalent would be an array in C# which can only be reached through code, not via the designer.

Abel
  • 56,041
  • 24
  • 146
  • 247
MinhNguyen
  • 182
  • 2
  • That's basically what happened in VB6 too: all buttons that were indexed got one event handle automatically. – Abel Jul 23 '10 at 09:12
  • 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 Jul 24 '10 at 13:29
2

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
  • 56,041
  • 24
  • 146
  • 247
  • 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 Aug 10 '10 at 08:56
  • @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 Aug 11 '10 at 11:21
  • 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 Aug 11 '10 at 16:14
  • @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 Aug 11 '10 at 21:35