HelpEventHandler 委托

表示将要处理 ControlHelpRequested 事件的方法。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Delegate Sub HelpEventHandler ( _
    sender As Object, _
    hlpevent As HelpEventArgs _
)
用法
Dim instance As New HelpEventHandler(AddressOf HandlerMethod)
public delegate void HelpEventHandler (
    Object sender,
    HelpEventArgs hlpevent
)
public delegate void HelpEventHandler (
    Object^ sender, 
    HelpEventArgs^ hlpevent
)
/** @delegate */
public delegate void HelpEventHandler (
    Object sender, 
    HelpEventArgs hlpevent
)
JScript 支持使用委托,但不支持进行新的声明。

参数

  • sender
    事件源。

备注

当创建 HelpEventHandler 委托时,将标识处理事件的方法。若要使该事件与事件处理程序相关联,请将该委托的一个实例添加到事件中。除非移除了该委托,否则每当发生该事件时就调用事件处理程序。有关用委托处理事件的更多信息,请参见 事件和委托

示例

下面的示例演示 HelpRequested 事件的处理以便在包含四个地址字段的窗体上显示自定义帮助内容。焦点位于一个地址字段时按 F1 键,或使用上下文相关的“帮助”按钮并单击地址字段上的“帮助”光标,将引发 HelpRequested 事件。将 Handled 属性设置为真以指示 HelpRequested 事件已处理过。该示例还演示了在 Control.Tag 属性中存储帮助文本。

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

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents addressTextBox As System.Windows.Forms.TextBox
    Private WithEvents label2 As System.Windows.Forms.Label
    Private WithEvents cityTextBox As System.Windows.Forms.TextBox
    Private WithEvents label3 As System.Windows.Forms.Label
    Private WithEvents stateTextBox As System.Windows.Forms.TextBox
    Private WithEvents zipTextBox As System.Windows.Forms.TextBox
    Private WithEvents helpLabel As System.Windows.Forms.Label

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

    Public Sub New()
        Me.addressTextBox = New System.Windows.Forms.TextBox
        Me.helpLabel = New System.Windows.Forms.Label
        Me.label2 = New System.Windows.Forms.Label
        Me.cityTextBox = New System.Windows.Forms.TextBox
        Me.label3 = New System.Windows.Forms.Label
        Me.stateTextBox = New System.Windows.Forms.TextBox
        Me.zipTextBox = New System.Windows.Forms.TextBox

        ' Help Label
        Me.helpLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.helpLabel.Location = New System.Drawing.Point(8, 80)
        Me.helpLabel.Size = New System.Drawing.Size(272, 72)
        Me.helpLabel.Text = "Click on any control to give it focus, and then " & _
            "press F1 to display help for that" + " control.  Alternately, you can " & _
            "click the help button at the top of the dialog and then click on a control."

        ' Address Label
        Me.label2.Location = New System.Drawing.Point(16, 8)
        Me.label2.Size = New System.Drawing.Size(100, 16)
        Me.label2.Text = "Address:"

        ' Comma Label
        Me.label3.Location = New System.Drawing.Point(136, 56)
        Me.label3.Size = New System.Drawing.Size(16, 16)
        Me.label3.Text = ","

        ' Address TextBox
        Me.addressTextBox.Location = New System.Drawing.Point(16, 24)
        Me.addressTextBox.Size = New System.Drawing.Size(264, 20)
        Me.addressTextBox.TabIndex = 0
        Me.addressTextBox.Tag = "Enter the stree address in this text box."
        Me.addressTextBox.Text = ""

        ' City TextBox
        Me.cityTextBox.Location = New System.Drawing.Point(16, 48)
        Me.cityTextBox.Size = New System.Drawing.Size(120, 20)
        Me.cityTextBox.TabIndex = 3
        Me.cityTextBox.Tag = "Enter the city here."
        Me.cityTextBox.Text = ""

        ' State TextBox
        Me.stateTextBox.Location = New System.Drawing.Point(152, 48)
        Me.stateTextBox.MaxLength = 2
        Me.stateTextBox.Size = New System.Drawing.Size(32, 20)
        Me.stateTextBox.TabIndex = 5
        Me.stateTextBox.Tag = "Enter the state in this text box."
        Me.stateTextBox.Text = ""

        ' Zip TextBox
        Me.zipTextBox.Location = New System.Drawing.Point(192, 48)
        Me.zipTextBox.Size = New System.Drawing.Size(88, 20)
        Me.zipTextBox.TabIndex = 6
        Me.zipTextBox.Tag = "Enter the zip code here."
        Me.zipTextBox.Text = ""

        ' Set up how the form should be displayed and add the controls to the form.
        Me.ClientSize = New System.Drawing.Size(292, 160)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.zipTextBox, _
                                Me.stateTextBox, Me.label3, Me.cityTextBox, _
                                Me.label2, Me.helpLabel, Me.addressTextBox})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.HelpButton = True
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Text = "Help Event Demonstration"
    End Sub 'New

    Private Sub textBox_HelpRequested(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs) Handles addressTextBox.HelpRequested, cityTextBox.HelpRequested, stateTextBox.HelpRequested, zipTextBox.HelpRequested
        ' This event is raised when the F1 key is pressed or the
        ' Help cursor is clicked on any of the address fields.
        ' The Help text for the field is in the control's
        ' Tag property. It is retrieved and displayed in the label.

        Dim requestingControl As Control = CType(sender, Control)
        helpLabel.Text = CStr(requestingControl.Tag)
        hlpevent.Handled = True

    End Sub 'textBox_HelpRequested
