none
给自制组件添加键盘事件求教各位,谢谢!!! RRS feed

  • 问题

  • 给自制组件添加键盘事件求教各位,谢谢!!!
    如下,我写了一个组件Ntext2,但添加后,在他获得焦点后,我发现下面的无反应, 求教各位,谢谢!!!
    Private Sub Ntext2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Ntext2.KeyDown
    MsgBox(e.KeyCode)
    End Sub

    组件:

    Imports System.Drawing.Drawing2D
    Public Class Ntext
        Inherits Control
        Private _ColorFillSolid As Color = Color.White
        Public Property ColorFillSolid() As Color
            Get
                Return _ColorFillSolid
            End Get
            Set(ByVal value As Color)
                _ColorFillSolid = value
                Invalidate()
            End Set
        End Property
        Private _ColorFillSolidB As Color = Color.Lime
        Public Property ColorFillSolidB() As Color
            Get
                Return _ColorFillSolidB
            End Get
            Set(ByVal value As Color)
                _ColorFillSolidB = value
                Invalidate()
            End Set
        End Property
        Private TextT As String = "" 'newnlabel.MODE
        Public Property Ntext As String
            Get
                Return TextT
            End Get
            Set(ByVal value As String)
                TextT = value
                Invalidate()
            End Set
        End Property
        Private _NFont As Font = New Font("Verdana", 8, FontStyle.Regular)
        Public Property NFont() As System.Drawing.Font
            Get
                Return _NFont
            End Get
            Set(ByVal Value As System.Drawing.Font)
                _NFont = Value
                Invalidate()
            End Set
        End Property
      
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            Me.DoubleBuffered = True
            Dim mpen As New Pen(_ColorFillSolid)
            mpen.Width = 2
            Dim P1 As New Point(Me.Width, 0)
            Dim P2 As New Point(0, 0)
            Dim P3 As New Point(0, Me.Height)
            Dim P4() As Point = {P1, P2, P3}
            e.Graphics.DrawLines(mpen, P4)
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), 0, 0) '(Me.Height - e.Graphics.MeasureString("G", Me.Font).Height) / 2
            e.Graphics.DrawString(Me.Ntext, _NFont, New SolidBrush(_ColorFillSolidB), Me.Width - e.Graphics.MeasureString(Me.Ntext, Me.NFont).Width, e.Graphics.MeasureString("G", Me.Font).Height * 1.5) 
        End Sub
        Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
            MyBase.OnKeyDown(e)
           
        End Sub
    End Class

    2012年10月18日 2:10

答案

  • 你好,

    如果要自定义事件,你可以在你的控件里面添加如下代码:

        Public Event UserKeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
        Private Sub MyUserControl_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            RaiseEvent UserKeyDown(Me, e)
        End Sub

    使用的时候就和平常的事件一样:

        Private Sub Ntext1_UserKeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles Ntext1.UserKeyDown
            MsgBox("pressed!" & e.KeyCode)
        End Sub

    希望以上可以帮助到你。

    Best regards,



    Shanks Zen
    MSDN Community Support | Feedback to us


    2012年10月19日 7:37
    版主

全部回复

  • 你好,

    如果要自定义事件,你可以在你的控件里面添加如下代码:

        Public Event UserKeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
        Private Sub MyUserControl_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            RaiseEvent UserKeyDown(Me, e)
        End Sub

    使用的时候就和平常的事件一样:

        Private Sub Ntext1_UserKeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles Ntext1.UserKeyDown
            MsgBox("pressed!" & e.KeyCode)
        End Sub

    希望以上可以帮助到你。

    Best regards,



    Shanks Zen
    MSDN Community Support | Feedback to us


    2012年10月19日 7:37
    版主
  • 承载你自定义控件的窗体,是否将属性“keypreview”设置为True?如果要form以及内部的控件可以接受按键操作,这个属性需要设置为True。


    • 已编辑 fatbudy 2012年11月2日 3:13 别字
    2012年11月2日 3:12