Control.DoubleClick Control.DoubleClick Control.DoubleClick Control.DoubleClick Event

定義

發生於按兩下控制項時。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 

範例

下列程式碼範例會使用DoubleClick的事件ListBox ,將中TextBox列出的ListBox文字檔載入至控制項。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

備註

按兩下是由使用者作業系統的滑鼠設定所決定。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. 例如,如果您有Click的和Click DoubleClick事件Form的事件處理常式,當按兩下表單DoubleClick並呼叫這兩種方法時,就會引發和事件。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.

您必須將的StandardDoubleClickStandardClick ControlStylestrue設定為,才會引發這個事件。You must set the StandardDoubleClick and StandardClick values of ControlStyles to true for this event to be raised. 如果您要繼承現有的 Windows Forms true控制項,這些值可能已經設定為。These values might already be set to true if you are inheriting from existing Windows Forms controls.

注意

TabControl除非MouseDown MouseEnter DoubleClick MouseHover Click MouseUp集合中TabPage至少有一個,否則不會針對類別引發下列事件:、、、、、、 TabControl.TabPagesMouseLeave和。MouseMoveThe 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.

給繼承者的注意事項

繼承自標準的 Windows Forms 控制項,並將StandardClickStandardDoubleClick或值ControlStyles變更true為,可能會導致非預期的行為,如果控制項不支援ClickDoubleClick,則完全不會有任何作用。事件。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 Forms 控制項,以及為了回應指定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
MonthCalendar,MonthCalendar, DateTimePicker,DateTimePicker, RichTextBox,RichTextBox, HScrollBar,HScrollBar, VScrollBar nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone
Button,Button, CheckBox,CheckBox, RadioButton 按一下Click 按一下,按一下Click, Click nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone
ListBox,ListBox, CheckedListBox,CheckedListBox, ComboBox 按一下Click 按一下、DoubleClickClick, DoubleClick nonenone nonenone nonenone nonenone nonenone nonenone nonenone nonenone
TextBox,TextBox, DomainUpDown,DomainUpDown, 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
ProgressBar,ProgressBar, TrackBar 按一下Click 按一下,按一下Click, Click 按一下Click 按一下,按一下Click, Click 按一下Click 按一下,按一下Click, Click 按一下Click 按一下,按一下Click, Click 按一下Click 按一下,按一下Click, Click
Form,Form, DataGrid,DataGrid, Label,Label, LinkLabel,LinkLabel, Panel,Panel, GroupBox,GroupBox, PictureBox,PictureBox, Splitter,Splitter, StatusBar,StatusBar, ToolBar,ToolBar, TabPage,TabPage, ** 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.

適用於

另請參閱