2014-06-29 18 views
0

我有一個編輯控件Inherits myTextBox這個Implements IDataGridViewEditingControlVB.Net檢測datagridview編輯控件上的SHIFT鍵

我知道如果編輯控件處理鍵事件,它應該在Function EditingControlWantsInputKey之內,因此我添加了下面的代碼。

Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey 
     If key = Keys.ShiftKey Then 
      Return True 
     End If 
End Function 

與該Keys.ShiftKey相關的鍵碼是16,但我用一個MsgBox(鍵)找出鍵碼&它打印出65552.我想檢測shift鍵允許用戶hightlight文本在使用SHIFT + ARROW_RIGHT/LEFT的文本框內。當我按下SHIFT + RIGHT/LEFT時,由於沒有檢測到SHIFT鍵,光標移動到下一個單元。

回答

2

keydata/key參數是按下修飾鍵的任意或全部的按位值(Ctrl + A,Shift + Left,Shift + Control + Right等)。值65552表示Keys.Shift爲真(即正在按下Shift鍵)並修改輸入。

對於Shift +向左,則該值將是65573(65536 Or 37):

If (keydata And Keys.Shift) = Keys.Shift AndAlso 
     (keydata And keys.Left) = Keys.Left Then  ' ToDo: add Keys.Right 

    Return True 
End If 
Return False ' MSDN: Return Not dataGridViewWantsInputKey 

Keys.ShiftKey是同時Keys.Shift(65536)的鍵(16)是用於移位或控制或者Alt改性劑。

+0

好吧,現在我的代碼看起來像這樣,這是正確的嗎? 如果鍵= Keys.Right OrElse鍵= Keys.Left OrElse(鍵和Keys.Shift)= Keys.Shift然後返回True – codeGEN

+0

爲什麼你返回false而不是返回不dataGridViewWantsInputKey – codeGEN

+1

要陷阱,它應該是:*如果(Shift子句)AndAlso((左子句)或(右子句))= Then *也意味着評估其餘的如果爲假,那麼你想要Shift和(左或右)。 '不dataGridViewWantsInputKey'可能是正確的,我沒有DGV方便測試,我認爲他們會是相同的。 – Plutonix