Solved

SelectionChanging for a combobox in vb.net

Posted on 2012-03-14
4
166 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 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
start a process from a service 3 36
Help with converting xml file to excel using VB.NET 3 30
Open a link in vb.net 2 25
vb.net datagrid point 4 16
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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