Solved

VB.Net: Differentiating between coding- and user-initatiated action

Posted on 2014-01-29
6
312 Views
Last Modified: 2014-01-30
I have a form in VB.Net, populated with a dropdown control.  I have a "SelectedIndexChanged" event tied to this control; and when the user makes a selection, changing the contents of this control, that triggers code to perform a check on the value they've chosen.  In this context, everything works as it should.  

When the form opens, coding initiates the population of this control with a value it receives from the registry, thus insuring, for the user, a continuity of parameter settings between each execution of the program.  Unfortunately, this first-time, coding-initiated setting of the control value also performs the same check, with undesirable consequences.  Below is a screenshot of the call stack with a coding-triggered check, as evidenced by the gap that saying "[External Code]".  How do I make the triggered code aware of what action triggered it, so as to make intelligent choices of what actions to take, based on this information?  

Note "SelectedIndexChange" following coded change of control value
I feel the question is a very important component of writing professional code, in which event-driven code leverages the context in which it was called to take appropriate action.  Kind regards, ~Peter Ferber
0
Comment
Question by:PeterFrb
  • 3
  • 2
6 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39819554
The simplest way to accomplish this would be to disconnect the event handler, perform your operation, then connect the event handler up again.

RemoveHandler Button1.Click, AddressOf Button1_Click

' do some stuff that might cause the event to fire

AddHandler Button1.Click, AddressOf Button1_Click

Open in new window

0
 
LVL 27

Expert Comment

by:Ark
ID: 39819974
I use a boolean variable
Dim bFromCode As Boolean
'....
bFromCode = True
'some code triggered event
bFromCode = False

Private Sub EventHandler() Handles Whatever.Event
    If bFromCode Then Return
    'rest of code here
End Sub

Open in new window

0
 

Author Closing Comment

by:PeterFrb
ID: 39821973
Woo hoo!  This rocks!  I've been slowly weaning myself from decades of VBA work into VB.Net, and I just elevated my stature as a .Net developer.  In gratitude, ~Peter
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 27

Expert Comment

by:Ark
ID: 39822505
Just to explain my choice of boolean variable - though Add/Remove handler is 'recommended' and 'proper' way, it's OK for simple controls like button or combo with few events. When u'r working with complex control like DataGridView with many events and using a lot of them your code became horrible - each time you refill datagridview (changing datasource for example), you write
RemoveHandler xxxxx
'Repeat above 10+ times for each handler
'     dgv.DataSource=GetData(...)
'AddHandler xxxx
'Repeat above 10+ times for each handler

Open in new window


BTW, I brought this trick from my old VB6 progs where it was the only way :)
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39822565
You only need to remove/add handlers for those you've actually mapped, so even though a control could have a lot of events you don't have to remove/add all of them. You also need to remember when adding a new event to check a boolean value which isn't easily discoverable. Finally, you can make the remove/add operations separate methods so you don't need to write that each time, you just call a method.
0
 
LVL 27

Expert Comment

by:Ark
ID: 39822586
I does using many events in dgv (row/cells begin/end edit,validating,formatting,parsing, editingcontrolshowing,dataerror etc.
And I made my habbit to declare at form level Dim bFromCode as boolean and event handlers just starts with this line: If bFromCode then return
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now