Solved

SelectionChanging for a combobox in vb.net

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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