Solved

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

Posted on 2014-01-29
6
320 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 28

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

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 28

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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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