首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按下只有字符"abcde“的数据框并转换为大写。

按下只有字符"abcde“的数据框并转换为大写。
EN

Stack Overflow用户
提问于 2016-04-21 12:00:05
回答 3查看 606关注 0票数 2

如何限制keypress上只有datagridview字符abcde和转换为大写在vb.net?

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-21 12:10:31

更新:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2016-04-21 14:57:49

另一个解决方案,我试着用这个,它的工作

代码语言:javascript
复制
    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
EN

Stack Overflow用户

发布于 2017-11-15 05:51:18

尝尝这个

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36768947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档