Wine Too
Wine Too

Reputation: 4655

Using combobox from class

I have some questions on using of combobox.

1) I need to reference combobox from class like this:

If Me.ActiveControl.GetType Is GetType(ComboBox) And combodroppeddown = False) Then
   something...
End If

From Here I need right from the AND to check if this combobox is dropped down but I don't know how.

2) My actual type of combobox is of "DropDownList" type.
Problem is that if I drop it down and type with up/down keys the value is changed according to selected row. If I then press ESC then last value stays as choosen what is not wanted.

Is here way to return original value from moment of dropping in case if I press ESC when list is dropped?
How to do that?

Here is closer look to my xCombo subclass to get help in second question...

Public Class xAutoCombo
Inherits ComboBox

Private entertext As String

Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)

    If e.Control Then ' this dropped a list with keyboard
        If e.KeyCode = Keys.Down Then
            Me.DroppedDown = True
        End If
    End If

    '' this should return value back if ESC was pressed
    '' but don't work!
    If Me.DroppedDown And e.KeyCode = Keys.Escape Then 
        Me.Text = entertext
    End If

    MyBase.OnKeyDown(e)
End Sub

Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs)

    entertext = Me.Text '' this remember text in moment of droping
    MyBase.OnDropDown(e)
End Sub

EDIT:
Here I found one issue in functionality which I like to be solved.
When combo is dropped and I navigate through list by keyboard and then press with mouse to form (or outside of combo) it closes a list and set value which is last been selected.

Instead of that I would like that combo set his new value ONLY on click with mouse to list or with pressing Enter key with keyboard.

Upvotes: 0

Views: 347

Answers (1)

Examine the DroppedDown property, but it seemed like you have other things you wanted to do while dropped down.

Dim cbo As ComboBox
If Me.ActiveControl.GetType Is GetType(ComboBox) then
    cbo=Ctype(Me.ActiveControl, ComboBox)

    ' make it easier to refernece

    if cbo.DroppedDOwn then
        ....
    End iF
End if

' Note:
Ctype(Me.ActiveControl, ComboBox).DroppedDown
' should work, but the above is easier to read and use since you apparently 
' will have other things to do/override with it

Note also that I think one of the three combobox dropdown types does not use/support the DroppedDown property.

For the rest of your question, which I dont entirely follow, you could also store the last selected item and restore it in similar fashion. Overriding windows default behavior though, is rarely a good idea because you are creating something the user has never encountered before.

EDIT

To change Escape functionality from CLOSE DROPDOWN to ABORT CHOICE: NOTE: I just used a std cbo, refs will need to be changed to MyBase, events to On...

Private selectedindex As Integer = -1
Private bEsc As Boolean = False

Private Sub cbo_Enter(....
   ' reset tracking vars...might not be needed
    selectedindex = -1
    bEsc  = False
End Sub

Private Sub cbo_DropDown(...
    ' capture starting state
    selectedindex = cbo.SelectedIndex
    bEsc = False
End Sub

KeyDown:

    If cbo.DroppedDown AndAlso e.KeyCode = Keys.Escape Then
        bEsc = True
        e.Handled = True
        ' this MUST be last!
        cbo.DroppedDown = False
    End If


 Private Sub cbo_DropDownClosed(...
    ' rest cbo.selectedindex if escape was pressed
    If bEsc Then
       ' note: SelectedIndexChanged event will fire and any code there will run
       ' may need to qualify with If Esc = False...
        cbo.SelectedIndex = selectedindex
    End If
End Sub

Upvotes: 1

Related Questions