none
c转vb.net RRS feed

  • 问题

  • 我只会vb.net,网上找了一段。用网页转成vb.net后有错误,请帮忙转一下!谢谢!

    代码的意思是控制datagridview单元格只能输入正数,源码如下:

    Private Sub DataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
    If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
    Dim dgv As DataGridView = DirectCast(sender, DataGridView)
    Dim tb As DataGridViewTextBoxEditingControl = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
    tb.KeyPress -= New KeyPressEventHandler(AddressOf dataGridViewTextBox_KeyPress)
    If dgv.CurrentCell.OwningColumn.Name = "" Then
    tb.KeyPress += New KeyPressEventHandler(AddressOf dataGridViewTextBox_KeyPress)
    End If
    End If
    End Sub

    Private Shared Sub dataGridViewTextBox_KeyPress(sender As Object, e As KeyPressEventArgs)
    '这里还可加长度判断
    If (e.KeyChar >= DirectCast(Keys.D0, [Char]) AndAlso e.KeyChar <= DirectCast(Keys.D9, [Char])) OrElse e.KeyChar = DirectCast(Keys.Delete, [Char]) OrElse e.KeyChar = DirectCast(Keys.Back, [Char]) Then
    e.Handled = False
    Else
    e.Handled = True
    End If
    End Sub


    '=======================================================
    'Service provided by Telerik (www.telerik.com)
    'Conversion powered by NRefactory.
    'Twitter: @telerik
    'Facebook: facebook.com/telerik
    '=======================================================

    翻译后的如下:

    Private Sub DataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
    	If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
    		Dim dgv As DataGridView = DirectCast(sender, DataGridView)
    		Dim tb As DataGridViewTextBoxEditingControl = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
    		tb.KeyPress -= New KeyPressEventHandler(AddressOf dataGridViewTextBox_KeyPress)
    		If dgv.CurrentCell.OwningColumn.Name = "" Then
    			tb.KeyPress += New KeyPressEventHandler(AddressOf dataGridViewTextBox_KeyPress)
    		End If
    	End If
    End Sub
    
    Private Shared Sub dataGridViewTextBox_KeyPress(sender As Object, e As KeyPressEventArgs)
    	'这里还可加长度判断
    	If (e.KeyChar >= DirectCast(Keys.D0, [Char]) AndAlso e.KeyChar <= DirectCast(Keys.D9, [Char])) OrElse e.KeyChar = DirectCast(Keys.Delete, [Char]) OrElse e.KeyChar = DirectCast(Keys.Back, [Char]) Then
    		e.Handled = False
    	Else
    		e.Handled = True
    	End If
    End Sub
    
    '=======================================================
    'Service provided by Telerik (www.telerik.com)
    'Conversion powered by NRefactory.
    'Twitter: @telerik
    'Facebook: facebook.com/telerik
    '=======================================================
    


    Work8862

    2016年4月24日 1:58

答案

全部回复

  • 参考下面的连接

    http://www.cnblogs.com/colder/p/3517798.html

    或是

    http://blog.csdn.net/suncherrydream/article/details/19986279


    专注于.NET MIS开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro)+ Infragistics WinForms


    2016年4月25日 0:16
  • 谢谢!是我想要的东西了,但是我只想控制第2,3,4列的单元格,请问该怎么改一下代码?

    下面是放在我的程序里的代码,好用,只是整个datagridview单元格都只能输入数字了

        ' 声明 一个 CellEdit
        Public dgvTxt As DataGridViewTextBoxEditingControl = Nothing
    
        Private Sub DoorWindowSchedule_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DoorWindowSchedule.EditingControlShowing
    
            ' 赋值  
            dgvTxt = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
            dgvTxt.SelectAll()
    
            ' 绑定到事件   
            AddHandler dgvTxt.KeyPress, AddressOf Cells_KeyPress
    
        End Sub
    
        ' 自定义事件KeyPress事件
        Private Sub Cells_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    
            keyPressXS(e, dgvTxt)
        End Sub
    
    
        Public Shared Sub keyPressXS(ByVal e As KeyPressEventArgs, ByVal dgvTxt As DataGridViewTextBoxEditingControl)
            If Char.IsNumber(e.KeyChar) OrElse Asc(e.KeyChar) = 8 Then
                e.Handled = False
                '让操作生效
                Dim j As Integer = 0
                Dim k As Integer = 0
                Dim flag As Boolean = False
                If dgvTxt.Text.Length = 0 Then
                    If e.KeyChar = "."c Then
                        '让操作失效
                        e.Handled = True
                    End If
                End If
                For i As Integer = 0 To dgvTxt.Text.Length - 1
                    If dgvTxt.Text(i) = "."c Then
                        j += 1
                        flag = True
                    End If
                    If flag Then
                        If Char.IsNumber(dgvTxt.Text(i)) AndAlso Asc(e.KeyChar) <> 8 Then
                            k += 1
                        End If
                    End If
                    If j >= 1 Then
                        If e.KeyChar = "."c Then
                            '让操作失效
                            e.Handled = True
                        End If
                    End If
                    If k = 2 Then
                        If Char.IsNumber(dgvTxt.Text(i)) AndAlso Asc(e.KeyChar) <> 8 Then
                            If dgvTxt.Text.Length - dgvTxt.SelectionStart < 3 Then
                                If dgvTxt.SelectedText <> dgvTxt.Text Then
                                    '/让操作失效
                                    e.Handled = True
                                End If
                            End If
                        End If
                    End If
                Next
            Else
                e.Handled = True
            End If
        End Sub


    Work8862

    2016年4月25日 12:02
  • 可以参考下这个  Only Allow to enter Numeric values Datagridview specific column

    http://stackoverflow.com/questions/17017864/only-allow-to-enter-numeric-values-datagridview-specific-column

    关键的代码是

    private void dataGridView1_EditingControlShowing(object sender, 
        DataGridViewEditingControlShowingEventArgs e)    
    {
    String sCellName =  dataGridView1.Columns(e.ColumnIndex).Name; 
        If (UCase(sCellName) == "QUANTITY") //----change with yours
        {
    
            e.Control.KeyPress  += new EventHandler(CheckKey);
    
         }
    }
    
    private void CheckKey(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) 
            && !char.IsDigit(e.KeyChar) 
            && e.KeyChar != '.')
        {
            e.Handled = true;
        }   
    }
    


    专注于.NET MIS开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年4月26日 0:09