End Class 'Form1
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.TextBox addressTextBox;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox cityTextBox;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox stateTextBox;
    private System.Windows.Forms.TextBox zipTextBox;
    private System.Windows.Forms.Label helpLabel;

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

    public Form1()
    {
        this.addressTextBox = new System.Windows.Forms.TextBox();
        this.helpLabel = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.cityTextBox = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.stateTextBox = new System.Windows.Forms.TextBox();
        this.zipTextBox = new System.Windows.Forms.TextBox();

        // Help Label
        this.helpLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.helpLabel.Location = new System.Drawing.Point(8, 80);
        this.helpLabel.Size = new System.Drawing.Size(272, 72);
        this.helpLabel.Text = "Click on any control to give it focus, and then " +
            "press F1 to display help for that control.  Alternately, you can " +
            "click the help button at the top of the dialog and then click on a control.";

        // Address Label
        this.label2.Location = new System.Drawing.Point(16, 8);
        this.label2.Size = new System.Drawing.Size(100, 16);
        this.label2.Text = "Address:";

        // Comma Label
        this.label3.Location = new System.Drawing.Point(136, 56);
        this.label3.Size = new System.Drawing.Size(16, 16);
        this.label3.Text = ",";

        // Address TextBox
        this.addressTextBox.Location = new System.Drawing.Point(16, 24);
        this.addressTextBox.Size = new System.Drawing.Size(264, 20);
        this.addressTextBox.TabIndex = 0;
        this.addressTextBox.Tag = "Enter the street address in this text box.";
        this.addressTextBox.Text = "";
        this.addressTextBox.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.textBox_HelpRequested);

        // City TextBox
        this.cityTextBox.Location = new System.Drawing.Point(16, 48);
        this.cityTextBox.Size = new System.Drawing.Size(120, 20);
        this.cityTextBox.TabIndex = 3;
        this.cityTextBox.Tag = "Enter the city here.";
        this.cityTextBox.Text = "";
        this.cityTextBox.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.textBox_HelpRequested);

        // State TextBox
        this.stateTextBox.Location = new System.Drawing.Point(152, 48);
        this.stateTextBox.MaxLength = 2;
        this.stateTextBox.Size = new System.Drawing.Size(32, 20);
        this.stateTextBox.TabIndex = 5;
        this.stateTextBox.Tag = "Enter the state in this text box.";
        this.stateTextBox.Text = "";
        this.stateTextBox.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.textBox_HelpRequested);

        // Zip TextBox
        this.zipTextBox.Location = new System.Drawing.Point(192, 48);
        this.zipTextBox.Name = "zipTextBox";
        this.zipTextBox.Size = new System.Drawing.Size(88, 20);
        this.zipTextBox.TabIndex = 6;
        this.zipTextBox.Tag = "Enter the zip code here.";
        this.zipTextBox.Text = "";
        this.zipTextBox.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.textBox_HelpRequested);

        // Set up how the form should be displayed and add the controls to the form.
        this.ClientSize = new System.Drawing.Size(292, 160);
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.zipTextBox, 
                                this.stateTextBox, this.label3, this.cityTextBox,  
                                this.label2, this.helpLabel, this.addressTextBox});

        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.HelpButton = true;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Text = "Help Event Demonstration";    
    }

    private void textBox_HelpRequested(object sender, System.Windows.Forms.HelpEventArgs hlpevent)
    {
        // This event is raised when the F1 key is pressed or the
        // Help cursor is clicked on any of the address fields.
        // The Help text for the field is in the control's
        // Tag property. It is retrieved and displayed in the label.

        Control requestingControl = (Control)sender;
        helpLabel.Text = (string)requestingControl.Tag;
        hlpevent.Handled = true;
    }
}
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::TextBox^ addressTextBox;
   System::Windows::Forms::Label ^ label2;
   System::Windows::Forms::TextBox^ cityTextBox;
   System::Windows::Forms::Label ^ label3;
   System::Windows::Forms::TextBox^ stateTextBox;
   System::Windows::Forms::TextBox^ zipTextBox;
   System::Windows::Forms::Label ^ helpLabel;

