2016-04-21 50 views
2

如何在datagridview唯一字符限制keypress ABCDE並轉換爲vb.net爲大寫?按鍵上datagridview的唯一字符「ABCDE」,並轉換爲大寫

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing  
     If DataGridView1.CurrentCell.ColumnIndex = 3 Then 
       AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBoxabcde_keyPress 
     End If 
End Sub 

Private Sub TextBoxabcde_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) 
     If Not Char.IsControl(e.KeyChar) And Not Char.IsLetter(e.KeyChar) And e.KeyChar <> "." Then 
       e.Handled = True 
     End If 
End Sub 

回答

1

更新時間:

Private Sub DataGridView1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress 
    Dim allLetters As String = "abcde" 
    If Not allLetters.Contains(e.KeyChar.ToString.ToLower) Then 
     e.KeyChar = ChrW(0) 
     e.Handled = True 
    End If 
End Sub 

Private Sub dataGridView1_CellFormatting(sender As Object, e As 
DataGridViewCellFormattingEventArgs) 
    If e.Value IsNot Nothing Then 
     e.Value = e.Value.ToString().ToUpper() 
     e.FormattingApplied = True 
    End If 
End Sub 
+0

我在e.KeyChar = UCase(e.KeyChar)中出現錯誤,但按鍵限制僅適用於字符abcde –

+0

究竟錯誤說的是什麼? – Claudius

+0

@FendyPlick現在嘗試 – Claudius

0

另一種解決方案,即時通訊嘗試與此,它的工作

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing  
      If DataGridView1.CurrentCell.ColumnIndex = 3 Then 
        DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper 
        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBoxabcde_keyPress 
      End If 
    End Sub 

    Private Sub TextBoxabcde_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) 
      If Not (Asc(e.KeyChar) = 8) Then 
      Dim allowedChars As String = "ABCDE" 
         If Not allowedChars.Contains(e.KeyChar.ToString.ToUpper) Then 
           e.Handled = True 
         End If 
      End If 
    End Sub 

感謝您的幫助克勞

+0

我會編輯你的問題,併發布在那裏。只是不要刪除你的問題,以便其他人可以學習 – Claudius

0

試試這個

Dim txtEC As DataGridViewTextBoxEditingControl = Nothing 
Private Sub DGV_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DGV.EditingControlShowing 

If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then 
    If DGV.CurrentCell.ColumnIndex =1 Then 
     txtEC = DirectCast(e.Control, DataGridViewTextBoxEditingControl) 
     txtEC.CharacterCasing = CharacterCasing.Upper 
    End If 
End If 
End Sub 

Private Sub DGV_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellEndEdit 

If txtEC IsNot Nothing Then 
     txtEC.CharacterCasing = CharacterCasing.Normal 
     txtEC = Nothing 
End If 
End Sub 
相關問題