2014-11-06 51 views
0

我有一個ComboBox「我命名爲Combo」,它在第二列中加載到C1FlexGrid「我命名爲fgMain」中,並且我想要處理鍵盤以按下輸入這個組合框可供用戶使用。在C1FlexGrid中處理ComboBox列的KeyDown事件

我使用此代碼來創建組合框,並用數據填充它:

fgMain.Cols(2).Editor = Combo  
Combo.DrawMode = DrawMode.Normal  
Dim dap As New OleDbDataAdapter()  
     Dim dat As New DataTable() 
     dap.SelectCommand = New OleDbCommand("SELECT EmpInfo.ManualId, EmpInfo.Name FROM EmpInfo;", conn) 
     dap.Fill(dat) 
     Combo.DataSource = dat 
     Combo.DisplayMember = "Name" 
     Combo.ValueMember = "ManualId" 

我用這個代碼來處理按下任​​何單元格中輸入列2 c1flexgrid它只能處理1回車壓上的任何出現在第2列單元格,並在下拉組合框的列表:

Private Sub fgMain_KeyDown(sender As Object, e As KeyEventArgs) Handles fgMain.KeyDown  
    Select Case e.KeyCode  
     Case Keys.Enter  
      Select Case fgMain.Selection.c1  
       Case 2  
        fgMain.StartEditing(fgMain.Selection.BottomRow, 2)  
        e.SuppressKeyPress = True  
        e.Handled = True  
      End Select 
    End Select  

現在我需要處理,如果用戶選擇他想要的項目,然後他會按第二次進入...我的問題是,當我按第二個進入選擇選擇的項目,它出現我n「fgMain(e.row,2)」只有不到一秒鐘,那麼整行消失,並且fgMain(e.row,2)變空

回答

0

看起來像是因爲您使用的是KeyPress事件FlexGrid,而您希望ComboBox處理KeyPress事件。您可以使用組合框的事件,或者,如果您需要一些網格參考,請使用KeyPressEdit事件。 KeyPressEdit事件對應於System.Windows.Forms.Control.KeyPress事件,除非在網格處於編輯模式時觸發(在這種情況下,接收密鑰的控件是編輯器,而不是網格本身)。

請參考下面的代碼片段:

Private Sub fgM_KyDwnEdt(ByVal sender As Object, ByVal e As KeyEditEventArgs) Handles fgMain.KeyDownEdit 
    Select Case e.KeyCode 
     Case Keys.Enter 
      Select Case fgMain.Selection.c1 
       Case 2 
        fgMain.StartEditing(fgMain.Selection.BottomRow, 2) 
        e.Handled = True 
      End Select 
    End Select 
End Sub 
+0

感謝Vishwakarma先生的幫助。但我的問題是不開始編輯'fgMain.StartEditing(fgMain.Selection.BottomRow,2)'。我的問題是讓選定的項目停留在'fgMain(fgMain.Selection.BottomRow,2)'後第二次按下確認選擇這個項目。我想要處理按下輸入'fgMain_KeyDownEdit'接收數據不進入編輯模式原因KeyDownEdit事件在單元格已處於編輯模式時觸發「據我所知」 – 2014-11-08 08:57:02

+0

我正在使用[KeyDown](http://helpcentral.componentone.com/nethelp/c1flexgrid/#C1.Win.C1FlexGrid.4~C1.Win .C1FlexGrid.C1FlexGrid〜KeyDown_EV.html)事件不是[按鍵響應(http://helpcentral.componentone.com/nethelp/c1flexgrid/#C1.Win.C1FlexGrid.4~C1.Win.C1FlexGrid.C1FlexGridBase~KeyPress_EV.html)事件 – 2014-11-09 10:44:07

+0

如果你可以使用keypressedit而不是keypress,或者使用keydownedit而不是keydown,你會看到我試圖解釋的區別 – 2014-11-10 11:27:54

0

我的領隊告訴我,這不是好辦法,但一個組合框到C1FlexGrid。你可以用這種方式

Dim tmpStyle As CellStyle 
Dim MyCmd As New OleDbCommand 
Dim DbReader As OleDbDataReader 
MyCmd.Connection = conn 
MyCmd.CommandText = "SELECT EmpInfo.ManualId, EmpInfo.Name FROM EmpInfo;" 
DbReader = MyCmd.ExecuteReader 
dtMap.Clear() 
Do While DbReader.Read 
    dtMap.Add(DbReader("ManualId"), DbReader("Name")) 
Loop 
tmpStyle = fgMain.Styles.Add("Name") 
tmpStyle.DataType = GetType(Integer) 
tmpStyle.DataMap = dtMap 
fgMain.Cols(2).Style = tmpStyle 
DbReader.Close() 
MyCmd.Dispose() 

你也不需要KeyPress事件或KeyPressEdit事件來處理輸入壓。你只需要處理AfterEdit事件來處理編輯你的組合框中的任何單元格後發生的事情。希望這足以幫助任何人需要將組合框放入C1FlexGrid中

相關問題