C # equivalent of WindowsForms for VB6 indexed controls

In VB6, you can name your index controls.

ie: cmdStartInterval (1), cmdStartInterval (2), ....

Then you have a method that looks like this:

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

Is this possible in a similar way in C #?

+3
source share
2 answers

In C #, you can assign all buttons to one event descriptor

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

when the button with this event was pressed, and an instance of this button is passed to this event by the sender parameter.

( )
, , , , , . VB6 . , #, , .

+3

- . . , .

#, VB.NET: , .

: .

EDIT:
:

// 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: , . , , .. ( VB6), , . , "". "" , , .

+2

Source: https://habr.com/ru/post/1756148/


All Articles