Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2014-01-29
6
318 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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