Mono gtk.RadioButton Clicked 事件触发两次 [英] Mono gtk.RadioButton Clicked event firing twice
问题描述
我对 Gtk.RadioButton
小部件的 Clicked
事件有问题.下面是示例代码:
I have a problem with the Gtk.RadioButton
widget's Clicked
event.
Here is the example code:
using System;
using Gtk;
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
RadioButton rbt1 = new RadioButton (null, "rbt1");
RadioButton rbt2 = new RadioButton (rbt1, "rbt2");
RadioButton rbt3 = new RadioButton (rbt1, "rbt3");
VBox vbx1 = new VBox ();
vbx1.PackStart (rbt1, false, false, 0);
vbx1.PackStart (rbt2, false, false, 0);
vbx1.PackStart (rbt3, false, false, 0);
this.Add (vbx1);
this.ShowAll ();
rbt1.Clicked+= HandleClicked;
rbt2.Clicked+= HandleClicked1;
rbt3.Clicked+= HandleClicked2;
}
void HandleClicked2 (object sender, EventArgs e)
{
Console.WriteLine ("rbt3.Clicked");
}
void HandleClicked1 (object sender, EventArgs e)
{
Console.WriteLine ("rbt2.Clicked");
}
void HandleClicked (object sender, EventArgs e)
{
Console.WriteLine ("rbt1.Clicked");
}
}
问题是:
当我点击 rbt2 时,输出是:
When i click rbt2, the output is :
rbt1.Clicked
rbt2.点击
rbt1.Clicked
rbt2.Clicked
当我点击 rbt3 时,输出是:
When i click rbt3, the output is :
rbt2.Clicked
rbt3.点击
rbt2.Clicked
rbt3.Clicked
当我点击 rbt1 时,输出是:
When i click rbt1, the output is:
rbt3.Clicked
rbt1.点击
rbt3.Clicked
rbt1.Clicked
但我期望的是,当我点击 rbt* 时,唯一的输出是rbt*.Clicked".
But what I expect is that when I click rbt*, the only output is "rbt*.Clicked".
推荐答案
您的期望是错误的.您应该改为连接到 Toggled
信号并检查按钮是否被激活或停用:
Your expectation is wrong. You should connect to the Toggled
signal instead and check whether the button is being activated or deactivated:
void HandleToggled(object sender, EventArgs e)
{
if(sender.Active)
{
Console.WriteLine("rbt1.Toggled");
}
}
在您的情况下,当按钮被激活或停用时会调用 Clicked
信号.单击一个停用的按钮会激活它,并停用另一个按钮,因此会调用两个信号.Toggled
信号在这两种情况下也被调用.我不太确定有什么区别,如果以编程方式更改按钮的状态,则可能无法保证调用 Clicked
.
In your case, the Clicked
signal is called when the button is activated or deactivated. Clicking on a deactivated button activates it, and deactivates a different button, so two signals are called. The Toggled
signal is also called in both cases. I'm not quite sure what the difference is, it may be that Clicked
is not guaranteed to be called if the button's state is changed programmatically.
这篇关于Mono gtk.RadioButton Clicked 事件触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!