共用方式為


Control.DoubleClick 事件

定義

發生於按兩下控制項時。

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

事件類型

範例

下列程式碼範例會 DoubleClick 使用 的 ListBox 事件,將 列在 控制項中的 ListBoxTextBox 文字檔載入。

   // 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

備註

按兩下是由使用者作業系統的滑鼠設定所決定。 使用者可以設定滑鼠按鍵的按一下之間應間隔多少時間才視為按兩下,而不是兩次按一下。 Click每次按兩下控制項時,就會引發 事件。 例如,如果您有 的 和 事件的事件處理常式 ClickClick 則會在按兩下表單並 DoubleClick 呼叫這兩種方法時引發 和 DoubleClick 事件。 Form 如果按兩下控制項,且該控制項不支援 DoubleClick 事件, Click 則事件可能會引發兩次。

您必須將 的 和 StandardClickControlStyles 設定 StandardDoubleClicktrue ,才能引發此事件。 如果您繼承自現有的Windows Forms控制項,這些值可能已經設定為 true

注意

除非集合 Click 中至少有一個 TabPage :、 DoubleClick 、、 MouseDown 、、 MouseEnterMouseHoverMouseUpMouseLeaveMouseMove ,否則不會針對 TabControl 類別引發下列 TabControl.TabPages 事件。 如果集合中至少有一個 TabPage ,而且使用者與索引標籤控制項的標頭互動 (TabPage 名稱出現在) ,則會 TabControl 引發適當的事件。 不過,如果使用者互動是在索引標籤頁面的工作區內,則會 TabPage 引發適當的事件。

如需處理事件的詳細資訊,請參閱 處理和引發事件

給繼承者的注意事項

繼承自標準Windows Forms控制項,並將 的 或 值變更 StandardClicktrue 可能會導致非預期的行為,如果控制項不支援 ClickDoubleClick 事件,則 ControlStyles 完全沒有作用。 StandardDoubleClick

下表列出Windows Forms控制項,以及引發哪些事件 (ClickDoubleClick) ,以回應指定的滑鼠動作。

控制 滑鼠左鍵按一下 滑鼠左鍵按兩下 按一下滑鼠右鍵 滑鼠右鍵按兩下 按一下滑鼠中間鍵 滑鼠中間鍵按兩下 XButton1 滑鼠按一下 XButton1 滑鼠Double-Click XButton2 滑鼠按一下 XButton2 滑鼠Double-Click
MonthCalendar,

DateTimePicker,

RichTextBox,

HScrollBar,

VScrollBar

Button,

CheckBox,

RadioButton

按一下

按一下,按一下
ListBox,

CheckedListBox,

ComboBox

按一下

按一下,DoubleClick
TextBox,

DomainUpDown,

NumericUpDown

按一下

按一下,DoubleClick
* TreeView,

* ListView

按一下

按一下,DoubleClick 按一下 按一下,DoubleClick
ProgressBar,

TrackBar

按一下

按一下,按一下 按一下 按一下,按一下 按一下 按一下,按一下 按一下 按一下,按一下 按一下 按一下,按一下
Form,

DataGrid,

Label,

LinkLabel,

Panel,

GroupBox,

PictureBox,

Splitter,

StatusBar,

ToolBar,

TabPage,

** TabControl

按一下

按一下,DoubleClick 按一下 按一下,DoubleClick 按一下 按一下,DoubleClick 按一下 按一下,DoubleClick 按一下 按一下,DoubleClick

* 滑鼠指標必須位於子物件上方, (TreeNodeListViewItem) 。

** 在其 TabControl 集合中 TabPages 必須至少有一個 TabPage

適用於

另請參閱