我正在开发一个应用程序来解码通过串行端口发送的消息,并根据它发出用户按钮。我将这些存储在名为"按钮"的列表中。现在,为了演示双向通信,我想根据"颜色"发送消息。我在用户控件中定义的属性。因此,如果我点击一个红色按钮,程序会发出一个" r",依此类推。我想知道如何获取单击按钮的列表索引,甚至只是获取给定按钮的属性。
因此,例如,根据来自串口的消息,我的程序会在列表中添加一个按钮。然后我设置它的各种参数,如位置,大小,颜色等。当我点击它时,根据它的颜色,我想通过串口发送相应的字符。
我为此定义了一个按钮单击事件处理程序。我打算在事件处理程序中获取按钮的列表索引,我只是不知道如何。我尝试使用活动的发件人,我尝试使用"这个",我看了一遍,但我对如何做到这一点没有任何想法。
这是我添加按钮的地方:
guiButton newButton = new guiButton();
EventHandler myHandler = new EventHandler(guibutton_click);
newButton.Click += myHandler;
newButton.Location = new System.Drawing.Point(10, 10);
newButton.Size = new System.Drawing.Size(10, 10);
buttons.Add(newButton);
this.Controls.Add(newButton);
这将是事件
private void guibutton_click(object sender, EventArgs e)
{
//somehow get ID number here (which is an int)
if (buttons[ID].color == "green") port.WriteLine("g");
if (buttons[ID].color == "blue") port.WriteLine("b");
if (buttons[ID].color == "red") port.WriteLine("r");
if (buttons[ID].color == "yellow") port.WriteLine("y");
}
仅供参考,这是我的按钮列表:
List<guiButton> buttons = new List<guiButton>();
编辑:我设法从事件处理程序的发件人对象中获取用户按钮的颜色属性,但我仍然需要按钮的列表索引,因为我想删除该按钮并将其删除双击时从列表中。这可能吗?发件人信息是否也包含此内容?