变量看作System.EventArgs,似乎是KeyPressEventArgs

时间:2012-06-11 17:23:23

标签: vb.net richtextbox

Private Sub RichTextView_TextChanged(ByVal sender As Object, ByVal e As 
   KeyPressEventArgs) Handles RichTextView.KeyPress, RichTextView.TextChanged

    Dim c As Char = e.KeyChar
    Dim i As Integer = Asc(c)
    Dim h As Char = Chr(i)

capitalise_first_letter.Add(h)
End Sub

上面的代码产生错误:Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.KeyPressEventArgs'.它会在capitalise_first_letter.Add(h)处抛出错误(capitalise_first_letter是一个字符串列表)。

为什么呢?既然h是e.KeyChar通过转换?

1 个答案:

答案 0 :(得分:3)

这是因为您还尝试使用相同的例程来处理RichTextView.TextChanged,而KeyPressEventArg没有通过TextChanged

如果您希望KeyPressEventArgs允许此事件,则需要为{{1}}事件设置单独的事件处理程序。