Solved

SelectionChanging for a combobox in vb.net

Posted on 2012-03-14
4
176 Views
Last Modified: 2012-06-27
Hi

Is it possible to have a "SelectionChanging" event (rather than the SelecionChanged)?

The reason I ask is that I would find it extremely useful to prevent the selection change if certain criteria in the form weren't satisfied.

Note that I don't want to disable the combo to get round the problem.

Thanks in advance

John
0
Comment
Question by:IssacJones
[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
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37721940
On your selection changed,

use the following pseudo_code:


if CriteriaMet then
   cmb.Tag = cmb.SelectedValue
else
   WarnUser()
   cmb.SelectedValue = cmb.Tag
end if
0
 

Author Comment

by:IssacJones
ID: 37722059
Interesting. But won't result in recursive calls to selectionchanged?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37722089
I suppose the only way to achieve that is by having a custom method added to an inherited combobox. I have not tested this but the logic seems good.
Public Class xCombo
    Inherits ComboBox
    Private _ValidatingEventRaised As Boolean = False
    Protected Overrides Sub OnValidating(e As System.ComponentModel.CancelEventArgs)
        If Me.SelectedValue = "YourTest" Then
            _ValidatingEventRaised = True
            MyBase.OnValidating(e)
        End If
    End Sub
    Protected Overrides Sub OnValidated(e As System.EventArgs)
        If Not _ValidatingEventRaised Then Return
        MyBase.OnValidated(e)
    End Sub
End Class

Open in new window

0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 37722134
But won't result in recursive calls to selectionchanged?  

you can get around that by removing the handler before setting the value back:

RemoveHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
cmb.SelectedValue = cmb.Tag
AddHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged


But the call is negligble as it's only going to set it back to what it was already.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
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…

630 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