How can I use the object passed in the event

I

Ivan Sammut

Hi,

I have a form which contains about 100 Textboxes but I mapped all their
onTextchanged event to the same procedure. Now I would like to know if it is
possible from the sender argument passed to the procedure to get to know
which of the 100 textboxes was changed.

My code is :
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
//Here I need to get the name of which texbox was changed
}


private void button1_Click_1(object sender, System.EventArgs e)
{
int iCnt = 0;
for (iCnt=0;iCnt<=100;iCnt++)
{
TextBox iLabel = new TextBox();
iLabel.Parent = panel1;
iLabel.Text = iCnt.ToString();
iLabel.Top = (iCnt * 21);
iLabel.Name = ("Form" + iCnt.ToString());
iLabel.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
iLabel.Refresh();
}
}

Thanks
Ivan
 
J

Jon Skeet [C# MVP]

Ivan said:
I have a form which contains about 100 Textboxes but I mapped all their
onTextchanged event to the same procedure. Now I would like to know if it is
possible from the sender argument passed to the procedure to get to know
which of the 100 textboxes was changed.

Yes - that's the point of the sender argument. Just cast it to TextBox,
and it'll be the one which generated the event.

Jon
 
K

Kevin Spencer

Hi Ivan,

The sender is a reference to the object that raised the event. In this case,
it is always going to be a TextBox. However, you can use the same Event
Handler for various different Controls in a Windows Form. All
System.Windows.Forms.Controls have a Name property. So, I would suggest
casting the sender as a Control to get the name (in case you ever use the
same Event Handler for a different Control):

string name = ((Control)sender).Name;

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 

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

Top