How do you determine which event called another event?

P

pinkfloydfan

Hi there

I have a form with two TextBoxes each of which has an _Enter event.
In each case the _Enter event binds to the same Excel
SheetSelectionChange event.

What I want to know is: within the SheetSelectionChange event how can
I determine which _Enter event fired it and therefore which TextBox I
should be manipulating by this code?

I have tried working off the Focused property of the TextBox but
unfortunately as you shift to Excel this changes.

Thanks for your advice
 
M

Mel Weaver

If I understand you, you can check the ActiveControl or use the sender to
the event.

TextBox tb = Sender as TextBox;
if (tb != null)
MessageBox.show(tb.Name);
 
P

pinkfloydfan

Hi Pete

Sorry for not being clear previously, I don't have the code to hand
just now but basically I have an event (a change in the range selected
on an Excel worksheet) that is trapped by the Enter event of two
separate TextBoxes.

What I want to know is how to determine which of the two Enter events
calls the Excel event.

Does that make it clearer?
 
J

Jeff Gaines

Hi Pete

Sorry for not being clear previously, I don't have the code to hand
just now but basically I have an event (a change in the range selected
on an Excel worksheet) that is trapped by the Enter event of two
separate TextBoxes.

What I want to know is how to determine which of the two Enter events
calls the Excel event.

Does that make it clearer?

I think you are trapping the Enter event of the Text Box, e.g.:

private void txtPath1_Enter(object sender, EventArgs e)
{
}

If you use the same event for both Text Boxes then you can work out which
one called it:
TextBox txtCaller = sender as TextBox.

If you have a separate event for each Text Box then you know which one was
entered.

If you want to use that information eleswhere then you could save it in a
module wide variable.
 
P

Pavel Minaev

pinkfloydfan said:
Hi there

I have a form with two TextBoxes each of which has an _Enter event.
In each case the _Enter event binds to the same Excel
SheetSelectionChange event.

What I want to know is: within the SheetSelectionChange event how can
I determine which _Enter event fired it and therefore which TextBox I
should be manipulating by this code?

To determine which control raised the event that caused the invocation of
your event handler, you can use the "sender" argument of the event handler -
it will be that control.
 
P

pinkfloydfan

Thankyou all for your suggestions.

Unfortunately, the SheetSelectionChange event does not have a sender
argument but I adapted the alternative solution suggested by Jeff and
have a string variable in the Form that is set by each TextBox's Enter
event and records the name of the TextBox when the event fires.

Have a good weekend
 
B

Bob Powell [MVP]

The Sender object in the parameters of an event handler contain the object
that raises the event.

You can determine which of the textboxes sent the message by just comparing
with, say textBox1. (Cast the Sender to a TextBox first)

You can also use other schemes such as using the Tag member of the TextBox
to contain some useful information or if you want to be really clever,
create a custom attribute and use it on your textbox.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

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