public:
   Form1()
   {
      this->addressTextBox = gcnew System::Windows::Forms::TextBox;
      this->helpLabel = gcnew System::Windows::Forms::Label;
      this->label2 = gcnew System::Windows::Forms::Label;
      this->cityTextBox = gcnew System::Windows::Forms::TextBox;
      this->label3 = gcnew System::Windows::Forms::Label;
      this->stateTextBox = gcnew System::Windows::Forms::TextBox;
      this->zipTextBox = gcnew System::Windows::Forms::TextBox;
      
      // Help Label
      this->helpLabel->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
      this->helpLabel->Location = System::Drawing::Point( 8, 80 );
      this->helpLabel->Size = System::Drawing::Size( 272, 72 );
      this->helpLabel->Text = "Click on any control to give it focus, and then press F1 to display help for that control.  Alternately, you can click the help button at the top of the dialog and then click on a control.";
      
      // Address Label
      this->label2->Location = System::Drawing::Point( 16, 8 );
      this->label2->Size = System::Drawing::Size( 100, 16 );
      this->label2->Text = "Address:";
      
      // Comma Label
      this->label3->Location = System::Drawing::Point( 136, 56 );
      this->label3->Size = System::Drawing::Size( 16, 16 );
      this->label3->Text = ", ";
      
      // Address TextBox
      this->addressTextBox->Location = System::Drawing::Point( 16, 24 );
      this->addressTextBox->Size = System::Drawing::Size( 264, 20 );
      this->addressTextBox->TabIndex = 0;
      this->addressTextBox->Tag = "Enter the street address in this text box.";
      this->addressTextBox->Text = "";
      this->addressTextBox->HelpRequested += gcnew System::Windows::Forms::HelpEventHandler( this, &Form1::textBox_HelpRequested );
      
      // City TextBox
      this->cityTextBox->Location = System::Drawing::Point( 16, 48 );
      this->cityTextBox->Size = System::Drawing::Size( 120, 20 );
      this->cityTextBox->TabIndex = 3;
      this->cityTextBox->Tag = "Enter the city here.";
      this->cityTextBox->Text = "";
      this->cityTextBox->HelpRequested += gcnew System::Windows::Forms::HelpEventHandler( this, &Form1::textBox_HelpRequested );
      
      // State TextBox
      this->stateTextBox->Location = System::Drawing::Point( 152, 48 );
      this->stateTextBox->MaxLength = 2;
      this->stateTextBox->Size = System::Drawing::Size( 32, 20 );
      this->stateTextBox->TabIndex = 5;
      this->stateTextBox->Tag = "Enter the state in this text box.";
      this->stateTextBox->Text = "";
      this->stateTextBox->HelpRequested += gcnew System::Windows::Forms::HelpEventHandler( this, &Form1::textBox_HelpRequested );
      
      // Zip TextBox
      this->zipTextBox->Location = System::Drawing::Point( 192, 48 );
      this->zipTextBox->Name = "zipTextBox";
      this->zipTextBox->Size = System::Drawing::Size( 88, 20 );
      this->zipTextBox->TabIndex = 6;
      this->zipTextBox->Tag = "Enter the zip code here.";
      this->zipTextBox->Text = "";
      this->zipTextBox->HelpRequested += gcnew System::Windows::Forms::HelpEventHandler( this, &Form1::textBox_HelpRequested );
      
      // Set up how the form should be displayed and add the controls to the form.
      this->ClientSize = System::Drawing::Size( 292, 160 );
      array<System::Windows::Forms::Control^>^temp0 = {this->zipTextBox,this->stateTextBox,this->label3,this->cityTextBox,this->label2,this->helpLabel,this->addressTextBox};
      this->Controls->AddRange( temp0 );
      this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
      this->HelpButton = true;
      this->MaximizeBox = false;
      this->MinimizeBox = false;
      this->Text = "Help Event Demonstration";
   }


private:
   void textBox_HelpRequested( Object^ sender, System::Windows::Forms::HelpEventArgs^ hlpevent )
   {
      
      // This event is raised when the F1 key is pressed or the
      // Help cursor is clicked on any of the address fields.
      // The Help text for the field is in the control's
      // Tag property. It is retrieved and displayed in the label.
      Control^ requestingControl = dynamic_cast<Control^>(sender);
      helpLabel->Text = dynamic_cast<String^>(requestingControl->Tag);
      hlpevent->Handled = true;
   }

};


[STAThread]
int main()
{
   Application::Run( gcnew Form1 );
}
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;

