Reputation: 31
i have a Button with a click event in the background. Can i use this method in another timer triggered event?
private void bt_Disconnect_Click(object sender, RoutedEventArgs e)
{
if(ser.disconnect())
{
img_Ampel.Source = ampeln[0];
bt_Connect.IsEnabled = true;
bt_Disconnect.IsEnabled = false;
}
}
Thats the click event. The following snippet is the timer trigger.
private void polling_tick(object sender, EventArgs e)
{
if (!serial_port.IsOpen)
{
mw.Show("Die Verbindung ist abgebrochen!", "Bitte schließen Sie das Gerät wieder an uns klicken Sie auf Verbinden.");
polling.Stop();
MainWindow.bt_Disconnect_Click();
}
}
Upvotes: 0
Views: 553
Reputation: 125312
If you really want to to raise the Click
event of a button you can use PerformClick()
method of button.
yourbutton.PerformClick();
This method can be called to raise the Click event.
Upvotes: 1
Reputation: 37790
Separate logic and event handling.
The simplest way is to extract method:
private void DoSmth()
{
if(ser.disconnect())
{
img_Ampel.Source = ampeln[0];
bt_Connect.IsEnabled = true;
bt_Disconnect.IsEnabled = false;
}
}
private void bt_Disconnect_Click(object sender, RoutedEventArgs e)
{
DoSmth();
}
private void polling_tick(object sender, EventArgs e)
{
if (!serial_port.IsOpen)
{
mw.Show("...");
polling.Stop();
DoSmth();
}
}
Upvotes: 2