Control.DoubleClick Event

Definition

在双击控件时发生。Occurs when the control is double-clicked.

public:
 event EventHandler ^ DoubleClick;
public event EventHandler DoubleClick;
member this.DoubleClick : EventHandler 
Public Custom Event DoubleClick As EventHandler 

Examples

下面的代码示例使用 ListBoxDoubleClick 事件将 ListBox 中列出的文本文件加载到 TextBox 控件中。The following code example uses the DoubleClick event of a ListBox to load text files listed in the ListBox into a TextBox control.

   // This example uses the DoubleClick event of a ListBox to load text files
   // listed in the ListBox into a TextBox control. This example
   // assumes that the ListBox, named listBox1, contains a list of valid file
   // names with path and that this event handler method
   // is connected to the DoublClick event of a ListBox control named listBox1.
   // This example requires code access permission to access files.
private:
   void listBox1_DoubleClick( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Get the name of the file to open from the ListBox.
      String^ file = listBox1->SelectedItem->ToString();
      try
      {
         // Determine if the file exists before loading.
         if ( System::IO::File::Exists( file ) )
         {
            
            // Open the file and use a TextReader to read the contents into the TextBox.
            System::IO::FileInfo^ myFile = gcnew System::IO::FileInfo( listBox1->SelectedItem->ToString() );
            System::IO::TextReader^ myData = myFile->OpenText();
            ;
            textBox1->Text = myData->ReadToEnd();
            myData->Close();
         }
      }
      // Exception is thrown by the OpenText method of the FileInfo class.
      catch ( System::IO::FileNotFoundException^ ) 
      {
         MessageBox::Show( "The file you specified does not exist." );
      }
      // Exception is thrown by the ReadToEnd method of the TextReader class.
      catch ( System::IO::IOException^ ) 
      {
         MessageBox::Show( "There was a problem loading the file into the TextBox. Ensure that the file is a valid text file." );
      }
   }
// This example uses the DoubleClick event of a ListBox to load text files
// listed in the ListBox into a TextBox control. This example
// assumes that the ListBox, named listBox1, contains a list of valid file
// names with path and that this event handler method
// is connected to the DoublClick event of a ListBox control named listBox1.
// This example requires code access permission to access files.
private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
    // Get the name of the file to open from the ListBox.
    String file = listBox1.SelectedItem.ToString();

    try
    {
        // Determine if the file exists before loading.
        if (System.IO.File.Exists(file))
        {
            // Open the file and use a TextReader to read the contents into the TextBox.
            System.IO.FileInfo myFile = new System.IO.FileInfo(listBox1.SelectedItem.ToString());
            System.IO.TextReader myData = myFile.OpenText();;

            textBox1.Text = myData.ReadToEnd();
            myData.Close();
        }
    }
        // Exception is thrown by the OpenText method of the FileInfo class.
    catch(System.IO.FileNotFoundException)
    {
        MessageBox.Show("The file you specified does not exist.");
    }
        // Exception is thrown by the ReadToEnd method of the TextReader class.
    catch(System.IO.IOException)
    {
        MessageBox.Show("There was a problem loading the file into the TextBox. Ensure that the file is a valid text file.");
    }
}
' This example uses the DoubleClick event of a ListBox to load text files  
' listed in the ListBox into a TextBox control. This example
' assumes that the ListBox, named listBox1, contains a list of valid file 
' names with path and that this event handler method
' is connected to the DoublClick event of a ListBox control named listBox1.
' This example requires code access permission to access files.
Private Sub listBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.DoubleClick
    ' Get the name of the file to open from the ListBox.
    Dim file As [String] = listBox1.SelectedItem.ToString()

    Try
        ' Determine if the file exists before loading.
        If System.IO.File.Exists(file) Then
            ' Open the file and use a TextReader to read the contents into the TextBox.
            Dim myFile As New System.IO.FileInfo(listBox1.SelectedItem.ToString())
            Dim myData As System.IO.TextReader = myFile.OpenText()

            textBox1.Text = myData.ReadToEnd()
            myData.Close()
        End If
        ' Exception is thrown by the OpenText method of the FileInfo class.
    Catch
        MessageBox.Show("The file you specified does not exist.")
        ' Exception is thrown by the ReadToEnd method of the TextReader class.
    Catch
     MessageBox.Show("There was a problem loading the file into the TextBox. Ensure that the file is a valid text file.")
    End Try
End Sub

Remarks

