net中的restirct数值

net中的restirct数值

本文介绍了datagrid视图vb.net中的restirct数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请帮我限制datagrid列中的数值。



显示以下错误。



错误1'公共事件KeyPress(发送者为对象,e为System.Windows.Forms.KeyPressEventArgs)'是一个事件,不能直接调用。使用'RaiseEvent'语句来引发事件。



***********源代码********************* *





Hi all,
Please help me to restricct the numeric value in datagrid column.

it shows following error.

Error1'Public Event KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

***********source code**********************


Private Sub datagridrequest_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles datagridrequest.EditingControlShowing
        If TypeOf e.Control Is TextBox Then

            Dim tb As TextBox = TryCast(e.Control, TextBox)
            tb.KeyPress -= New KeyPressEventHandler(AddressOf tb_KeyPress)
            If Me.datagridrequest.CurrentCell.ColumnIndex = 0 Then

                tb.KeyPress += New KeyPressEventHandler(AddressOf tb_KeyPress)
            End If
        End If
    End Sub
    Private Sub tb_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        If Not (Char.IsDigit(e.KeyChar)) Then
            If e.KeyChar <> ControlChars.Back Then
                'allow the backspace key

                e.Handled = True
            End If
        End If

    End Sub

推荐答案

AddHandler tb.KeyPress, AddressOf tb_KeyPress



是VB中唯一正确的语法。


is the only correct syntax in VB.


Private Sub dgvItemService_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvItemService.EditingControlShowing
        If TypeOf e.Control Is TextBox Then
            Dim tb As TextBox = TryCast(e.Control, TextBox)
            If Me.dgvItemService.CurrentCell.ColumnIndex = 2 Then
                AddHandler tb.KeyPress, AddressOf tb_KeyPress
            End If
        End If
    End Sub







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





只需使用您自己的DataGridview名称更改dgvItemService。



Just Change the dgvItemService with your own DataGridview Name.


这篇关于datagrid视图vb.net中的restirct数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 06:17