Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:获取gtk按钮数组中按下的按钮?_C#_Arrays_Events_Button_Gtk# - Fatal编程技术网

C#:获取gtk按钮数组中按下的按钮?

C#:获取gtk按钮数组中按下的按钮?,c#,arrays,events,button,gtk#,C#,Arrays,Events,Button,Gtk#,我有一个按钮数组,所有按钮都将调用相同的方法,但是按钮的索引作为参数 using System; using Gtk; public class Input : Gtk.Window { private Gtk.Button[] plus; public Input() : base(Gtk.WindowType.Toplevel) { plus = new Button[10]; [……] for(uint i=0;i

我有一个按钮数组,所有按钮都将调用相同的方法,但是按钮的索引作为参数

using System;
using Gtk;

public class Input : Gtk.Window {

    private Gtk.Button[] plus;

    public Input() : base(Gtk.WindowType.Toplevel) {

        plus = new Button[10];
[……]

for(uint i=0;i<10;i++){
加上[i]=新按钮();
加上[i].Name=i.ToString();
加上[i]。ButtonPressEvent+=AddButtonPressed;
}
}
我尝试使用此方法,但似乎它甚至没有被调用,因为没有输出:

    protected virtual void AddButtonPressed(object sender, System.EventArgs e) {
        Console.WriteLine("Button pressed");
        for (uint i = 0; i < plus.Length; i++) {
        if (sender.Equals(plus[i])) {
            uint index = i;
            i = (uint)plus.Length;
            Console.WriteLine(index);
        }
    }
protected virtual void AddButtonPressed(对象发送方,System.EventArgs e){
控制台。写入线(“按下按钮”);
对于(uint i=0;i
也许有人能给我指出正确的方向?
谢谢。

我非常确定您需要将按钮添加到GTK窗口层次结构中,例如:

    for (uint i = 0; i < 10; i++) {
        plus[i] = new Button();
        plus[i].Name = i.ToString();
        plus[i].ButtonPressEvent += AddButtonPressed;
        Add(plus[i]);
    }
for(uint i=0;i<10;i++){
加上[i]=新按钮();
加上[i].Name=i.ToString();
加上[i]。ButtonPressEvent+=AddButtonPressed;
加上(加上[i]);
}

应该与此类似,从未实际使用过GTK我相当确定您需要实际将按钮添加到GTK窗口层次结构中,例如:

    for (uint i = 0; i < 10; i++) {
        plus[i] = new Button();
        plus[i].Name = i.ToString();
        plus[i].ButtonPressEvent += AddButtonPressed;
        Add(plus[i]);
    }
for(uint i=0;i<10;i++){
加上[i]=新按钮();
加上[i].Name=i.ToString();
加上[i]。ButtonPressEvent+=AddButtonPressed;
加上(加上[i]);
}

应该与此类似,如果GTK按钮有一个标记属性,则从未实际使用过GTK使用标记属性

for (uint i = 0; i < 10; i++) {
    plus[i] = new Button();
    plus[i].Name = i.ToString();
    plus[i].ButtonPressEvent += AddButtonPressed;
    plus[i].Tag = i;
    Add(plus[i]);
}

如果Gtk按钮有标记属性,则使用标记属性

for (uint i = 0; i < 10; i++) {
    plus[i] = new Button();
    plus[i].Name = i.ToString();
    plus[i].ButtonPressEvent += AddButtonPressed;
    plus[i].Tag = i;
    Add(plus[i]);
}
快速回答:

[GLib.ConnectBefore]
protected virtual void AddButtonPressed(object sender, EventArgs e)     {
    Console.WriteLine("Button pressed");
    for (uint i = 0; i < plus.Length; i++) {
        if (sender.Equals(plus[i])) {
        uint index = i;
        i = (uint)plus.Length;
        Console.WriteLine(index);
        }
    }
}
它编译和运行时没有错误,但它也有同样的问题,事件永远不会触发。我意识到不能将属性直接放在委托/lambda上。因此,即使支持方法有
[GLib.ConnectBefore]
委托没有,它也会失败

最后,您可以使用中的Clicked事件。我验证了它是否按预期工作。人们可能会认为它只会在鼠标单击时启动,但实际上它也会在空格键上启动。

快速回答:

[GLib.ConnectBefore]
protected virtual void AddButtonPressed(object sender, EventArgs e)     {
    Console.WriteLine("Button pressed");
    for (uint i = 0; i < plus.Length; i++) {
        if (sender.Equals(plus[i])) {
        uint index = i;
        i = (uint)plus.Length;
        Console.WriteLine(index);
        }
    }
}
for (uint i = 0; i < 10; i++) 
{
    plus[i] = new Button();
    plus[i].Data.Add("Index",i);
    plus[i].ButtonPressEvent += AddButtonPressed;
    Add(plus[i]);
}
它编译和运行时没有错误,但它也有同样的问题,事件永远不会触发。我意识到不能将属性直接放在委托/lambda上。因此,即使支持方法有
[GLib.ConnectBefore]
委托没有,它也会失败


最后,您可以使用中的Clicked事件。我验证了它是否按预期工作。人们可能会认为它只会在鼠标单击时启动,但实际上也会在空格键上启动。

它们都正确地添加到vbox中,并在执行程序时显示,但按下按钮不起作用。对不起,我以为它是clear…它们都正确地添加到vbox中,并在执行程序时显示,但按下按钮不起作用。对不起,我认为这很清楚…Gtk。按钮没有属性“标记”,但我尝试使用名称作为标识符,其工作方式应与此相同。无论如何,这无助于解决按下按钮时无法调用该方法的问题,但谢谢。您的事件处理程序的签名错误。这来自Mono文档:public delegate void ButtonPressEventHandler(对象o,ButtonPressEventArgs args)这是错误的。如果您实际测试过,“方法组到委托的转换在其参数类型中是逆变的”()。由于AddButtonPressed接受/any/EventArgs,因此它本质上接受任何ButtonPressEventArgs,因为ButtonPressEventArgs毕竟只是另一种EventArgs。Gtk.Button没有属性“Tag”,但我尝试使用名称作为标识符,其工作方式应与此相同。无论如何,这无助于解决按下按钮时无法调用该方法的问题,但谢谢。您的事件处理程序的签名错误。这来自Mono文档:public delegate void ButtonPressEventHandler(对象o,ButtonPressEventArgs args)这是错误的。如果您实际测试过,“方法组到委托的转换在其参数类型中是逆变的”()。由于AddButtonPressed接受/any/EventArgs,它本质上接受任何ButtonPressEventArgs,因为ButtonPressEventArgs毕竟只是另一种EventArgs。我现在采用了简短的hacky方式;-)感谢您提供了这个非常详细的答案和投入的时间!顺便说一句:它必须是[GLib.ConnectBeforeAttribute]事实上,这两种方式都可以。请看。我刚刚发现,这表示“按照惯例,所有属性名称都以“attribute”一词结尾,以区别于.NET Framework中的其他项。但是,在代码中使用属性时,您不需要指定属性后缀。”我现在采用了简短的hacky方式;-)谢谢你的详细回答和投入的时间!顺便说一句:它必须是[GLib.ConnectBeforeAttribute],事实上,这两种方式都有效。请看。我刚刚发现,它说“按照惯例,所有属性名称都以单词”attribute结尾将它们与.NET Framework中的其他项区分开来。但是,在代码中使用属性时,不需要指定属性后缀
for (uint i = 0; i < 10; i++) 
{
    plus[i] = new Button();
    plus[i].Data.Add("Index",i);
    plus[i].ButtonPressEvent += AddButtonPressed;
    Add(plus[i]);
}