Find control name though object sender?

  • Thread starter Thread starter VMI
  • Start date Start date
V

VMI

In my Windows Form, is it possible to get the control name through object
sender in an event handler? For example, in private void
dataGridView_zip_KeyPress(object sender, KeyPressEventArgs e), how can I
know that the sender is "dataGridView_zip" ?

Thanks.
VS2005 2.0
 
VMI,

If by control name, you mean the value of the Name property on the
control, then yes, it is, you just have to cast the sender parameter to a
type of Control, and then access the Name property.

However, this does not have to correspond to the name of the field or
variable that references the control. For dynamically created controls, the
name will not correspond to any field/variable you have. If you used the
designer to lay out the control, then it should have the same name as the
field it is assigned to in your containing class.

In general, you can not get the name of a variable/field that is
referencing a control (since multiple fields/variables can reference the
same control, and you wouldn't know what to do with the names of the
variables at that point, since the variables/fields could concievably have
the same name).

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top