Compartilhar via


Como: Manipular eventos de entrada do usuário nos controles de formulários do Windows

Este exemplo mostra como tratar a maioria dos eventos de teclado, mouse, foco e validação que podem ocorrer em um controle Formulários do Windows. A caixa de texto chamada TextBoxInput recebe os eventos quando ela tem o foco, e informações sobre cada evento são escritas na caixa de texto chamada TextBoxOutput na ordem em que os eventos são gerados. O aplicativo também inclui um conjunto de caixas de seleção que podem ser usados para filtrar os eventos que se deseja informar.

Exemplo

Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms

Namespace UserInputWalkthrough

    Public Class Form1
        Inherits Form

        Dim Label1 As New Label
        Dim Label2 As New Label
        Dim TextBoxOutput As New TextBox
        Dim WithEvents TextBoxInput As New TextBox
        Dim WithEvents GroupBoxEvents As New GroupBox
        Dim WithEvents ButtonClear As New Button
        Dim WithEvents LinkLabelDrag As New LinkLabel

        Dim WithEvents CheckBoxToggleAll As New CheckBox
        Dim WithEvents CheckBoxMouse As New CheckBox
        Dim WithEvents CheckBoxMouseEnter As New CheckBox
        Dim WithEvents CheckBoxMouseMove As New CheckBox
        Dim WithEvents CheckBoxMousePoints As New CheckBox
        Dim WithEvents CheckBoxMouseDrag As New CheckBox
        Dim WithEvents CheckBoxMouseDragOver As New CheckBox
        Dim WithEvents CheckBoxKeyboard As New CheckBox
        Dim WithEvents CheckBoxKeyUpDown As New CheckBox
        Dim WithEvents CheckBoxFocus As New CheckBox
        Dim WithEvents CheckBoxValidation As New CheckBox

        <STAThread()> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub

        Public Sub New()
            MyBase.New()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load

            Me.GroupBoxEvents.SuspendLayout()
            Me.SuspendLayout()

            Label1.Location = New Point(232, 12)
            Label1.Size = New Size(98, 14)
            Label1.AutoSize = True
            Label1.Text = "Generated Events:"

            Label2.Location = New Point(13, 12)
            Label2.Size = New Size(95, 14)
            Label2.AutoSize = True
            Label2.Text = "User Input Target:"

            TextBoxInput.Location = New Point(13, 34)
            TextBoxInput.Size = New Size(200, 200)
            TextBoxInput.AllowDrop = True
            TextBoxInput.AutoSize = False
            TextBoxInput.Cursor = Cursors.Cross
            TextBoxInput.Multiline = True
            TextBoxInput.TabIndex = 1

            LinkLabelDrag.AllowDrop = True
            LinkLabelDrag.AutoSize = True
            LinkLabelDrag.Location = New Point(13, 240)
            LinkLabelDrag.Size = New Size(175, 14)
            LinkLabelDrag.TabIndex = 2
            LinkLabelDrag.TabStop = True
            LinkLabelDrag.Text = "Click here to use as a drag source"
            LinkLabelDrag.Links.Add(New LinkLabel.Link(0, _
                LinkLabelDrag.Text.Length))

            GroupBoxEvents.Location = New Point(13, 281)
            GroupBoxEvents.Size = New Size(200, 302)
            GroupBoxEvents.TabIndex = 3
            GroupBoxEvents.TabStop = False
            GroupBoxEvents.Text = "Event Filter:"
            GroupBoxEvents.Controls.Add(CheckBoxMouseEnter)
            GroupBoxEvents.Controls.Add(CheckBoxToggleAll)
            GroupBoxEvents.Controls.Add(CheckBoxMousePoints)
            GroupBoxEvents.Controls.Add(CheckBoxKeyUpDown)
            GroupBoxEvents.Controls.Add(CheckBoxMouseDragOver)
            GroupBoxEvents.Controls.Add(CheckBoxMouseDrag)
            GroupBoxEvents.Controls.Add(CheckBoxValidation)
            GroupBoxEvents.Controls.Add(CheckBoxMouseMove)
            GroupBoxEvents.Controls.Add(CheckBoxFocus)
            GroupBoxEvents.Controls.Add(CheckBoxKeyboard)
            GroupBoxEvents.Controls.Add(CheckBoxMouse)

            CheckBoxToggleAll.AutoSize = True
            CheckBoxToggleAll.Location = New Point(7, 20)
            CheckBoxToggleAll.Size = New Size(122, 17)
            CheckBoxToggleAll.TabIndex = 4
            CheckBoxToggleAll.Text = "Toggle All Events"

            CheckBoxMouse.AutoSize = True
            CheckBoxMouse.Location = New Point(7, 45)
            CheckBoxMouse.Size = New Size(137, 17)
            CheckBoxMouse.TabIndex = 5
            CheckBoxMouse.Text = "Mouse and Click Events"

            CheckBoxMouseEnter.AutoSize = True
            CheckBoxMouseEnter.Location = New Point(26, 69)
            CheckBoxMouseEnter.Margin = New Padding(3, 3, 3, 1)
            CheckBoxMouseEnter.Size = New System.Drawing.Size(151, 17)
            CheckBoxMouseEnter.TabIndex = 6
            CheckBoxMouseEnter.Text = "Mouse Enter/Hover/Leave"

            CheckBoxMouseMove.AutoSize = True
            CheckBoxMouseMove.Location = New Point(26, 89)
            CheckBoxMouseMove.Margin = New Padding(3, 2, 3, 3)
            CheckBoxMouseMove.Size = New Size(120, 17)
            CheckBoxMouseMove.TabIndex = 7
            CheckBoxMouseMove.Text = "Mouse Move Events"

            CheckBoxMousePoints.AutoSize = True
            CheckBoxMousePoints.Location = New Point(26, 112)
            CheckBoxMousePoints.Margin = New Padding(3, 3, 3, 1)
            CheckBoxMousePoints.Size = New Size(141, 17)
            CheckBoxMousePoints.TabIndex = 8
            CheckBoxMousePoints.Text = "Draw Mouse Points"

            CheckBoxMouseDrag.AutoSize = True
            CheckBoxMouseDrag.Location = New Point(26, 135)
            CheckBoxMouseDrag.Margin = New Padding(3, 1, 3, 3)
            CheckBoxMouseDrag.Size = New Size(151, 17)
            CheckBoxMouseDrag.TabIndex = 9
            CheckBoxMouseDrag.Text = "Mouse Drag && Drop Events"

            CheckBoxMouseDragOver.AutoSize = True
            CheckBoxMouseDragOver.Location = New Point(44, 159)
            CheckBoxMouseDragOver.Size = New Size(142, 17)
            CheckBoxMouseDragOver.TabIndex = 10
            CheckBoxMouseDragOver.Text = "Mouse Drag Over Events"

            CheckBoxKeyboard.AutoSize = True
            CheckBoxKeyboard.Location = New Point(8, 184)
            CheckBoxKeyboard.Size = New Size(103, 17)
            CheckBoxKeyboard.TabIndex = 11
            CheckBoxKeyboard.Text = "Keyboard Events"

            CheckBoxKeyUpDown.AutoSize = True
            CheckBoxKeyUpDown.Location = New Point(26, 207)
            CheckBoxKeyUpDown.Margin = New Padding(3, 3, 3, 1)
            CheckBoxKeyUpDown.Size = New Size(133, 17)
            CheckBoxKeyUpDown.TabIndex = 12
            CheckBoxKeyUpDown.Text = "Key Up && Down Events"

            CheckBoxFocus.AutoSize = True
            CheckBoxFocus.Location = New Point(8, 233)
            CheckBoxFocus.Margin = New Padding(3, 2, 3, 3)
            CheckBoxFocus.Size = New Size(146, 17)
            CheckBoxFocus.TabIndex = 13
            CheckBoxFocus.Text = "Focus && Activation Events"

            CheckBoxValidation.AutoSize = True
            CheckBoxValidation.Location = New Point(8, 257)
            CheckBoxValidation.Size = New Size(104, 17)
            CheckBoxValidation.TabIndex = 14
            CheckBoxValidation.Text = "Validation Events"

            TextBoxOutput.Location = New Point(232, 34)
            TextBoxOutput.Size = New Size(308, 510)
            TextBoxOutput.Multiline = True
            TextBoxOutput.CausesValidation = False
            TextBoxOutput.ReadOnly = True
            TextBoxOutput.ScrollBars = ScrollBars.Vertical
            TextBoxOutput.TabIndex = 15
            TextBoxOutput.WordWrap = False

            ButtonClear.Location = New Point(232, 560)
            ButtonClear.Size = New Size(308, 23)
            ButtonClear.TabIndex = 16
            ButtonClear.Text = "Clear Event List"

            Me.ClientSize = New Size(552, 595)
            Me.Controls.Add(LinkLabelDrag)
            Me.Controls.Add(ButtonClear)
            Me.Controls.Add(GroupBoxEvents)
            Me.Controls.Add(Label1)
            Me.Controls.Add(Label2)
            Me.Controls.Add(TextBoxInput)
            Me.Controls.Add(TextBoxOutput)
            Me.Text = "User Input Events"

            Me.GroupBoxEvents.ResumeLayout(False)
            Me.GroupBoxEvents.PerformLayout()
            Me.ResumeLayout(False)
            Me.PerformLayout()
            CheckAllChildCheckBoxes(Me, True)
        End Sub

        ' Recursively search the form for all contained checkboxes and 
        ' initially check them
        Private Sub CheckAllChildCheckBoxes(ByRef parent As Control, _
            ByVal value As Boolean)

            Dim currentControl As Control
            Dim box As CheckBox
            For Each currentControl In parent.Controls
                box = TryCast(currentControl, CheckBox)
                If box IsNot Nothing Then
                    box.Checked = value
                End If

                'Recurse if control contains other controls
                If currentControl.Controls.Count > 0 Then
                    CheckAllChildCheckBoxes(currentControl, value)
                End If
            Next
        End Sub

        ' All-purpose method for displaying a line of text in one of the
        ' text boxes.
        Private Sub DisplayLine(ByRef line As String)

            TextBoxOutput.AppendText(line)
            TextBoxOutput.AppendText(ControlChars.NewLine)
        End Sub

        ' Click event handler for the button that clears the text box.
        Private Sub ButtonClear_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles ButtonClear.Click

            TextBoxOutput.Invalidate()
            TextBoxOutput.Clear()
        End Sub

        Private Sub TextBoxInput_KeyDown(ByVal sender As Object, _
            ByVal e As KeyEventArgs) Handles TextBoxInput.KeyDown

            If CheckBoxKeyUpDown.Checked = True Then
                DisplayLine("KeyDown: " + e.KeyData.ToString())
            End If
        End Sub

        Private Sub TextBoxInput_KeyUp(ByVal sender As Object, _
            ByVal e As KeyEventArgs) Handles TextBoxInput.KeyUp

            If CheckBoxKeyUpDown.Checked = True Then
                DisplayLine("KeyUp: " + e.KeyData.ToString())
            End If
        End Sub

        Private Sub TextBoxInput_KeyPress(ByVal sender As Object, _
            ByVal e As KeyPressEventArgs) Handles TextBoxInput.KeyPress

            If CheckBoxKeyboard.Checked = True Then

                If Char.IsWhiteSpace(e.KeyChar) Then
                    Dim keyVal As Integer
                    keyVal = AscW(e.KeyChar)
                    DisplayLine("KeyPress: WS-" + keyVal.ToString())
                Else
                    DisplayLine("KeyPress: " + e.KeyChar.ToString())
                End If
            End If
        End Sub

        Private Sub TextBoxInput_Click(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.Click

            If CheckBoxMouse.Checked = True Then
                DisplayLine("Click event")
            End If
        End Sub

        Private Sub TextBoxInput_DoubleClick(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.DoubleClick

            If CheckBoxMouse.Checked = True Then
                DisplayLine("DoubleClick event")
            End If
        End Sub

        Private Sub TextBoxInput_MouseClick(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles TextBoxInput.MouseClick

            If CheckBoxMouse.Checked = True Then
                DisplayLine("MouseClick: " + e.Button.ToString() + _
                    " " + e.Location.ToString())
            End If
        End Sub

        Private Sub TextBoxInput_MouseDoubleClick(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles TextBoxInput.MouseDoubleClick

            If CheckBoxMouse.Checked = True Then
                DisplayLine("MouseDoubleClick: " + e.Button.ToString() + _
                    " " + e.Location.ToString())
            End If
        End Sub

        Private Sub TextBoxInput_MouseDown(ByVal sender As System.Object, _
            ByVal e As MouseEventArgs) Handles TextBoxInput.MouseDown

            If CheckBoxMouse.Checked = True Then
                DisplayLine("MouseDown: " + e.Button.ToString() + _
                    " " + e.Location.ToString())
            End If
        End Sub

        Private Sub TextBoxInput_MouseUp(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles TextBoxInput.MouseUp

            If CheckBoxMouse.Checked = True Then
                DisplayLine("MouseUp: " + e.Button.ToString() + _
                    " " + e.Location.ToString())
            End If

            ' The TextBox control was designed to change focus only on  
            ' the primary click, so force focus to avoid user confusion.
            If TextBoxInput.Focused = False Then
                TextBoxInput.Focus()
            End If
        End Sub

        Private Sub TextBoxInput_MouseEnter(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.MouseEnter

            If CheckBoxMouseEnter.Checked = True Then
                DisplayLine("MouseEnter event")
            End If
        End Sub

        Private Sub TextBoxInput_MouseHover(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.MouseHover

            If CheckBoxMouseEnter.Checked = True Then
                DisplayLine("MouseHover event")
            End If
        End Sub

        Private Sub TextBoxInput_MouseLeave(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.MouseLeave

            If CheckBoxMouseEnter.Checked = True Then
                DisplayLine("MouseLeave event")
            End If
        End Sub

        Private Sub TextBoxInput_MouseWheel(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles TextBoxInput.MouseWheel

            If CheckBoxMouse.Checked = True Then
                DisplayLine("MouseWheel: " + e.Delta.ToString() + _
                    " detents at " + e.Location.ToString())
            End If
        End Sub

        Private Sub TextBoxInput_MouseMove(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles TextBoxInput.MouseMove

            If CheckBoxMouseMove.Checked = True Then
                DisplayLine("MouseMove: " + e.Button.ToString() + " " + _
                    e.Location.ToString())
            End If

            If CheckBoxMousePoints.Checked = True Then
                Dim g As Graphics = TextBoxInput.CreateGraphics()
                g.FillRectangle(Brushes.Black, e.Location.X, _
                    e.Location.Y, 1, 1)
                g.Dispose()
            End If
        End Sub

        Private Sub TextBoxInput_MouseCaptureChanged( _
            ByVal sender As Object, ByVal e As EventArgs) _
            Handles TextBoxInput.MouseCaptureChanged

            If CheckBoxMouseDrag.Checked = True Then
                DisplayLine("MouseCaptureChanged event")
            End If
        End Sub

        Private Sub TextBoxInput_DragEnter(ByVal sender As Object, _
            ByVal e As DragEventArgs) Handles TextBoxInput.DragEnter

            Dim pt As Point
            If CheckBoxMouseDrag.Checked = True Then
                pt = New Point(e.X, e.Y)
                DisplayLine("DragEnter: " + _
                    CovertKeyStateToString(e.KeyState) _
                    + " at " + pt.ToString())
            End If
        End Sub

        Private Sub TextBoxInput_DragDrop(ByVal sender As Object, _
            ByVal e As DragEventArgs) Handles TextBoxInput.DragDrop

            If CheckBoxMouseDrag.Checked = True Then
                Dim pt As Point
                pt = New Point(e.X, e.Y)

                DisplayLine("DragDrop: " + _
                    CovertKeyStateToString(e.KeyState) _
                    + " at " + pt.ToString())
            End If
        End Sub

        Private Sub TextBoxInput_DragOver(ByVal sender As Object, _
            ByVal e As DragEventArgs) Handles TextBoxInput.DragOver

            If CheckBoxMouseDragOver.Checked = True Then
                Dim pt As Point
                pt = New Point(e.X, e.Y)
                DisplayLine("DragOver: " + _
                    CovertKeyStateToString(e.KeyState) _
                    + " at " + pt.ToString())
            End If

            ' Allow if drop data is of type string.
            If Not (e.Data.GetDataPresent(GetType(String))) Then
                e.Effect = DragDropEffects.None
            Else
                e.Effect = DragDropEffects.Copy
            End If
        End Sub

        Private Sub TextBoxInput_DragLeave(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.DragLeave

            If CheckBoxMouseDrag.Checked = True Then
                DisplayLine("DragLeave event")
            End If
        End Sub

        Private Function CovertKeyStateToString(ByVal keyState As Integer) _
            As String

            Dim keyString As String = "None"

            ' Which button was pressed?
            If ((keyState And 1) = 1) Then
                keyString = "Left"
            ElseIf ((keyState And 2) = 2) Then
                keyString = "Right"
            ElseIf ((keyState And 16) = 16) Then
                keyString = "Middle"
            End If

            ' Are one or more modifier keys also pressed?
            If ((keyState And 4) = 4) Then
                keyString += "+SHIFT"
            End If
            If ((keyState And 8) = 8) Then
                keyString += "+CTRL"
            End If
            If ((keyState And 32) = 32) Then
                keyString += "+ALT"
            End If

            Return keyString
        End Function

        Private Sub TextBoxInput_Enter(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.Enter

            If CheckBoxFocus.Checked = True Then
                DisplayLine("Enter event")
            End If
        End Sub

        Private Sub TextBoxInput_Leave(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.Leave

            If CheckBoxFocus.Checked = True Then
                DisplayLine("Leave event")
            End If
        End Sub

        Private Sub TextBoxInput_GotFocus(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.GotFocus

            If CheckBoxFocus.Checked = True Then
                DisplayLine("GotFocus event")
            End If
        End Sub

        Private Sub TextBoxInput_LostFocus(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.LostFocus

            If CheckBoxFocus.Checked = True Then
                DisplayLine("LostFocus event")
            End If
        End Sub

        Private Sub TextBoxInput_Validated(ByVal sender As Object, _
            ByVal e As EventArgs) Handles TextBoxInput.Validated

            If CheckBoxValidation.Checked = True Then
                DisplayLine("Validated event")
            End If
        End Sub

        Private Sub TextBoxInput_Validating(ByVal sender As Object, _
            ByVal e As CancelEventArgs) _
            Handles TextBoxInput.Validating

            If CheckBoxValidation.Checked = True Then
                DisplayLine("Validating event")
            End If
        End Sub

        Private Sub CheckBoxToggleAll_CheckedChanged( _
            ByVal sender As Object, ByVal e As EventArgs) _
            Handles CheckBoxToggleAll.CheckedChanged

            Dim cb As CheckBox
            cb = TryCast(sender, CheckBox)
            If cb IsNot Nothing Then
                CheckAllChildCheckBoxes(Me, cb.Checked)
            End If
        End Sub

        Private Sub CheckBoxMouse_CheckedChanged( _
            ByVal sender As Object, ByVal e As EventArgs) _
            Handles CheckBoxMouse.CheckedChanged

            ConfigureCheckBoxSettings()
        End Sub

        Private Sub CheckBoxMouseDrag_CheckedChanged( _
            ByVal sender As Object, ByVal e As EventArgs) _
            Handles CheckBoxMouseDrag.CheckedChanged

            ConfigureCheckBoxSettings()
        End Sub

        Private Sub CheckBoxKeyboard_CheckedChanged( _
            ByVal sender As Object, ByVal e As EventArgs) _
            Handles CheckBoxKeyboard.CheckedChanged

            ConfigureCheckBoxSettings()
        End Sub

        Private Sub CheckBoxMouseMove_CheckedChanged( _
            ByVal sender As Object, ByVal e As EventArgs) _
            Handles CheckBoxMouseEnter.CheckedChanged, _
                CheckBoxMouseMove.CheckedChanged

            ConfigureCheckBoxSettings()
        End Sub

        ' Reconcile dependencies between the check box 
        ' selection choices. 
        Private Sub ConfigureCheckBoxSettings()

            ' CheckBoxMouse is a top-level check box.
            If CheckBoxMouse.Checked = False Then
                CheckBoxMouseEnter.Enabled = False
                CheckBoxMouseMove.Enabled = False
                CheckBoxMouseDrag.Enabled = False
                CheckBoxMouseDragOver.Enabled = False
                CheckBoxMousePoints.Enabled = False
            Else
                CheckBoxMouseEnter.Enabled = True
                CheckBoxMouseMove.Enabled = True
                CheckBoxMouseDrag.Enabled = True
                CheckBoxMousePoints.Enabled = True

                ' Enable children depending on the state of the parent.
                If CheckBoxMouseDrag.Checked = False Then
                    CheckBoxMouseDragOver.Enabled = False
                Else
                    CheckBoxMouseDragOver.Enabled = True
                End If
            End If

            If CheckBoxKeyboard.Checked = False Then
                CheckBoxKeyUpDown.Enabled = False
            Else
                CheckBoxKeyUpDown.Enabled = True
            End If
        End Sub

        Private Sub LinkLabelDrag_MouseDown(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles LinkLabelDrag.MouseDown

            Dim data As String = "Sample Data"
            LinkLabelDrag.DoDragDrop(data, DragDropEffects.All)
        End Sub

        Private Sub LinkLabelDrag_GiveFeedback(ByVal sender As Object, _
            ByVal e As GiveFeedbackEventArgs) _
            Handles LinkLabelDrag.GiveFeedback

            If ((e.Effect And DragDropEffects.Copy) = _
                DragDropEffects.Copy) Then
                LinkLabelDrag.Cursor = Cursors.HSplit
            Else
                LinkLabelDrag.Cursor = Cursors.Default
            End If
        End Sub

    End Class
End Namespace
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace UserInputWalkthrough
{
    public class Form1 : Form
    {
        Label Label1 = new Label();
        Label Label2 = new Label();
        TextBox TextBoxOutput = new TextBox();
        TextBox TextBoxInput = new TextBox();
        GroupBox GroupBoxEvents = new GroupBox();
        Button ButtonClear = new Button();
        LinkLabel LinkLabelDrag = new LinkLabel();

        CheckBox CheckBoxToggleAll = new CheckBox();
        CheckBox CheckBoxMouse = new CheckBox();
        CheckBox CheckBoxMouseEnter = new CheckBox();
        CheckBox CheckBoxMouseMove = new CheckBox();
        CheckBox CheckBoxMousePoints = new CheckBox();
        CheckBox CheckBoxMouseDrag = new CheckBox();
        CheckBox CheckBoxMouseDragOver = new CheckBox();
        CheckBox CheckBoxKeyboard = new CheckBox();
        CheckBox CheckBoxKeyUpDown = new CheckBox();
        CheckBox CheckBoxFocus = new CheckBox();
        CheckBox CheckBoxValidation = new CheckBox();

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public Form1()
            : base()
        {
            this.Load += new EventHandler(Form1_Load);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.GroupBoxEvents.SuspendLayout();
            this.SuspendLayout();

            Label1.Location = new Point(232, 12);
            Label1.Size = new Size(98, 14);
            Label1.AutoSize = true;
            Label1.Text = "Generated Events:";

            Label2.Location = new Point(13, 12);
            Label2.Size = new Size(95, 14);
            Label2.AutoSize = true;
            Label2.Text = "User Input Target:";

            TextBoxInput.Location = new Point(13, 34);
            TextBoxInput.Size = new Size(200, 200);
            TextBoxInput.AllowDrop = true;
            TextBoxInput.AutoSize = false;
            TextBoxInput.Cursor = Cursors.Cross;
            TextBoxInput.Multiline = true;
            TextBoxInput.TabIndex = 1;

            LinkLabelDrag.AllowDrop = true;
            LinkLabelDrag.AutoSize = true;
            LinkLabelDrag.Location = new Point(13, 240);
            LinkLabelDrag.Size = new Size(175, 14);
            LinkLabelDrag.TabIndex = 2;
            LinkLabelDrag.TabStop = true;
            LinkLabelDrag.Text = "Click here to use as a drag source";
            LinkLabelDrag.Links.Add(new LinkLabel.Link(0,
                LinkLabelDrag.Text.Length));

            GroupBoxEvents.Location = new Point(13, 281);
            GroupBoxEvents.Size = new Size(200, 302);
            GroupBoxEvents.Text = "Event Filter:";
            GroupBoxEvents.TabStop = true;
            GroupBoxEvents.TabIndex = 3;
            GroupBoxEvents.Controls.Add(CheckBoxMouseEnter);
            GroupBoxEvents.Controls.Add(CheckBoxToggleAll);
            GroupBoxEvents.Controls.Add(CheckBoxMousePoints);
            GroupBoxEvents.Controls.Add(CheckBoxKeyUpDown);
            GroupBoxEvents.Controls.Add(CheckBoxMouseDragOver);
            GroupBoxEvents.Controls.Add(CheckBoxMouseDrag);
            GroupBoxEvents.Controls.Add(CheckBoxValidation);
            GroupBoxEvents.Controls.Add(CheckBoxMouseMove);
            GroupBoxEvents.Controls.Add(CheckBoxFocus);
            GroupBoxEvents.Controls.Add(CheckBoxKeyboard);
            GroupBoxEvents.Controls.Add(CheckBoxMouse);

            CheckBoxToggleAll.AutoSize = true;
            CheckBoxToggleAll.Location = new Point(7, 20);
            CheckBoxToggleAll.Size = new Size(122, 17);
            CheckBoxToggleAll.TabIndex = 4;
            CheckBoxToggleAll.Text = "Toggle All Events";

            CheckBoxMouse.AutoSize = true;
            CheckBoxMouse.Location = new Point(7, 45);
            CheckBoxMouse.Size = new Size(137, 17);
            CheckBoxMouse.TabIndex = 5;
            CheckBoxMouse.Text = "Mouse and Click Events";

            CheckBoxMouseEnter.AutoSize = true;
            CheckBoxMouseEnter.Location = new Point(26, 69);
            CheckBoxMouseEnter.Margin = new Padding(3, 3, 3, 1);
            CheckBoxMouseEnter.Size = new System.Drawing.Size(151, 17);
            CheckBoxMouseEnter.TabIndex = 6;
            CheckBoxMouseEnter.Text = "Mouse Enter/Hover/Leave";

            CheckBoxMouseMove.AutoSize = true;
            CheckBoxMouseMove.Location = new Point(26, 89);
            CheckBoxMouseMove.Margin = new Padding(3, 2, 3, 3);
            CheckBoxMouseMove.Size = new Size(120, 17);
            CheckBoxMouseMove.TabIndex = 7;
            CheckBoxMouseMove.Text = "Mouse Move Events";

            CheckBoxMousePoints.AutoSize = true;
            CheckBoxMousePoints.Location = new Point(26, 112);
            CheckBoxMousePoints.Margin = new Padding(3, 3, 3, 1);
            CheckBoxMousePoints.Size = new Size(141, 17);
            CheckBoxMousePoints.TabIndex = 8;
            CheckBoxMousePoints.Text = "Draw Mouse Points";

            CheckBoxMouseDrag.AutoSize = true;
            CheckBoxMouseDrag.Location = new Point(26, 135);
            CheckBoxMouseDrag.Margin = new Padding(3, 1, 3, 3);
            CheckBoxMouseDrag.Size = new Size(151, 17);
            CheckBoxMouseDrag.TabIndex = 9;
            CheckBoxMouseDrag.Text = "Mouse Drag && Drop Events";

            CheckBoxMouseDragOver.AutoSize = true;
            CheckBoxMouseDragOver.Location = new Point(44, 159);
            CheckBoxMouseDragOver.Size = new Size(142, 17);
            CheckBoxMouseDragOver.TabIndex = 10;
            CheckBoxMouseDragOver.Text = "Mouse Drag Over Events";

            CheckBoxKeyboard.AutoSize = true;
            CheckBoxKeyboard.Location = new Point(8, 184);
            CheckBoxKeyboard.Size = new Size(103, 17);
            CheckBoxKeyboard.TabIndex = 11;
            CheckBoxKeyboard.Text = "Keyboard Events";

            CheckBoxKeyUpDown.AutoSize = true;
            CheckBoxKeyUpDown.Location = new Point(26, 207);
            CheckBoxKeyUpDown.Margin = new Padding(3, 3, 3, 1);
            CheckBoxKeyUpDown.Size = new Size(133, 17);
            CheckBoxKeyUpDown.TabIndex = 12;
            CheckBoxKeyUpDown.Text = "Key Up && Down Events";

            CheckBoxFocus.AutoSize = true;
            CheckBoxFocus.Location = new Point(8, 233);
            CheckBoxFocus.Margin = new Padding(3, 2, 3, 3);
            CheckBoxFocus.Size = new Size(146, 17);
            CheckBoxFocus.TabIndex = 13;
            CheckBoxFocus.Text = "Focus && Activation Events";

            CheckBoxValidation.AutoSize = true;
            CheckBoxValidation.Location = new Point(8, 257);
            CheckBoxValidation.Size = new Size(104, 17);
            CheckBoxValidation.TabIndex = 14;
            CheckBoxValidation.Text = "Validation Events";

            TextBoxOutput.Location = new Point(232, 34);
            TextBoxOutput.Size = new Size(308, 510);
            TextBoxOutput.Multiline = true;
            TextBoxOutput.CausesValidation = false;
            TextBoxOutput.ReadOnly = true;
            TextBoxOutput.ScrollBars = ScrollBars.Vertical;
            TextBoxOutput.TabIndex = 15;
            TextBoxOutput.WordWrap = false;

            ButtonClear.Location = new Point(232, 560);
            ButtonClear.Size = new Size(308, 23);
            ButtonClear.TabIndex = 16;
            ButtonClear.Text = "Clear Event List";

            this.ClientSize = new Size(552, 595);
            this.Controls.Add(LinkLabelDrag);
            this.Controls.Add(ButtonClear);
            this.Controls.Add(GroupBoxEvents);
            this.Controls.Add(Label1);
            this.Controls.Add(Label2);
            this.Controls.Add(TextBoxInput);
            this.Controls.Add(TextBoxOutput);
            this.Text = "User Input Events";

            ButtonClear.Click +=
                new EventHandler(ButtonClear_Click);
            TextBoxInput.KeyDown +=
                new KeyEventHandler(TextBoxInput_KeyDown);
            TextBoxInput.KeyPress +=
                new KeyPressEventHandler(TextBoxInput_KeyPress);
            TextBoxInput.KeyUp +=
                new KeyEventHandler(TextBoxInput_KeyUp);
            TextBoxInput.Click +=
                new EventHandler(TextBoxInput_Click);
            TextBoxInput.DoubleClick +=
                new EventHandler(TextBoxInput_DoubleClick);
            TextBoxInput.MouseClick +=
                new MouseEventHandler(TextBoxInput_MouseClick);
            TextBoxInput.MouseDoubleClick +=
                new MouseEventHandler(TextBoxInput_MouseDoubleClick);
            TextBoxInput.MouseDown +=
                new MouseEventHandler(TextBoxInput_MouseDown);
            TextBoxInput.MouseUp +=
                new MouseEventHandler(TextBoxInput_MouseUp);
            TextBoxInput.MouseEnter +=
                new EventHandler(TextBoxInput_MouseEnter);
            TextBoxInput.MouseHover +=
                new EventHandler(TextBoxInput_MouseHover);
            TextBoxInput.MouseLeave +=
                new EventHandler(TextBoxInput_MouseLeave);
            TextBoxInput.MouseWheel +=
                new MouseEventHandler(TextBoxInput_MouseWheel);
            TextBoxInput.MouseMove +=
                new MouseEventHandler(TextBoxInput_MouseMove);
            TextBoxInput.MouseCaptureChanged +=
                new EventHandler(TextBoxInput_MouseCaptureChanged);
            TextBoxInput.DragEnter +=
                new DragEventHandler(TextBoxInput_DragEnter);
            TextBoxInput.DragDrop +=
                new DragEventHandler(TextBoxInput_DragDrop);
            TextBoxInput.DragOver +=
                new DragEventHandler(TextBoxInput_DragOver);
            TextBoxInput.DragLeave +=
                new EventHandler(TextBoxInput_DragLeave);
            TextBoxInput.Enter +=
                new EventHandler(TextBoxInput_Enter);
            TextBoxInput.Leave +=
                new EventHandler(TextBoxInput_Leave);
            TextBoxInput.GotFocus +=
                new EventHandler(TextBoxInput_GotFocus);
            TextBoxInput.LostFocus +=
                new EventHandler(TextBoxInput_LostFocus);
            TextBoxInput.Validated +=
                new EventHandler(TextBoxInput_Validated);
            TextBoxInput.Validating +=
                new CancelEventHandler(TextBoxInput_Validating);

            LinkLabelDrag.MouseDown +=
                new MouseEventHandler(LinkLabelDrag_MouseDown);
            LinkLabelDrag.GiveFeedback +=
                new GiveFeedbackEventHandler(LinkLabelDrag_GiveFeedback);

            CheckBoxToggleAll.CheckedChanged +=
                new EventHandler(CheckBoxToggleAll_CheckedChanged);
            CheckBoxMouse.CheckedChanged +=
                new EventHandler(CheckBoxMouse_CheckedChanged);
            CheckBoxMouseDrag.CheckedChanged +=
                new EventHandler(CheckBoxMouseDrag_CheckedChanged);
            CheckBoxMouseEnter.CheckedChanged +=
                new EventHandler(CheckBoxMouseMove_CheckedChanged);
            CheckBoxMouseMove.CheckedChanged +=
                new EventHandler(CheckBoxMouseMove_CheckedChanged);
            CheckBoxKeyboard.CheckedChanged +=
                new EventHandler(CheckBoxKeyboard_CheckedChanged);

            this.GroupBoxEvents.ResumeLayout(false);
            this.GroupBoxEvents.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
            CheckAllChildCheckBoxes(this, true);
        }

        // Recursively search the form for all contained checkboxes and 
        // initially check them
        private void CheckAllChildCheckBoxes(Control parent, bool value)
        {
            CheckBox box;
            foreach (Control currentControl in parent.Controls)
            {
                if (currentControl is CheckBox)
                {
                    box = (CheckBox)currentControl;
                    box.Checked = value;
                }

                // Recurse if control contains other controls
                if (currentControl.Controls.Count > 0)
                {
                    CheckAllChildCheckBoxes(currentControl, value);
                }
            }
        }

        // All-purpose method for displaying a line of text in the
        // text boxe.
        private void DisplayLine(string line)
        {
            TextBoxOutput.AppendText(line);
            TextBoxOutput.AppendText(Environment.NewLine);
        }

        // Click event handler for the button that clears the text box.
        private void ButtonClear_Click(object sender, EventArgs e)
        {
            TextBoxOutput.Invalidate();
            TextBoxOutput.Clear();
        }

        private void TextBoxInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (CheckBoxKeyUpDown.Checked)
            {
                DisplayLine("KeyDown: " + e.KeyData.ToString());
            }
        }

        private void TextBoxInput_KeyUp(object sender, KeyEventArgs e)
        {
            if (CheckBoxKeyUpDown.Checked)
            {
                DisplayLine("KeyUp: " + e.KeyData.ToString());
            }
        }

        private void TextBoxInput_KeyPress(object sender,
            KeyPressEventArgs e)
        {
            if (CheckBoxKeyboard.Checked)
            {
                if (Char.IsWhiteSpace(e.KeyChar))
                {
                    DisplayLine("KeyPress: WS");
                }
                else
                {
                    DisplayLine("KeyPress: " + e.KeyChar.ToString());
                }
            }
        }

        private void TextBoxInput_Click(object sender, EventArgs e)
        {
            if (CheckBoxMouse.Checked)
            {
                DisplayLine("Click event");
            }
        }

        private void TextBoxInput_DoubleClick(object sender, EventArgs e)
        {
            if (CheckBoxMouse.Checked)
            {
                DisplayLine("DoubleClick event");
            }
        }

        private void TextBoxInput_MouseClick(object sender, MouseEventArgs e)
        {
            if (CheckBoxMouse.Checked)
            {
                DisplayLine("MouseClick: " + e.Button.ToString() +
                        " " + e.Location.ToString());
            }
        }

        private void TextBoxInput_MouseDoubleClick(object sender,
            MouseEventArgs e)
        {
            if (CheckBoxMouse.Checked)
            {
                DisplayLine("MouseDoubleClick: " + e.Button.ToString() +
                        " " + e.Location.ToString());
            }
        }

        private void TextBoxInput_MouseDown(object sender,
            MouseEventArgs e)
        {
            if (CheckBoxMouse.Checked)
            {
                DisplayLine("MouseDown: " + e.Button.ToString() +
                        " " + e.Location.ToString());
            }
        }

        private void TextBoxInput_MouseUp(object sender,
            MouseEventArgs e)
        {
            if (CheckBoxMouse.Checked)
            {
                DisplayLine("MouseUp: " + e.Button.ToString() +
                        " " + e.Location.ToString());
            }

            // The TextBox control was designed to change focus only on  
            // the primary click, so force focus to avoid user confusion.
            if (!TextBoxInput.Focused)
            {
                TextBoxInput.Focus();
            }
        }

        private void TextBoxInput_MouseEnter(object sender, EventArgs e)
        {
            if (CheckBoxMouseEnter.Checked)
            {
                DisplayLine("MouseEnter event");
            }
        }

        private void TextBoxInput_MouseHover(object sender, EventArgs e)
        {
            if (CheckBoxMouseEnter.Checked)
            {
                DisplayLine("MouseHover event");
            }
        }

        private void TextBoxInput_MouseLeave(object sender, EventArgs e)
        {
            if (CheckBoxMouseEnter.Checked)
            {
                DisplayLine("MouseLeave event");
            }
        }

        private void TextBoxInput_MouseWheel(object sender,
            MouseEventArgs e)
        {
            if (CheckBoxMouse.Checked)
            {
                DisplayLine("MouseWheel: " + e.Delta.ToString() +
                        " detents at " + e.Location.ToString());
            }
        }

        private void TextBoxInput_MouseMove(object sender,
            MouseEventArgs e)
        {
            if (CheckBoxMouseMove.Checked)
            {
                DisplayLine("MouseMove: " + e.Button.ToString() + " " +
                        e.Location.ToString());
            }

            if (CheckBoxMousePoints.Checked)
            {
                Graphics g = TextBoxInput.CreateGraphics();
                g.FillRectangle(Brushes.Black, e.Location.X,
                    e.Location.Y, 1, 1);
                g.Dispose();
            }
        }

        private void TextBoxInput_MouseCaptureChanged(object sender,
            EventArgs e)
        {
            if (CheckBoxMouseDrag.Checked)
            {
                DisplayLine("MouseCaptureChanged event");
            }
        }

        private void TextBoxInput_DragEnter(object sender,
            DragEventArgs e)
        {
            if (CheckBoxMouseDrag.Checked)
            {
                Point pt = new Point(e.X, e.Y);
                DisplayLine("DragEnter: " +
                    CovertKeyStateToString(e.KeyState)
                    + " at " + pt.ToString());
            }
        }

        private void TextBoxInput_DragDrop(object sender,
            DragEventArgs e)
        {
            if (CheckBoxMouseDrag.Checked)
            {
                Point pt = new Point(e.X, e.Y);
                DisplayLine("DragDrop: " +
                    CovertKeyStateToString(e.KeyState)
                    + " at " + pt.ToString());
            }
        }

        private void TextBoxInput_DragOver(object sender,
            DragEventArgs e)
        {
            if (CheckBoxMouseDragOver.Checked)
            {
                Point pt = new Point(e.X, e.Y);
                DisplayLine("DragOver: " +
                    CovertKeyStateToString(e.KeyState)
                    + " at " + pt.ToString());
            }

            // Allow if drop data is of type string.
            if (!e.Data.GetDataPresent(typeof(String)))
            {
                e.Effect = DragDropEffects.None;
            }
            else
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void TextBoxInput_DragLeave(object sender,
            EventArgs e)
        {
            if (CheckBoxMouseDrag.Checked)
            {
                DisplayLine("DragLeave event");
            }
        }

        private string CovertKeyStateToString(int keyState)
        {
            string keyString = "None";

            // Which button was pressed?
            if ((keyState & 1) == 1)
            {
                keyString = "Left";
            }
            else if ((keyState & 2) == 2)
            {
                keyString = "Right";
            }
            else if ((keyState & 16) == 16)
            {
                keyString = "Middle";
            }

            // Are one or more modifier keys also pressed?
            if ((keyState & 4) == 4)
            {
                keyString += "+SHIFT";
            }

            if ((keyState & 8) == 8)
            {
                keyString += "+CTRL";
            }

            if ((keyState & 32) == 32)
            {
                keyString += "+ALT";
            }

            return keyString;
        }

        private void TextBoxInput_Enter(object sender, EventArgs e)
        {
            if (CheckBoxFocus.Checked)
            {
                DisplayLine("Enter event");
            }
        }

        private void TextBoxInput_Leave(object sender, EventArgs e)
        {
            if (CheckBoxFocus.Checked)
            {
                DisplayLine("Leave event");
            }
        }

        private void TextBoxInput_GotFocus(object sender, EventArgs e)
        {
            if (CheckBoxFocus.Checked)
            {
                DisplayLine("GotFocus event");
            }
        }

        private void TextBoxInput_LostFocus(object sender, EventArgs e)
        {
            if (CheckBoxFocus.Checked)
            {
                DisplayLine("LostFocus event");
            }
        }

        private void TextBoxInput_Validated(object sender, EventArgs e)
        {
            if (CheckBoxValidation.Checked)
            {
                DisplayLine("Validated event");
            }
        }

        private void TextBoxInput_Validating(
            object sender, CancelEventArgs e)
        {
            if (CheckBoxValidation.Checked)
            {
                DisplayLine("Validating event");
            }
        }

        private void CheckBoxToggleAll_CheckedChanged(
            object sender, EventArgs e)
        {
            if (sender is CheckBox)
            {
                CheckAllChildCheckBoxes(this, ((CheckBox)sender).Checked);
            }
        }

        private void CheckBoxMouse_CheckedChanged(
            object sender, EventArgs e)
        {
            ConfigureCheckBoxSettings();
        }

        private void CheckBoxMouseDrag_CheckedChanged(
            object sender, EventArgs e)
        {
            ConfigureCheckBoxSettings();
        }

        private void CheckBoxKeyboard_CheckedChanged(
            object sender, EventArgs e)
        {
            ConfigureCheckBoxSettings();
        }

        private void CheckBoxMouseMove_CheckedChanged(
            object sender, EventArgs e)
        {
            ConfigureCheckBoxSettings();
        }

        // Reconcile dependencies between the check box 
        // selection choices. 
        private void ConfigureCheckBoxSettings()
        {
            // CheckBoxMouse is a top-level check box.
            if (!CheckBoxMouse.Checked)
            {
                CheckBoxMouseEnter.Enabled = false;
                CheckBoxMouseMove.Enabled = false;
                CheckBoxMouseDrag.Enabled = false;
                CheckBoxMouseDragOver.Enabled = false;
                CheckBoxMousePoints.Enabled = false;
            }
            else
            {
                CheckBoxMouseEnter.Enabled = true;
                CheckBoxMouseMove.Enabled = true;
                CheckBoxMouseDrag.Enabled = true;
                CheckBoxMousePoints.Enabled = true;

                // Enable children depending on the state of the parent.
                if (!CheckBoxMouseDrag.Checked)
                {
                    CheckBoxMouseDragOver.Enabled = false;
                }
                else
                {
                    CheckBoxMouseDragOver.Enabled = true;
                }
            }

            if (!CheckBoxKeyboard.Checked)
            {
                CheckBoxKeyUpDown.Enabled = false;
            }
            else
            {
                CheckBoxKeyUpDown.Enabled = true;
            }
        }

        private void LinkLabelDrag_MouseDown(object sender,
            MouseEventArgs e)
        {
            string data = "Sample Data";
            LinkLabelDrag.DoDragDrop(data, DragDropEffects.All);
        }

        private void LinkLabelDrag_GiveFeedback(object sender,
            GiveFeedbackEventArgs e)
        {
            if ((e.Effect & DragDropEffects.Copy) ==
                DragDropEffects.Copy)
            {
                LinkLabelDrag.Cursor = Cursors.HSplit;
            }
            else
            {
                LinkLabelDrag.Cursor = Cursors.Default;
            }
        }
    }
}
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;

namespace UserInputWalkthrough
{
    public ref class Form1 : public Form
    {
        Label^ lblEvent;
        Label^ lblInput;

        TextBox^ TextBoxOutput;
        TextBox^ TextBoxInput;
        GroupBox^ GroupBoxEvents;
        Button^ ButtonClear;
        LinkLabel^ LinkLabelDrag;

        CheckBox^ CheckBoxToggleAll;
        CheckBox^ CheckBoxMouse;
        CheckBox^ CheckBoxMouseEnter;
        CheckBox^ CheckBoxMouseMove;
        CheckBox^ CheckBoxMousePoints;
        CheckBox^ CheckBoxMouseDrag;
        CheckBox^ CheckBoxMouseDragOver;
        CheckBox^ CheckBoxKeyboard;
        CheckBox^ CheckBoxKeyUpDown;
        CheckBox^ CheckBoxFocus;
        CheckBox^ CheckBoxValidation;

    public:
        Form1() : Form()
        {
            this->Load += gcnew EventHandler(this, &Form1::Form1_Load);

            lblEvent = gcnew Label();
            lblInput = gcnew Label();

            TextBoxOutput = gcnew TextBox();
            TextBoxInput = gcnew TextBox();
            GroupBoxEvents = gcnew GroupBox();
            ButtonClear = gcnew Button();
            LinkLabelDrag = gcnew LinkLabel();

            CheckBoxToggleAll = gcnew CheckBox();
            CheckBoxMouse = gcnew CheckBox();
            CheckBoxMouseEnter = gcnew CheckBox();
            CheckBoxMouseMove = gcnew CheckBox();
            CheckBoxMousePoints = gcnew CheckBox();
            CheckBoxMouseDrag = gcnew CheckBox();
            CheckBoxMouseDragOver = gcnew CheckBox();
            CheckBoxKeyboard = gcnew CheckBox();
            CheckBoxKeyUpDown = gcnew CheckBox();
            CheckBoxFocus = gcnew CheckBox();
            CheckBoxValidation = gcnew CheckBox();
        }

    private:
        void Form1_Load(Object^ sender, EventArgs^ e)
        {
            this->GroupBoxEvents->SuspendLayout();
            this->SuspendLayout();

            lblEvent->Location = Point(232, 12);
            lblEvent->Size = System::Drawing::Size(98, 14);
            lblEvent->AutoSize = true;
            lblEvent->Text = "Generated Events:";

            lblInput->Location = Point(13, 12);
            lblInput->Size = System::Drawing::Size(95, 14);
            lblInput->AutoSize = true;
            lblInput->Text = "User Input Target:";

            TextBoxInput->Location = Point(13, 34);
            TextBoxInput->Size = System::Drawing::Size(200, 200);
            TextBoxInput->AllowDrop = true;
            TextBoxInput->AutoSize = false;
            TextBoxInput->Cursor = Cursors::Cross;
            TextBoxInput->Multiline = true;
            TextBoxInput->TabIndex = 1;

            LinkLabelDrag->AllowDrop = true;
            LinkLabelDrag->AutoSize = true;
            LinkLabelDrag->Location = Point(13, 240);
            LinkLabelDrag->Size = System::Drawing::Size(175, 14);
            LinkLabelDrag->TabIndex = 2;
            LinkLabelDrag->TabStop = true;
            LinkLabelDrag->Text = "Click here to use as a drag source";
            LinkLabelDrag->Links->Add(gcnew LinkLabel::Link(0,
                LinkLabelDrag->Text->Length));

            GroupBoxEvents->Location = Point(13, 281);
            GroupBoxEvents->Size = System::Drawing::Size(200, 302);
            GroupBoxEvents->Text = "Event Filter:";
            GroupBoxEvents->TabStop = true;
            GroupBoxEvents->TabIndex = 3;
            GroupBoxEvents->Controls->Add(CheckBoxMouseEnter);
            GroupBoxEvents->Controls->Add(CheckBoxToggleAll);
            GroupBoxEvents->Controls->Add(CheckBoxMousePoints);
            GroupBoxEvents->Controls->Add(CheckBoxKeyUpDown);
            GroupBoxEvents->Controls->Add(CheckBoxMouseDragOver);
            GroupBoxEvents->Controls->Add(CheckBoxMouseDrag);
            GroupBoxEvents->Controls->Add(CheckBoxValidation);
            GroupBoxEvents->Controls->Add(CheckBoxMouseMove);
            GroupBoxEvents->Controls->Add(CheckBoxFocus);
            GroupBoxEvents->Controls->Add(CheckBoxKeyboard);
            GroupBoxEvents->Controls->Add(CheckBoxMouse);

            CheckBoxToggleAll->AutoSize = true;
            CheckBoxToggleAll->Location = Point(7, 20);
            CheckBoxToggleAll->Size = System::Drawing::Size(122, 17);
            CheckBoxToggleAll->TabIndex = 4;
            CheckBoxToggleAll->Text = "Toggle All Events";

            CheckBoxMouse->AutoSize = true;
            CheckBoxMouse->Location = Point(7, 45);
            CheckBoxMouse->Size = System::Drawing::Size(137, 17);
            CheckBoxMouse->TabIndex = 5;
            CheckBoxMouse->Text = "Mouse and Click Events";

            CheckBoxMouseEnter->AutoSize = true;
            CheckBoxMouseEnter->Location = Point(26, 69);
            CheckBoxMouseEnter->Margin = 
                System::Windows::Forms::Padding(3, 3, 3, 1);
            CheckBoxMouseEnter->Size = System::Drawing::Size(151, 17);
            CheckBoxMouseEnter->TabIndex = 6;
            CheckBoxMouseEnter->Text = "Mouse Enter/Hover/Leave";

            CheckBoxMouseMove->AutoSize = true;
            CheckBoxMouseMove->Location = Point(26, 89);
            CheckBoxMouseMove->Margin = 
                System::Windows::Forms::Padding(3, 2, 3, 3);
            CheckBoxMouseMove->Size = System::Drawing::Size(120, 17);
            CheckBoxMouseMove->TabIndex = 7;
            CheckBoxMouseMove->Text = "Mouse Move Events";

            CheckBoxMousePoints->AutoSize = true;
            CheckBoxMousePoints->Location = Point(26, 112);
            CheckBoxMousePoints->Margin =
                System::Windows::Forms::Padding(3, 3, 3, 1);
            CheckBoxMousePoints->Size = System::Drawing::Size(141, 17);
            CheckBoxMousePoints->TabIndex = 8;
            CheckBoxMousePoints->Text = "Draw Mouse Points";

            CheckBoxMouseDrag->AutoSize = true;
            CheckBoxMouseDrag->Location = Point(26, 135);
            CheckBoxMouseDrag->Margin = 
                System::Windows::Forms::Padding(3, 1, 3, 3);
            CheckBoxMouseDrag->Size = System::Drawing::Size(151, 17);
            CheckBoxMouseDrag->TabIndex = 9;
            CheckBoxMouseDrag->Text = "Mouse Drag && Drop Events";

            CheckBoxMouseDragOver->AutoSize = true;
            CheckBoxMouseDragOver->Location = Point(44, 159);
            CheckBoxMouseDragOver->Size = System::Drawing::Size(142, 17);
            CheckBoxMouseDragOver->TabIndex = 10;
            CheckBoxMouseDragOver->Text = "Mouse Drag Over Events";

            CheckBoxKeyboard->AutoSize = true;
            CheckBoxKeyboard->Location = Point(8, 184);
            CheckBoxKeyboard->Size = System::Drawing::Size(103, 17);
            CheckBoxKeyboard->TabIndex = 11;
            CheckBoxKeyboard->Text = "Keyboard Events";

            CheckBoxKeyUpDown->AutoSize = true;
            CheckBoxKeyUpDown->Location = Point(26, 207);
            CheckBoxKeyUpDown->Margin = 
                System::Windows::Forms::Padding(3, 3, 3, 1);
            CheckBoxKeyUpDown->Size = System::Drawing::Size(133, 17);
            CheckBoxKeyUpDown->TabIndex = 12;
            CheckBoxKeyUpDown->Text = "Key Up && Down Events";

            CheckBoxFocus->AutoSize = true;
            CheckBoxFocus->Location = Point(8, 233);
            CheckBoxFocus->Margin = 
                System::Windows::Forms::Padding(3, 2, 3, 3);
            CheckBoxFocus->Size = System::Drawing::Size(146, 17);
            CheckBoxFocus->TabIndex = 13;
            CheckBoxFocus->Text = "Focus && Activation Events";

            CheckBoxValidation->AutoSize = true;
            CheckBoxValidation->Location = Point(8, 257);
            CheckBoxValidation->Size = System::Drawing::Size(104, 17);
            CheckBoxValidation->TabIndex = 14;
            CheckBoxValidation->Text = "Validation Events";

            TextBoxOutput->Location = Point(232, 34);
            TextBoxOutput->Size = System::Drawing::Size(308, 510);
            TextBoxOutput->Multiline = true;
            TextBoxOutput->CausesValidation = false;
            TextBoxOutput->ReadOnly = true;
            TextBoxOutput->ScrollBars = ScrollBars::Vertical;
            TextBoxOutput->TabIndex = 15;
            TextBoxOutput->WordWrap = false;

            ButtonClear->Location = Point(232, 560);
            ButtonClear->Size = System::Drawing::Size(308, 23);
            ButtonClear->TabIndex = 16;
            ButtonClear->Text = "Clear Event List";

            this->ClientSize = System::Drawing::Size(552, 595);
            this->Controls->Add(LinkLabelDrag);
            this->Controls->Add(ButtonClear);
            this->Controls->Add(GroupBoxEvents);
            this->Controls->Add(lblEvent);
            this->Controls->Add(lblInput);
            this->Controls->Add(TextBoxInput);
            this->Controls->Add(TextBoxOutput);
            this->Text = "User Input Events";

            ButtonClear->Click +=
                gcnew EventHandler(this, &Form1::ButtonClear_Click);
            TextBoxInput->KeyDown +=
                gcnew KeyEventHandler(this, &Form1::TextBoxInput_KeyDown);
            TextBoxInput->KeyPress +=
                gcnew KeyPressEventHandler(this, 
                    &Form1::TextBoxInput_KeyPress);
            TextBoxInput->KeyUp +=
                gcnew KeyEventHandler(this, &Form1::TextBoxInput_KeyUp);
            TextBoxInput->Click +=
                gcnew EventHandler(this, &Form1::TextBoxInput_Click);
            TextBoxInput->DoubleClick +=
                gcnew EventHandler(this, &Form1::TextBoxInput_DoubleClick);
            TextBoxInput->MouseClick +=
                gcnew MouseEventHandler(this, &Form1::TextBoxInput_MouseClick);
            TextBoxInput->MouseDoubleClick +=
                gcnew MouseEventHandler(this, 
                    &Form1::TextBoxInput_MouseDoubleClick);
            TextBoxInput->MouseDown +=
                gcnew MouseEventHandler(this, &Form1::TextBoxInput_MouseDown);
            TextBoxInput->MouseUp +=
                gcnew MouseEventHandler(this, &Form1::TextBoxInput_MouseUp);
            TextBoxInput->MouseEnter +=
                gcnew EventHandler(this, &Form1::TextBoxInput_MouseEnter);
            TextBoxInput->MouseHover +=
                gcnew EventHandler(this, &Form1::TextBoxInput_MouseHover);
            TextBoxInput->MouseLeave +=
                gcnew EventHandler(this, &Form1::TextBoxInput_MouseLeave);
            TextBoxInput->MouseWheel +=
                gcnew MouseEventHandler(this, &Form1::TextBoxInput_MouseWheel);
            TextBoxInput->MouseMove +=
                gcnew MouseEventHandler(this, &Form1::TextBoxInput_MouseMove);
            TextBoxInput->MouseCaptureChanged +=
                gcnew EventHandler(this, 
                    &Form1::TextBoxInput_MouseCaptureChanged);
            TextBoxInput->DragEnter +=
                gcnew DragEventHandler(this, &Form1::TextBoxInput_DragEnter);
            TextBoxInput->DragDrop +=
                gcnew DragEventHandler(this, &Form1::TextBoxInput_DragDrop);
            TextBoxInput->DragOver +=
                gcnew DragEventHandler(this, &Form1::TextBoxInput_DragOver);
            TextBoxInput->DragLeave +=
                gcnew EventHandler(this, &Form1::TextBoxInput_DragLeave);
            TextBoxInput->Enter +=
                gcnew EventHandler(this, &Form1::TextBoxInput_Enter);
            TextBoxInput->Leave +=
                gcnew EventHandler(this, &Form1::TextBoxInput_Leave);
            TextBoxInput->GotFocus +=
                gcnew EventHandler(this, &Form1::TextBoxInput_GotFocus);
            TextBoxInput->LostFocus +=
                gcnew EventHandler(this, &Form1::TextBoxInput_LostFocus);
            TextBoxInput->Validated +=
                gcnew EventHandler(this, &Form1::TextBoxInput_Validated);
            TextBoxInput->Validating +=
                gcnew CancelEventHandler(this,
                    &Form1::TextBoxInput_Validating);

            LinkLabelDrag->MouseDown +=
                gcnew MouseEventHandler(this, &Form1::LinkLabelDrag_MouseDown);
            LinkLabelDrag->GiveFeedback +=
                gcnew GiveFeedbackEventHandler(this,
                    &Form1::LinkLabelDrag_GiveFeedback);

            CheckBoxToggleAll->CheckedChanged +=
                gcnew EventHandler(this, 
                    &Form1::CheckBoxToggleAll_CheckedChanged);
            CheckBoxMouse->CheckedChanged +=
                gcnew EventHandler(this, &Form1::CheckBoxMouse_CheckedChanged);
            CheckBoxMouseDrag->CheckedChanged +=
                gcnew EventHandler(this,
                    &Form1::CheckBoxMouseDrag_CheckedChanged);
            CheckBoxMouseEnter->CheckedChanged +=
                gcnew EventHandler(this,
                    &Form1::CheckBoxMouseMove_CheckedChanged);
            CheckBoxMouseMove->CheckedChanged +=
                gcnew EventHandler(this,
                    &Form1::CheckBoxMouseMove_CheckedChanged);
            CheckBoxKeyboard->CheckedChanged +=
                gcnew EventHandler(this,
                    &Form1::CheckBoxKeyboard_CheckedChanged);

            this->GroupBoxEvents->ResumeLayout(false);
            this->GroupBoxEvents->PerformLayout();
            this->ResumeLayout(false);
            this->PerformLayout();
            CheckAllChildCheckBoxes(this, true);
        }

        // Recursively search the form for all contained checkboxes and
        // initially check them
    private:
        void CheckAllChildCheckBoxes(Control^ parent, bool value)
        {
            CheckBox^ box;
            for each (Control^ currentControl in parent->Controls)
            {
                if (dynamic_cast<CheckBox^>(currentControl))
                {
                    box = (CheckBox^)currentControl;
                    box->Checked = value;
                }

                // Recurse if control contains other controls
                if (currentControl->Controls->Count > 0)
                {
                    CheckAllChildCheckBoxes(currentControl, value);
                }
            }
        }

        // All-purpose method for displaying a line of text in the
        // text boxe.
    private:
        void DisplayLine(String^ line)
        {
            TextBoxOutput->AppendText(line);
            TextBoxOutput->AppendText(Environment::NewLine);
        }

        // Click event handler for the button that clears the text box.
    private:
        void ButtonClear_Click(Object^ sender, EventArgs^ e)
        {
            TextBoxOutput->Invalidate();
            TextBoxOutput->Clear();
        }

    private:
        void TextBoxInput_KeyDown(Object^ sender, KeyEventArgs^ e)
        {
            if (CheckBoxKeyUpDown->Checked)
            {
                DisplayLine("KeyDown: " + e->KeyData.ToString());
            }
        }

    private:
        void TextBoxInput_KeyUp(Object^ sender, KeyEventArgs^ e)
        {
            if (CheckBoxKeyUpDown->Checked)
            {
                DisplayLine("KeyUp: " + e->KeyData.ToString());
            }
        }

    private:
        void TextBoxInput_KeyPress(Object^ sender,
            KeyPressEventArgs^ e)
        {
            if (CheckBoxKeyboard->Checked)
            {
                if (Char::IsWhiteSpace(e->KeyChar))
                {
                    DisplayLine("KeyPress: WS");
                }
                else
                {
                    DisplayLine("KeyPress: " + e->KeyChar.ToString());
                }
            }
        }

    private:
        void TextBoxInput_Click(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxMouse->Checked)
            {
                DisplayLine("Click event");
            }
        }

    private:
        void TextBoxInput_DoubleClick(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxMouse->Checked)
            {
                DisplayLine("DoubleClick event");
            }
        }

    private:
        void TextBoxInput_MouseClick(Object^ sender, MouseEventArgs^ e)
        {
            if (CheckBoxMouse->Checked)
            {
                DisplayLine("MouseClick: " + e->Button.ToString() +
                    " " + e->Location.ToString());
            }
        }

    private:
        void TextBoxInput_MouseDoubleClick(Object^ sender,
            MouseEventArgs^ e)
        {
            if (CheckBoxMouse->Checked)
            {
                DisplayLine("MouseDoubleClick: " + e->Button.ToString() +
                    " " + e->Location.ToString());
            }
        }

    private:
        void TextBoxInput_MouseDown(Object^ sender,
            MouseEventArgs^ e)
        {
            if (CheckBoxMouse->Checked)
            {
                DisplayLine("MouseDown: " + e->Button.ToString() +
                    " " + e->Location.ToString());
            }
        }

    private:
        void TextBoxInput_MouseUp(Object^ sender,
            MouseEventArgs^ e)
        {
            if (CheckBoxMouse->Checked)
            {
                DisplayLine("MouseUp: " + e->Button.ToString() +
                    " " + e->Location.ToString());
            }

            // The TextBox control was designed to change focus only on
            // the primary click, so force focus to avoid user confusion.
            if (!TextBoxInput->Focused)
            {
                TextBoxInput->Focus();
            }
        }

    private:
        void TextBoxInput_MouseEnter(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxMouseEnter->Checked)
            {
                DisplayLine("MouseEnter event");
            }
        }

    private:
        void TextBoxInput_MouseHover(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxMouseEnter->Checked)
            {
                DisplayLine("MouseHover event");
            }
        }

    private:
        void TextBoxInput_MouseLeave(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxMouseEnter->Checked)
            {
                DisplayLine("MouseLeave event");
            }
        }

    private:
        void TextBoxInput_MouseWheel(Object^ sender,
            MouseEventArgs^ e)
        {
            if (CheckBoxMouse->Checked)
            {
                DisplayLine("MouseWheel: " + e->Delta.ToString() +
                    " detents at " + e->Location.ToString());
            }
        }

    private:
        void TextBoxInput_MouseMove(Object^ sender,
            MouseEventArgs^ e)
        {
            if (CheckBoxMouseMove->Checked)
            {
                DisplayLine("MouseMove: " + e->Button.ToString() + " " +
                    e->Location.ToString());
            }

            if (CheckBoxMousePoints->Checked)
            {
                Graphics^ g = TextBoxInput->CreateGraphics();
                g->FillRectangle(Brushes::Black, e->Location.X,
                    e->Location.Y, 1, 1);
                delete g;
            }
        }

    private:
        void TextBoxInput_MouseCaptureChanged(Object^ sender,
            EventArgs^ e)
        {
            if (CheckBoxMouseDrag->Checked)
            {
                DisplayLine("MouseCaptureChanged event");
            }
        }

    private:
        void TextBoxInput_DragEnter(Object^ sender, DragEventArgs^ e)
        {
            if (CheckBoxMouseDrag->Checked)
            {
                Point^ pt = gcnew Point(e->X, e->Y);
                DisplayLine("DragEnter: " +
                    CovertKeyStateToString(e->KeyState)
                    + " at " + pt->ToString());
            }
        }

    private:
        void TextBoxInput_DragDrop(Object^ sender, DragEventArgs^ e)
        {
            if (CheckBoxMouseDrag->Checked)
            {
                Point^ pt = gcnew Point(e->X, e->Y);
                DisplayLine("DragDrop: " +
                    CovertKeyStateToString(e->KeyState)
                    + " at " + pt->ToString());
            }
        }

    private:
        void TextBoxInput_DragOver(Object^ sender, DragEventArgs^ e)
        {
            if (CheckBoxMouseDragOver->Checked)
            {
                Point^ pt = gcnew Point(e->X, e->Y);
                DisplayLine("DragOver: " +
                    CovertKeyStateToString(e->KeyState)
                    + " at " + pt->ToString());
            }

            // Allow if drop data is of type string.
            if (!e->Data->GetDataPresent(String::typeid))
            {
                e->Effect = DragDropEffects::None;
            }
            else
            {
                e->Effect = DragDropEffects::Copy;
            }
        }

    private:
        void TextBoxInput_DragLeave(Object^ sender,
            EventArgs^ e)
        {
            if (CheckBoxMouseDrag->Checked)
            {
                DisplayLine("DragLeave event");
            }
        }

    private:
        static String^ CovertKeyStateToString(int keyState)
        {
            String^ keyString = "None";

            // Which button was pressed?
            if ((keyState & 1) == 1)
            {
                keyString = "Left";
            }
            else if ((keyState & 2) == 2)
            {
                keyString = "Right";
            }
            else if ((keyState & 16) == 16)
            {
                keyString = "Middle";
            }

            // Are one or more modifier keys also pressed?
            if ((keyState & 4) == 4)
            {
                keyString += "+SHIFT";
            }

            if ((keyState & 8) == 8)
            {
                keyString += "+CTRL";
            }

            if ((keyState & 32) == 32)
            {
                keyString += "+ALT";
            }

            return keyString;
        }

    private:
        void TextBoxInput_Enter(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxFocus->Checked)
            {
                DisplayLine("Enter event");
            }
        }

    private:
        void TextBoxInput_Leave(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxFocus->Checked)
            {
                DisplayLine("Leave event");
            }
        }

    private:
        void TextBoxInput_GotFocus(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxFocus->Checked)
            {
                DisplayLine("GotFocus event");
            }
        }

    private:
        void TextBoxInput_LostFocus(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxFocus->Checked)
            {
                DisplayLine("LostFocus event");
            }
        }

    private:
        void TextBoxInput_Validated(Object^ sender, EventArgs^ e)
        {
            if (CheckBoxValidation->Checked)
            {
                DisplayLine("Validated event");
            }
        }

    private:
        void TextBoxInput_Validating(
            Object^ sender, CancelEventArgs^ e)
        {
            if (CheckBoxValidation->Checked)
            {
                DisplayLine("Validating event");
            }
        }

    private:
        void CheckBoxToggleAll_CheckedChanged(
            Object^ sender, EventArgs^ e)
        {
            if (dynamic_cast<CheckBox^>(sender))
            {
                CheckAllChildCheckBoxes(this, ((CheckBox^)sender)->Checked);
            }
        }

    private:
        void CheckBoxMouse_CheckedChanged(
            Object^ sender, EventArgs^ e)
        {
            ConfigureCheckBoxSettings();
        }

    private:
        void CheckBoxMouseDrag_CheckedChanged(
            Object^ sender, EventArgs^ e)
        {
            ConfigureCheckBoxSettings();
        }

    private:
        void CheckBoxKeyboard_CheckedChanged(
            Object^ sender, EventArgs^ e)
        {
            ConfigureCheckBoxSettings();
        }

    private:
        void CheckBoxMouseMove_CheckedChanged(
            Object^ sender, EventArgs^ e)
        {
            ConfigureCheckBoxSettings();
        }

        // Reconcile dependencies between the check box
        // selection choices.
    private:
        void ConfigureCheckBoxSettings()
        {
            // CheckBoxMouse is a top-level check box.
            if (!CheckBoxMouse->Checked)
            {
                CheckBoxMouseEnter->Enabled = false;
                CheckBoxMouseMove->Enabled = false;
                CheckBoxMouseDrag->Enabled = false;
                CheckBoxMouseDragOver->Enabled = false;
                CheckBoxMousePoints->Enabled = false;
            }
            else
            {
                CheckBoxMouseEnter->Enabled = true;
                CheckBoxMouseMove->Enabled = true;
                CheckBoxMouseDrag->Enabled = true;
                CheckBoxMousePoints->Enabled = true;

                // Enable children depending on the state of the parent.
                if (!CheckBoxMouseDrag->Checked)
                {
                    CheckBoxMouseDragOver->Enabled = false;
                }
                else
                {
                    CheckBoxMouseDragOver->Enabled = true;
                }
            }

            if (!CheckBoxKeyboard->Checked)
            {
                CheckBoxKeyUpDown->Enabled = false;
            }
            else
            {
                CheckBoxKeyUpDown->Enabled = true;
            }
        }

    private:
        void LinkLabelDrag_MouseDown(Object^ sender, MouseEventArgs^ e)
        {
            String^ data = "Sample Data";
            LinkLabelDrag->DoDragDrop(data, DragDropEffects::All);
        }

    private:
        void LinkLabelDrag_GiveFeedback(Object^ sender,
            GiveFeedbackEventArgs^ e)
        {
            if ((e->Effect & DragDropEffects::Copy) ==
                DragDropEffects::Copy)
            {
                LinkLabelDrag->Cursor = Cursors::HSplit;
            }
            else
            {
                LinkLabelDrag->Cursor = Cursors::Default;
            }
        }
    };
}

[STAThread]
int main()
{
    Application::EnableVisualStyles();
    Application::Run(gcnew UserInputWalkthrough::Form1());
}

Compilando o código

Este exemplo requer:

  • Referências para as montagens (assemblys) do System, System.Drawing e System.Windows.Forms.

Para obter informações sobre como criar este exemplo a partir da linha de comando para Visual Basic ou Visual C#, consulte Compilando a partir da linha de comando (Visual Basic) ou Comando -<>>linha criando com CSC. exe. Você também pode construir este exemplo no Visual Studio colando o código em um novo projeto. Para obter mais informações, consulte Como: Compilar e executar um exemplo de código Windows Forms concluída usando Visual Studio e Como: Compilar e executar um exemplo de código Windows Forms concluída usando Visual Studio e Como: Compilar e executar um exemplo de código Windows Forms concluída usando Visual Studio e Como: Compilar e executar um exemplo de código de formulários Windows concluída usando o Visual Studio e Como: Compilar e executar um exemplo de código de formulários de Windows completa usando Visual Studio e Como compilar e executar um exemplo de código dos Windows Forms concluído usando Visual Studio.

Consulte também

Outros recursos

Entrada do Usuário nos Formulários do Windows