Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SelectionChanging for a combobox in vb.net

Posted on 2012-03-14
4
Medium Priority
?
181 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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