2016-06-29 62 views
0

我有一個數據綁定ListItems組合框與AutoComplete.SuggestAppend ,並希望使用上/下箭頭鍵而不是滾動項目的組合框導航到不同的控件。在離開時取消選擇組合框文本使用Recommendationappend

問題是,如果文本未完成,則建議文本在下一個控件具有焦點時保持高亮顯示。

Link to image example

下面是一些代碼顯示的我在做什麼

Public Class Form1 
Dim PreventCboBoxChanging As Boolean 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    ComboBox1.DataSource = New List(Of String)(New String() {10, 11, 20, 30}) 
    ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems 
    ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend 
End Sub 

Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown 
    If PreventCboBoxChanging = True Then 
     e.Handled = True 
    End If 
    PreventCboBoxChanging = False 
End Sub 

Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown 
    If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then 
     PreventCboBoxChanging = True 
     TextBox1.Select() 
    End If 
End Sub 

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown 
    If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then 
     ComboBox1.Select() 
    End If 
End Sub 

Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating 
    Dim index As Integer = sender.FindString(sender.Text) 
    If index > -1 Then 
     sender.SelectedIndex = index 
    Else 
     e.Cancel = True 
     Me.Text = "" 
     Beep() 
    End If 


End Sub 

末級

有什麼辦法來取消對文本的簡單的例子嗎?

回答

0

這應該使它:

Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave 
    ComboBox1.SelectionLength = 0 
End Sub 
+0

奇怪的是它不起作用。我是新來的,不知道如何正確回覆或發佈代碼。 ComboBox1_Leave >> ComboBox1.Text =「」不會不清除文本框。 – sgrya1

+0

@ sgrya1,我不知道它會有幫助,但請確保您的表單中沒有重複的重疊控件。當用^ C^V複製控件時,它可能會偶然發生......它發生在我身上一次,直到我注意到它,這是一個很大的痛苦,因爲我處理的事件的控件隱藏在控件的精確副本後面,所以我是處理與我想要處理的事件不同的控制。 – VBobCat

1

我發現在另一個線程的解決方案。需要關閉組合框自動完成模式,改變焦點,然後在PreviewKeyDown事件下重新啓用SuggestAppend模式。

Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown 
    If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then 
     ComboBox1.AutoCompleteMode = AutoCompleteMode.None 
     TextBox1.Select() 
     ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend 
    End If 
End Sub 
相關問題