Solved

SelectionChanging for a combobox in vb.net

Posted on 2012-03-14
4
162 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
  • 2
4 Comments
 
LVL 39

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 39

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now