public class Form1 extends System.Windows.Forms.Form
{
    private System.Windows.Forms.TextBox addressTextBox;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox cityTextBox;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox stateTextBox;
    private System.Windows.Forms.TextBox zipTextBox;
    private System.Windows.Forms.Label helpLabel;

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main

    public Form1()
    {
        this.addressTextBox = new System.Windows.Forms.TextBox();
        this.helpLabel = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.cityTextBox = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.stateTextBox = new System.Windows.Forms.TextBox();
        this.zipTextBox = new System.Windows.Forms.TextBox();
        // Help Label
        this.helpLabel.set_BorderStyle(
            System.Windows.Forms.BorderStyle.Fixed3D);
        this.helpLabel.set_Location(new System.Drawing.Point(8, 80));
        this.helpLabel.set_Size(new System.Drawing.Size(272, 72));
        this.helpLabel.set_Text("Click on any control to give it focus, " 
            + "and then press F1 to display help for that control.  " 
            + "Alternately, you can click the help button at the top of " 
            + "the dialog and then click on a control.");
        // Address Label
        this.label2.set_Location(new System.Drawing.Point(16, 8));
        this.label2.set_Size(new System.Drawing.Size(100, 16));
        this.label2.set_Text("Address:");
        // Comma Label
        this.label3.set_Location(new System.Drawing.Point(136, 56));
        this.label3.set_Size(new System.Drawing.Size(16, 16));
        this.label3.set_Text(",");
        // Address TextBox
        this.addressTextBox.set_Location(new System.Drawing.Point(16, 24));
        this.addressTextBox.set_Size(new System.Drawing.Size(264, 20));
        this.addressTextBox.set_TabIndex(0);
        this.addressTextBox.set_Tag("Enter the street address " 
            + "in this text box.");
        this.addressTextBox.set_Text("");
        this.addressTextBox.add_HelpRequested(
            new System.Windows.Forms.HelpEventHandler(
            this.TextBox_HelpRequested));
        // City TextBox
        this.cityTextBox.set_Location(new System.Drawing.Point(16, 48));
        this.cityTextBox.set_Size(new System.Drawing.Size(120, 20));
        this.cityTextBox.set_TabIndex(3);
        this.cityTextBox.set_Tag("Enter the city here.");
        this.cityTextBox.set_Text("");
        this.cityTextBox.add_HelpRequested(
            new System.Windows.Forms.HelpEventHandler(
            this.TextBox_HelpRequested));
        // State TextBox
        this.stateTextBox.set_Location(new System.Drawing.Point(152, 48));
        this.stateTextBox.set_MaxLength(2);
        this.stateTextBox.set_Size(new System.Drawing.Size(32, 20));
        this.stateTextBox.set_TabIndex(5);
        this.stateTextBox.set_Tag("Enter the state in this text box.");
        this.stateTextBox.set_Text("");
        this.stateTextBox.add_HelpRequested(
            new System.Windows.Forms.HelpEventHandler(
            this.TextBox_HelpRequested));
        // Zip TextBox
        this.zipTextBox.set_Location(new System.Drawing.Point(192, 48));
        this.zipTextBox.set_Name("zipTextBox");
        this.zipTextBox.set_Size(new System.Drawing.Size(88, 20));
        this.zipTextBox.set_TabIndex(6);
        this.zipTextBox.set_Tag("Enter the zip code here.");
        this.zipTextBox.set_Text("");
        this.zipTextBox.add_HelpRequested(
            new System.Windows.Forms.HelpEventHandler(
            this.TextBox_HelpRequested));
        // Set up how the form should be displayed and add the controls 
        // to the form.
        this.set_ClientSize(new System.Drawing.Size(292, 160));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[] { 
            this.zipTextBox, this.stateTextBox, this.label3, this.cityTextBox, 
            this.label2, this.helpLabel, this.addressTextBox });

        this.set_FormBorderStyle(
            System.Windows.Forms.FormBorderStyle.FixedDialog);
        this.set_HelpButton(true);
        this.set_MaximizeBox(false);
        this.set_MinimizeBox(false);
        this.set_Text("Help Event Demonstration");
    } //Form1

    private void TextBox_HelpRequested(Object sender, 
        System.Windows.Forms.HelpEventArgs hlpEvent)
    {
        // This event is raised when the F1 key is pressed or the
        // Help cursor is clicked on any of the address fields.
        // The Help text for the field is in the control's
        // Tag property. It is retrieved and displayed in the label.
        Control requestingControl = (Control)sender;
        helpLabel.set_Text((String)requestingControl.get_Tag());
        hlpEvent.set_Handled(true);
    } //TextBox_HelpRequested
} //Form1

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

System.Windows.Forms 命名空间
Control.HelpRequested 事件
HelpEventArgs 类