Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > combo box - how to prevent selectedindex change
 

combo box - how to prevent selectedindex change

I need help on this, how can I prevent user from changing the selection of item from the dropdown list? what I am trying acheive is this, when user clicks the down button on the combo box or uses down arrow key, I want to be able track it and based on some logic I want to be able to disallow the user from changing the selected item.

Thank you,

Jesbert

sinkee  Friday, April 07, 2006 12:31 PM
I'd rather populate the combobox withitems that are allowed to be selected. You can perform your logic and populate the combo inthe DropDown event of a ComboBox.
vkh75  Friday, April 07, 2006 12:51 PM

hi vkh75

thank for your suggestion, unfortunately, my logic required that I need to prevent selection change ... what happen is that the selectedValuewas used to link to other controls and do some complex works.when user acciedentally change the selectedindex again ... this complex works would be lost ... so I will like to trap the selectedindex change and offer user the choice of "are you sure you want to lost this works .." and if user reply as "no", then I should prevent the selectedindex to change ... the problem I faced is I can only be notified when selectedindex is changed ... which is too late ..

sinkee  Friday, April 07, 2006 1:18 PM

One more try:

Dim i As Integer

Private Sub cbo_DropDown(ByVal o As Object, ByVal e As System.EventArgs) Handles cbo.DropDown

     i = Me.cbo.SelectedIndex

End Sub

Private Sub cbo_DropDownClosed(ByVal o As Object, ByVal e As System.EventArgs) Handles cbo.DropDownClosed

If MessageBox.Show("Do you want to cancel?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

     Me.cbo.SelectedIndex = i

End If

End Sub

vkh75  Friday, April 07, 2006 1:59 PM

Hi again ... not sure if there is any better way? I use cbo.Enter method instead of dropdown because keydown also can result in changes (without firing the dropdown) ... so at the end, I need cbo.enter to capture the original index, then trap the dropdownclosed and keydown event to prompt for confirmation ... really hope that there is a better way to do it ..

sinkee  Friday, April 07, 2006 2:50 PM

You can use google to search for other answers

Custom Search

More Threads

• How to select multiple rows in datagridview by using datagridviewCheckBox?
• executing SQL query in C#
• dataset xml
• How do I generate typed objects from stored procedures?
• adding controls to child and grand-child grids
• how to update tables data one server to another using vb.net
• Sort by multiple columns in a Datagridview
• random appearance of datagridview coloumn in DataSet (even when visible is set to "False")
• Reg - Building a presentation table for a datagrid
• Little help with DataGridView please