Control.OnDoubleClick 方法
引发 DoubleClick 事件。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Protected Overridable Sub OnDoubleClick ( _
e As EventArgs _
)
用法
Dim e As EventArgs
Me.OnDoubleClick(e)
protected virtual void OnDoubleClick (
EventArgs e
)
protected:
virtual void OnDoubleClick (
EventArgs^ e
)
protected void OnDoubleClick (
EventArgs e
)
protected function OnDoubleClick (
e : EventArgs
)
参数
- e
包含事件数据的 EventArgs。
备注
引发事件时会通过委托调用事件处理程序。有关更多信息,请参见 引发事件。
OnDoubleClick 方法还允许派生类对事件进行处理而不必附加委托。这是在派生类中处理事件的首选技术。
给继承者的说明 在派生类中重写 OnDoubleClick 时,一定要调用基类的 OnDoubleClick 方法,以便已注册的委托对事件进行接收。
示例
下面的代码示例使用 ListBox 的 DoubleClick 事件将 ListBox 中列出的文本文件加载到 TextBox 控件中。
' 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
// 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:
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.get_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.
get_SelectedItem().ToString());
System.IO.TextReader myData = myFile.OpenText();
textBox1.set_Text(myData.ReadToEnd());
myData.Close();
}
}
// Exception is thrown by the OpenText method of the FileInfo class.
catch (System.IO.FileNotFoundException exp) {
MessageBox.Show("The file you specified does not exist.");
}
// Exception is thrown by the ReadToEnd method of the TextReader class.
catch (System.IO.IOException exp) {
MessageBox.Show("There was a problem loading the file into the "
+ "TextBox. Ensure that the file is a valid text file.");
}
} //listBox1_DoubleClick
平台
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