双击由用户操作系统的鼠标设置确定。A double-click is determined by the mouse settings of the user's operating system. 用户可以设置两次单击鼠标按钮之间的时间以便将这两次单击认为是双击而不是两次单击。The user can set the time between clicks of a mouse button that should be considered a double-click rather than two clicks. 每次双击控件时都会引发 Click 事件。The Click event is raised every time a control is double-clicked. 例如,如果您具有 FormClickDoubleClick 事件的事件处理程序,则在双击窗体并调用这两个方法时,将引发 ClickDoubleClick 事件。For example, if you have event handlers for the Click and DoubleClick events of a Form, the Click and DoubleClick events are raised when the form is double-clicked and both methods are called. 如果双击控件,而该控件不支持 DoubleClick 事件,则 Click 事件可能会引发两次。If a control is double-clicked and that control does not support the DoubleClick event, the Click event might be raised twice.

必须将 ControlStylesStandardDoubleClickStandardClick 值设置为 "true",才能引发此事件。You must set the StandardDoubleClick and StandardClick values of ControlStyles to true for this event to be raised. 如果要从现有 Windows 窗体控件继承,则这些值可能已设置为 trueThese values might already be set to true if you are inheriting from existing Windows Forms controls.

Note

除非 TabControl.TabPages 集合中至少有一个 TabPage,否则不会为 TabControl 类引发以下事件: ClickDoubleClickMouseDownMouseUpMouseHoverMouseEnterMouseLeaveMouseMoveThe following events are not raised for the TabControl class unless there is at least one TabPage in the TabControl.TabPages collection: Click, DoubleClick, MouseDown, MouseUp, MouseHover, MouseEnter, MouseLeave and MouseMove. 如果集合中至少有一个 TabPage,并且用户与选项卡控件的标头(显示 TabPage 名称)交互,TabControl 将引发相应的事件。If there is at least one TabPage in the collection, and the user interacts with the tab control's header (where the TabPage names appear), the TabControl raises the appropriate event. 但是,如果用户交互位于选项卡页的工作区中,则 TabPage 引发相应的事件。However, if the user interaction is within the client area of the tab page, the TabPage raises the appropriate event.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

Notes to Inheritors

如果控件不支持 trueClick 事件,从标准 Windows 窗体控件继承并将 ControlStylesStandardClickStandardDoubleClick 值更改为 DoubleClick 可能会导致意外的行为或根本不会有任何影响。Inheriting from a standard Windows Forms control and changing the StandardClick or StandardDoubleClick values of ControlStyles to true can cause unexpected behavior or have no effect at all if the control does not support the Click or DoubleClick events.

下表列出 Windows 窗体控件以及为响应指定的鼠标操作而引发的事件(ClickDoubleClick)。The following table lists Windows Forms controls and which event (Click or DoubleClick) is raised in response to the mouse action specified.

控件Control 单击鼠标左键Left Mouse Click 双击鼠标左键Left Mouse Double Click 鼠标右键单击Right Mouse Click 右键单击鼠标右键Right Mouse Double Click 鼠标中键单击Middle Mouse Click 双击鼠标中键Middle Mouse Double Click XButton1 鼠标单击XButton1 Mouse Click XButton1 鼠标双击XButton1 Mouse Double-Click XButton2 鼠标单击XButton2 Mouse Click XButton2 鼠标双击XButton2 Mouse Double-Click
MonthCalendarMonthCalendar, DateTimePickerDateTimePicker, RichTextBoxRichTextBox, HScrollBarHScrollBar, VScrollBar nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone
ButtonButton, CheckBoxCheckBox, RadioButton 单击Click 单击,单击Click, Click nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone
ListBoxListBox, CheckedListBoxCheckedListBox, ComboBox 单击Click 单击,DoubleClickClick, DoubleClick nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone
TextBoxTextBox, DomainUpDownDomainUpDown, NumericUpDown 单击Click 单击,DoubleClickClick, DoubleClick nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone
\* TreeView\* TreeView, \* ListView\* ListView 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick nonenone nonenone nonenone nonenone nonenone nonenone
ProgressBarProgressBar, TrackBar 单击Click 单击,单击Click, Click 单击Click 单击,单击Click, Click 单击Click 单击,单击Click, Click 单击Click 单击,单击Click, Click 单击Click 单击,单击Click, Click
FormForm, DataGridDataGrid, LabelLabel, LinkLabelLinkLabel, PanelPanel, GroupBoxGroupBox, PictureBoxPictureBox, SplitterSplitter, StatusBarStatusBar, ToolBarToolBar, TabPageTabPage, ** TabControl 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick

* 鼠标指针必须位于子对象上(TreeNodeListViewItem)。* The mouse pointer must be over a child object (TreeNode or ListViewItem).

* * TabControlTabPages 集合中必须至少有一个 TabPage** The TabControl must have at least one TabPage in its TabPages collection.

Applies to

See also