Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.NET
Private MyButton(3) As Button

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    MyButton(0) = Button1
    MyButton(1) = Button2
    MyButton(2) = Button3

    For x = 0 To 2
        AddHandler MyButton(x).Click, AddressOf MyButtonsClicked
    Next

End Sub

Private Sub MyButtonsClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Console.WriteLine(IndexOf MyButton Pressed)

End Sub
Posted

Bad idea. Normally, you should not have the index. You can get the reference to the button by type-casting the object sender to the type System.Windows.Forms.Button. Then you should use this reference.

But I understand if you really need to find out the index of this object in your array MyButton. You may really need it for some calculations. It's quite trivial operation to find it by the reference sender using, say, System.Array.IndexOf:
Array.IndexOf Method (Array, Object) (System)[^].

Of course, it will work, and, for such a small array, even the miserable time complexity of O(N) would not be a problem, from the practical point of view, but it would be the silliest way of doing things in general. A habit to search something with O(N) and a habit of searching of something which does not need to be searched my play an evil trick to you, eventually. You can avoid search. I would, for example, assigned the index of each button to the property Button.Tag, which can be any object. So, you can get this index from type-cast reference to Button taken from the sender in your event handler. The only little problem would be another type-cast operation, in addition to the one in the line above. You can perform this assignment in the same loop you used for adding MyButtonClicked handler to the invocation list of the event for all your instances.

There can be other ways. The biggest problem of your question is that you did not share the purpose of finding the index; if you did, the solution could be even simpler.

—SA
 
Share this answer
 
v2
If you are already using Tag property, you can extend the button class to add a property Index to it. Something like this:

This is C# code. I hope you will be able to convert this to VB.Net. Also, this code is typed right here so there may be some compile errors.

C#
public class CustomButton: Button{

    public int ButtonIndex{
    get;
    set;
    }
}


When you are creating the buttons, use CustomButton class and make sure you assign ButtonIndex property while creation. In the event handler, you can cast the sender as CustomButton object and the index from there.

C#
public void ClickButton(object sender, Eventargs e){
CustomButton button = sender as CustomButton;
int index = button.ButtonIndex;
}
 
Share this answer
 
VB.NET
I can easily cast sender to the type System.Windows.Forms.Button. But isn't the purpose of creating an Array to have the ability to group objects together and access them by their unique Index?

Assume that MyButton .text and .Tag are Dynamic and that there are 25 and not just 3. The only way to reference it in the event handler is by its name? 

I need to be able to pass the index(x) of the button pressed to Subs and functions so that I can reference Mybutton(x)in other parts of the project 
or even use it to change MyLabel(x) or change information in a matching Variable array. In my code the Index is my way of identifying the exact button pressed everywhere by using a single integer.
 
Share this answer
 
I think I'm just going to use the Tabindex property and disable Tabstop, since most objects have this property as an integer anyway.

Thanks Guys
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900