Without going into detail about what I’m trying to achieve, is it possible (and if so how) can I reference an object that I don’t know the name of?
For example I want to saything like:
someButton.Text = someButton.Name;
But on the right side I don’t want to state the object name. So what I’d really like to do is something like:
someButton.Text = currentButton.Name;
otherButton.Text = currentButton.Name;
Essentially I want to dynamically assign the control text at runtime depending on a bunch of other stuff. Hopefully if I can do this I can just put decision making in a single method and reuse this.
Update: (I’m using Windows Forms.)
I was hoping there was some way for the program to know what control I was talking about (the current one) a bit like the this reference. So I didn’t have to keep passing in the specific object, if I’m going to do that I could probably just pass the string through. I was hoping to paste the same line of code calling the method every time I wanted it. I suspect I’m just being lazy… and what I want to do is ridiculous.
And there’s no associated event for the control.
I reckon the answer might be to customise the controls. (Not just buttons, a variety of GUI controls). Which is going to take A LOT more effort than my alternative.
How do you determine the “current” button?
Was it just clicked? Use the
senderargument to theClickevent handler (and cast it toButton).Do you mean the one with focus? Read the
ActiveControlproperty (and cast it toButton).EDIT: Wait, by current button you mean the one on the left hand side of the assignment statement? If you want to perform some operation of a bunch of different buttons you can definitely avoid naming each button multiple times. Here’s an example:
or