C. Carrasquinho C. Carrasquinho - 1 year ago 115
C# Question

C# Get the name of an object without knowing it's Type

private void function(object sender, EventArgs e)

Is it possible to get the Name, without using a variable?

if(sender.getType().Name == "Button"){
Button btn = sender as Button;
ObjName = btn.Name
if(sender.getType().Name == "CheckBox"){
CheckBox cbx= sender as CheckBox;
ObjName = cbx.Name

I have a function that works with a lot of different object types, and the only thing i need is the Name.


Answer Source

just cast it to its base type Control

var name = ((Control)sender).Name
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download