Solved

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

Posted on 2014-01-29
6
328 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

627 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