views:

39

answers:

2

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.

+1  A: 

How do you determine the "current" button?

Was it just clicked? Use the sender argument to the Click event handler (and cast it to Button).

Do you mean the one with focus? Read the ActiveControl property (and cast it to Button).

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:

foreach(Button currentButton in new Button[] { thisButton, thatButton, otherButton, yetAnotherButton })
{
    currentButton.Text = currentButton.Name;
}

or

Action<Button> copyName = (currentButton) => currentButton.Text = currentButton.Name;
copyName(thisButton);
copyName(thatButton);
copyName(otherButton);
copyName(yetAnotherButton);
Ben Voigt
Thanks. I think this is what I'm after. I have just been looking at something to do with collections, and your first example looks like what I want to do.I might even combine it with @Draak's code if I need to handle a variety of controls (not just buttons).Thanks.
Doug
As long as you aren't using any properties that are Button-specific, you can just change `Button` to `Control` and work on all sorts of controls. There's no reason to use reflection when you have a base class.
Ben Voigt
A: 

Use reflection.

Imports System.Reflection

Public Function GetName(b as Button) as String
  Dim t As Type = b.GetType()
  Dim prop As PropertyInfo = t.GetProperty("Name")
  Return prop.GetValue(b, Nothing).ToString()
End Function

In C#, I believe it would like this:

using System.Reflection;

public string GetName(Button b)
{
    Type t = b.GetType();
    PropertyInfo prop = t.GetProperty("Name");
    return prop.GetValue(b, null).ToString();
}
Antony Highsky
Except I don't think Button has a Name property, so I'm guessing you'd use ID or something else that worked.
Antony Highsky
Well there's no indication as to what kind of UI technology he's talking about but if it's WPF or Windows Forms, they both have a Name property. And in both cases there's no need for reflection - just cast the object to type Control (WinForms) or FrameworkElement (WPF).
Josh Einstein
Thanks, I was excited about this answer. Then I realised it is the effectively the same as calling Button.Name property. :( (Which it does have ;)) I'll update the question, with a little more detail.
Doug
It is the same alright. Except that you can pass different controls dynamically as I understood you wanted and not just buttons, and you can do that without knowing the type in advance or doing some long case select. As long as the Name property is present on the control, this code will work. You may want to add an Exception handler though in case the property is not there.
Antony Highsky
The compile-time type of the formal parameter is `Button`, so no you can't pass anything except `Button`s. If you did need to work with a heterogenous mixture of objects without a property defined on a common base, `dynamic` would be the most straightforward approach.
Ben Voigt
Oops.. so, change to object. I guess at that point you just cast to Control as easily though.
Antony Highsky
Cheers, I think the above answer is closer to what I want to do, but I did learn about a couple of new classes from your answer, that might be useful.
Doug