Hello!
I'm trying to add some undo/redo functionality for my app. It deals with many controls, (mostly textboxes, checkboxes and radiobuttons), and I want to write one void for each type. When someone enters to one of them, the Enter event fires, and gets the text, value of the current object.
My problem is that when I can't determine, which object sent the event. E.g.: all textboxes would use the undotextbox event on entering them:

private void undotextbox(object sender, EventArgs e)
{

}

How can I get the one which fired the event? I can get the type via the sender, but I know that fairly well, I'd need the Name property of the objects.

Thanks in advance!

Recommended Answers

All 5 Replies

Yes, that only possible solution is to get specific textbox by name or text!

private void undotextbox(object sender, EventArgs e)
{
if(sender is TextBox)
{
switch( ((TextBox)sender).Name )
{
case "":
....
break;
case "":
....
break;
}
}
}

Similar to Ramey, assuming that the event handler is only assigned to TextBox controls.

private void undotextbox(object sender, EventArgs e)
{
        TextBox textbox = sender as TextBox;
        if(textbox != null)
       {
            string oldValue = textbox.Text;
       }
}

Jerry, I think you mean

//outside event handler
string oldValue = null; 
//inside event handler
textbox.Text = oldValue;
oldValue = textbox.Text;

Hi Ramy,

Actually, since he claims that this event handler is assigned to the Enter event, I made the assumption that this is where he wants to get the original value stored into a var that would be used later to put it back.

Basically the idea I was trying to convey is that when dealing with lots of textboxes, it is better to cast the sender, and deal with that one object rather than take its name and run though a buzillion case statements :)

BTW: Sorry for misspelling your name earlier.

// Jerry

Yes, I understand you. and your answer confused me. Anyway good answer and never mind, you're old friend :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.