C# finding sender
Asked Answered
D

3

5
private void buttonCheck(object sender, EventArgs e)
{
   Type x = sender.GetType();
   var y = Activator.CreateInstance(x); //sends me back to the original problem : sender is an object, not a usable object.

   var x = (Button)sender;  // == button, but you never know what sender is until it's already in this function... so 
   dynamic z = sender; //gives you the image of sender i'm looking for, but it's at runtime, so no intellisense/actual compiletime knowledge of what sender is.
}

how do you go about creating a usable instance of sender without prior knowledge of the class sender is actually bringing to this method?

Depolymerize answered 1/1, 2013 at 15:6 Comment(19)
You need to separate out the text of your question from the code, and you need to actually tell us what you're trying to achieve. Please read tinyurl.com/so-hints and tinyurl.com/so-listCnidoblast
How do you expect to know at compile time? Any control can be the sender.Arakawa
ok so i'd have to be editing at runtime then? So what do you do with the object y to make it into an actual control?Depolymerize
Well, it's not quite clear what you're trying to do. However one of your options will be to check what type sender is. E.g.: Button button = sender as Button; if (button != null) { //do something with button } else { //do other stuff }Miguel
@PLB or if (sender is Button)Arakawa
@Arakawa Another option... I have chosen cast because OP uses it in his code. and (Button)sender will throw invalidCastException, so his option will be to use as keyword. BTW, I have no idea, what op wants.Miguel
I don't know it's a button, it was just an example that gives you specific properties.. There are thousands of possiblities it could be any control/class, no way do i use that many ifs.Depolymerize
@Depolymerize Reflection is also an option, but you really need to tell us what the end goal is.Arakawa
one:one relation of property retrieval by casting to an exact replica image of the type sender brings in.. if it's a button it'd be like the var x = (Button)sender, but i don't know if it's a button...Depolymerize
if you use reflection, you get a list and have to loop through properties, i want a direct relation by creating an image of the sender object... like var x = (sender.GetType())sender; --this won't work though.Depolymerize
Let's say the sender is a ListBox, what do you want to do with that ListBox then?Pantin
ok, you're on the right track, i want a replica of it.. usually you do this by creating a new instance, or by casting... so it'd be var x = (ListBox)sender... but there really is no way to tell what sender is before hand.. goin in hot.Depolymerize
a replica so i can operate on it, shatter it, rip properties from it.. anything i want... sender alone doesn't give you those options that i've uncovered anyway.Depolymerize
Your statements are contradictive... "There are thousands of possiblities it could be any control/class" and "a replica so i can operate on it, shatter it, rip properties from it" ... either you want class specific code or you don't.Arakawa
sender IS a specific class... i just don't know what it is at compile time.Depolymerize
head explodes.. so how do you know what you want to do with it?Arakawa
thinking it's now a runtime project. Expression ahoy!Depolymerize
@JonSkeet your tinyurl's are brokenLungi
@FrankPytel: They're now tinyurl.com/stack-hints and tinyurl.com/stack-checklistCnidoblast
G
8

In the vast majority of cases you know what control(s) will be firing the event because you (the programmer) wire them up. For example, if you wire this event up to a button (or even multiple buttons), You know the sender is a Button so you can just cast:

var b = sender as Button;

or

var b = (Button)sender;

either one will give you full intellisense.

If you wire up this event to multiple control types, your best bet is to check each possible type:

if(sender is Button)
//  cast to Button
else if (sender is TextBox)
//  cast to TextBox
else is (sender is CobmoBox)
//  cast to ComboBox

It may seem messy, but since you haven't stated what you actually want to do in the event handler that's the cleanest way in one event to handle multiple possible sender types.

Another option would be to just create multiple event handlers (one for each type) and wire them up to their respective types. I can't think of many code-reuse scenarios between a Button and a TextBox.

Gabriellagabrielle answered 1/1, 2013 at 15:35 Comment(2)
it's not that simple. plus that takes the whole point of an arg out... you're no longer using sender to create something, but rather throwing a rock into a hole.Depolymerize
sender IS something, so it's now only a runtime operation.. so my program will now have to have operations capabilities at runtime it looks no matter how it's done.Depolymerize
D
1

I think dynamic keyword is what you are looking for. At compile time compiler assumes that there is Text property in btn.

  private void button1_Click(object sender, EventArgs e)
  {
     var btn = (dynamic)sender;

     string text = btn.Text;
  }
Desrochers answered 1/1, 2013 at 15:35 Comment(1)
resolved at runtime.. so it's going to have to be a two step process... ok.Depolymerize
M
0

Here's a DataBindingComplete variation that clears the default selection from the DataGridView control. I've got numerous DataGridView controls across multiple tabs and only need this one event handler for all of them which is fantastic. This is based off the response from VladL so all credit should go to them.

    private void Dynamic_DataBindingComplete(Object sender, DataGridViewBindingCompleteEventArgs e)
    {
        var control = (dynamic) sender;
        control.ClearSelection();
    }
Maisey answered 21/7, 2015 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.