I want to be able to click on a button, and set a string variable to be whatever the current button's text says.
For example.... if you have 2 buttons, one that says "Dog", and one that says "Cat", for example.... how can I make it so when the button is pushed, a string variable becomes the value of the TEXT of the button pushed?
I think my brain may be missing a key component of how object oriented programming works.
'Me' keyword returns the current instance of the class where you use it, so it can be used inside any class. I don't think this is what you're looking for, I will give a practical example that I think will lead to what you actually are looking for.
Let's say you have DogButton in a form, when you doubleclick it in Designer you get this code:
VB.NET:
Public Class TheForm
Private Sub DogButton_Clicked(sender As Object, e As EventArgs) Handles DogButton.Click
End Sub
End Class
If you use the Me keyword here is refers to current instance of TheForm, because you use it in TheForm class. The reference to the DogButton would be Me.DogButton. Similar you could doubleclick the CatButton and get an event handler for that button where you referenced the Me.CatButton. This is most common when you have event handlers that do different things. The following example will show how you can use the same event handler for multiple class instances.
All events signatures in .Net follows the same parameters pattern, and this is very useful: the 'sender' parameter is a reference to the class instance that raised the event, in above example 'sender' would always be a reference to DogButton because that event handler only handles the Click event for that button. The event handler may handle the events of many class instances, also for different events as long as they have the same signature. So you could modify it like this:
VB.NET:
Private Sub AnimalButtons_Clicked(sender As Object, e As EventArgs) Handles DogButton.Click, CatButton.Click
You can write this manually, but IDE also supports doing this from Designer. Now this event handles the Click event of both these buttons, and you can actively use the sender parameter in code to do different things depending on which button was actually clicked. You see the type of the 'sender' parameter is Object, this is because this code pattern is used for any class types. It still reference the specific class object, in this case the Button class. To see these objects as their actual type in code you can cast to the right type using CType or DirectCast, for example:
VB.NET:
Dim b As Button = CType(sender, Button)
Here b variable would either reference the CatButton or the DogButton when one of them was clicked.
This below would be equally valid in this case, since Button class inherits Control class:
VB.NET:
Dim c As Control = CType(sender, Control)
The Text property is also something that Button class inherits from Control class, so you would get the same result for b.Text and c.Text here.
You can loop through the controls using Container.Controls collection. If the container is Form, then use the below code; otherwise replace 'Me' with the containers name.
VB.NET:
For Each ctl In Me.Controls
If TypeOf ctl Is Button Then
If Not ctl.Equals(sender) Then 'Returns True, if the control is equal to the sender.
DirectCast(ctl, Button).BackColor = Color.White
Else
DirectCast(ctl, Button).BackColor = Color.Yellow
End If
End If
Next
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.