Прочитать на английском

Поделиться через


Control.OnDoubleClick(EventArgs) Метод

Определение

Вызывает событие DoubleClick.

C#
protected virtual void OnDoubleClick(EventArgs e);

Параметры

e
EventArgs

Объект класса EventArgs, содержащий данные события.

Примеры

В следующем примере кода используется DoubleClick событие объекта ListBox для загрузки текстовых файлов, перечисленных ListBox в , в TextBox элемент управления .

C#
// 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.");
    }
}

Комментарии

При возникновении события через делегат вызывается обработчик события. Дополнительные сведения см. в разделе Обработка и вызов событий.

Метод OnDoubleClick также позволяет производным классам обрабатывать событие, не присоединяя делегат. Это предпочтительная методика обработки событий в производном классе.

Примечания для тех, кто наследует этот метод

При переопределении метода OnDoubleClick(EventArgs) в производном классе нужно убедиться, что вызывается метод OnDoubleClick(EventArgs) базового класса, чтобы зарегистрированные делегаты получили событие.

Применяется к

Продукт Версии
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

См. также раздел