2013-10-20 20 views
0

我對使用組合框有一些疑問。使用類組合框

1)我需要從類參考組合框是這樣的:

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

在這裏,我需要從一併檢查該組合框掉落下來,但我不知道怎麼辦。

2)我的實際類型的組合框是「DropDownList」類型。
問題是,如果我放下它並鍵入向上/向下鍵,值將根據選定的行進行更改。如果我然後按下ESC,則最後一個值保持爲選擇不需要的值。

如果我在列表被刪除時按ESC鍵,是否可以從丟棄時刻返回原始值?
如何做到這一點?

這裏是仔細看看我的xCombo子類,以獲得第二個問題幫助...

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 

編輯:
在這裏,我發現在功能上有一個問題,我喜歡被解決。
當組合被刪除,我通過鍵盤瀏覽列表,然後用鼠標按下形成(或在組合外)它關閉列表並設置最後被選擇的值。

取而代之的是,我希望組合鍵設置其新值,只需用鼠標點擊列表或按下鍵盤上的Enter鍵即可。

+0

不,它不在DataGridView中,是獨立的。但我試圖將其子類化以獲得幾個所需的功能。通過事件協調可以。 –

+0

啊!好。抱歉。我會刪除我上面的​​評論。 – varocarbas

+0

嘗試:'如果Me.DroppedDown AndAlso e.KeyCode = Keys.Escape Then' ...我也不完全確定你的一些'ME'引用不應該是'MyBase' ...而不是捕獲文本,我會捕獲selectedindex或selectedItem – Plutonix

回答

1

檢查DroppedDown財產,但它似乎有你想要做的其他事情,而下降。

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 

還請注意,我覺得這三個組合框下拉類型不使用一個/支持DroppedDown財產。

對於您的其餘問題,我不完全遵循,您也可以存儲最後選擇的項目並以類似的方式恢復它。儘管覆蓋Windows的默認行爲很少是一個好主意,因爲您正在創建用戶以前從未遇到過的內容。

編輯

從CLOSE DROPDOWN改變逃生功能中止選擇: 注:我只是用一個std CBO,裁判將需要更改爲MyBase,事件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 
+0

嗨plutonix。我想解決沒有標誌。直接從讀數的組合框讀取狀態。 –

+0

還有一個'DroppedDown'屬性,但你的問題似乎暗示你有其他東西要合併 – Plutonix

+0

DroppedDown會很好。但是如何從課堂上獲得它?我舉例「如果Me.ActiveControl.GetType是GetType(ComboBox)和combodroppeddown = False)然後」所以,如何使用DroppedDown替換combodroppeddown = False? –