|
我对DataGridView的单元格或列属性中设置CharacterCasing来实现自动转换大小写功能,并调用其TextChange方法。可根本找不到。只好在DataGridView的EditControl中另外添加了KeyPress和TextChange两个事件。并在KeyPress事件中对输入的字符转换大小写。
我想和对TextBox等控件一样在设计时能直接修改CharacterCasing属性,调用KeyPress和TextChange两个事件,应该怎么做?
'以下是程序的代码,窗体中加一个DataGridView控件,一个TextBox控件
Private Sub AddEditControlKeypressHandler(ByVal GrdObject As DataGridView)
GrdObject.ReadOnly = False
If GrdObject.GetCellCount(DataGridViewElementStates.None) = 0 Then
GrdObject.Rows.Add()
End If
GrdObject.CurrentCell = GrdObject.Rows(0).Cells(0)
If GrdObject.Columns(GrdObject.CurrentCell.ColumnIndex).CellTemplate.GetType.Name <> "DataGridViewTextBoxCell" Then
MsgBox(GrdObject.Columns(GrdObject.CurrentCell.ColumnIndex).CellTemplate.GetType.Name)
Exit Sub
End If
InEditMode(GrdObject)
Call AddEvent(GrdObject.EditingControl)
End Sub
Sub AddEvent(ByVal obj As Control)
If obj Is Nothing Then
Exit Sub
End If
AddHandler obj.KeyPress, New KeyPressEventHandler(AddressOf CellEditKeyPress)
AddHandler obj.TextChanged, AddressOf CellTextChanged
End Sub
'转换为大写
Sub CellEditKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'将字符转换为大写
e.KeyChar = UCase(e.KeyChar)
e.Handled = False
End Sub
'在编辑状态下输入数据时,TextBox1中实时显示输入的内容
Sub CellTextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.TextBox1.Text = "您输入的是:" & sender.Text
End Sub
'表单加载时为DataGridView的第一个单元格添加事件
'这里有个问题,为什么只需要给第一个单元格添加事件后每个单元格就都能执行这个事件?请大家帮忙解疑一下。
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call AddEditControlKeypressHandler(Me.DataGridView1)
End Sub
'点击单元格时直接进入编辑状态
Private Sub DataGridView1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Call InEditMode(Me.DataGridView1)
End Sub
''' <summary>
''' 指定具有焦点并已被选为活动的单元格进入编辑状态
''' </summary>
''' <remarks></remarks>
Sub InEditMode(ByVal GrdObj As DataGridView)
Dim oldMode As DataGridViewEditMode = GrdObj.EditMode
GrdObj.EditMode = DataGridViewEditMode.EditProgrammatically
GrdObj.BeginEdit(True)
GrdObj.EditMode = oldMode
End Sub |
|