我要做的是,如果我键入" red"进入一个单元格,然后单元格的背景颜色变为红色,如果我键入"蓝色"进入一个细胞,然后细胞的背景颜色变为蓝色。我遇到的问题是,如果我键入"蓝色",只有" blu"记录为mystring,因为textchanged事件记录更改前的单元格值,因此单元格的背景颜色不会因此而改变颜色。我希望有人可能知道如何在文本更改后获取当前单元格值。我目前的代码如下(这只是一个例子)。任何想法或帮助将不胜感激,谢谢。
Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
Me.DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
Dim EditingControl As DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
EditingControl = e.Control
AddHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged
End Sub
Private Sub EditingControl_TextChanged(sender As Object, e As EventArgs)
mystring = Me.DataGridView1.CurrentCell.Value
If mystring = "red" Then
EditingControl.BackColor = Color.Red
ElseIf mystring = "blue" Then
EditingControl.BackColor = Color.Blue
Else
EditingControl.BackColor = Color.White
End If
End Sub