Groups | Blog | Home
all groups > vb.net controls > december 2005 >

vb.net controls : How to determine source of SelectedIndexChanged event?


Rob R. Ainscough
12/16/2005 9:48:24 AM
Is there a way for me to look at the Sender or e (System.EventArgs) to
determine the source of the event. Trying to figure out if an event was
triggered by user interaction or via my code (i.e. SelectedIndex = 2).

thanks, Rob.

Rob R. Ainscough
12/16/2005 10:16:16 AM
ListBox

I need a way to determine if an event triggered by my source code or if it
was triggered by some user action.

is this possible? -- I recall seeing how this can be done, but can't
remember where nor how .

Rob

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@tsbradley.net> wrote in
message news:uj3OuumAGHA.532@TK2MSFTNGP15.phx.gbl...
[quoted text, click to view]

Jay B. Harlow [MVP - Outlook]
12/16/2005 12:08:11 PM
Rob,
There are at least 9 base types that have a SelectedIndexChanged event in
..NET, which type are you referring to?

| Is there a way for me to look at the Sender or e (System.EventArgs) to
| determine the source of the event. Trying to figure out if an event was
| triggered by user interaction or via my code (i.e. SelectedIndex = 2).
Generally No, unless the e parameter itself specifically supports it. For
example CloseReason on the System.Windows.Forms.FormClosedEventArgs type.


The "sender" parameter identifies the control and/or object that is sending
the event, useful when you have a single event handler handling the same
event from multiple controls and/or objects. For example all the Change
events on your form are handled by a single ControlChanged handler that sets
a dirty flag.

The "e" parameter carries "additional info", when its a type that derives
from System.EventArgs. System.EventArgs itself does not carry any info, its
common practice to send the same object EventArgs.Empty, for
System.EventArgs itself. To actually carry "additional info" designers need
to inherit from System.EventArgs & add any additional info that may be
needed, such as System.Windows.Forms.MouseEventArgs which carries the X, Y,
Location, Delta, Clicks, and Button information on a mouse event.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


[quoted text, click to view]
| Is there a way for me to look at the Sender or e (System.EventArgs) to
| determine the source of the event. Trying to figure out if an event was
| triggered by user interaction or via my code (i.e. SelectedIndex = 2).
|
| thanks, Rob.
|
|

Jay B. Harlow [MVP - Outlook]
12/16/2005 1:00:41 PM
Rob,
| is this possible? -- I recall seeing how this can be done, but can't
| remember where nor how .
As I stated NO!

At least no direct ways that I know of.

How many places are you setting SelectedIndex directly?

In those places I would consider setting a flag that the event handler
checks to see if you are setting it directly.

Private m_inSetSelectedIndex As Boolean

Public Sub SetSelectedIndex(value As Integer)
m_inSetSelectedIndex = True
Try
SomeControl.SelectedIndex = value
Finally
m_inSetSelectedIndex = False
End Try
End If

Private Sub SomeControl_SelectedIndexChanged(...) Handles...
If m_inSetSelectedIndex Then Exit Sub
...
End Sub

Depending on the "real" requirements on setting SelectedIndex, I would
consider encapsulating the control in a user control & offer an improved
SelectedIndexChanged that has a Reason parameter on it.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


[quoted text, click to view]
| ListBox
|
| I need a way to determine if an event triggered by my source code or if it
| was triggered by some user action.
|
| is this possible? -- I recall seeing how this can be done, but can't
| remember where nor how .
|
| Rob
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@tsbradley.net> wrote in
| message news:uj3OuumAGHA.532@TK2MSFTNGP15.phx.gbl...
| > Rob,
| > There are at least 9 base types that have a SelectedIndexChanged event
in
| > .NET, which type are you referring to?
| >
| > | Is there a way for me to look at the Sender or e (System.EventArgs) to
| > | determine the source of the event. Trying to figure out if an event
was
| > | triggered by user interaction or via my code (i.e. SelectedIndex = 2).
| > Generally No, unless the e parameter itself specifically supports it.
For
| > example CloseReason on the System.Windows.Forms.FormClosedEventArgs
type.
| >
| >
| > The "sender" parameter identifies the control and/or object that is
| > sending
| > the event, useful when you have a single event handler handling the same
| > event from multiple controls and/or objects. For example all the Change
| > events on your form are handled by a single ControlChanged handler that
| > sets
| > a dirty flag.
| >
| > The "e" parameter carries "additional info", when its a type that
derives
| > from System.EventArgs. System.EventArgs itself does not carry any info,
| > its
| > common practice to send the same object EventArgs.Empty, for
| > System.EventArgs itself. To actually carry "additional info" designers
| > need
| > to inherit from System.EventArgs & add any additional info that may be
| > needed, such as System.Windows.Forms.MouseEventArgs which carries the X,
| > Y,
| > Location, Delta, Clicks, and Button information on a mouse event.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
[quoted text, click to view]
| > | Is there a way for me to look at the Sender or e (System.EventArgs) to
| > | determine the source of the event. Trying to figure out if an event
was
| > | triggered by user interaction or via my code (i.e. SelectedIndex = 2).
| > |
| > | thanks, Rob.
| > |
| > |
| >
| >
|
|

AddThis Social Bookmark Button