Solved

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

Posted on 2014-01-29
6
322 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
[X]
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
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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