动态添加menuStrip项目 [英] Dynamically adding menuStrip items

查看:186
本文介绍了动态添加menuStrip项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个问题,动态添加项目到menuStrip。我的意思是我知道如何添加项目,但我不知道如何使点击处理程序的动态添加项目。

so I have a problem with dynamically adding items to menuStrip. I mean I know how to add items to it, but I dont have any idea how to make Click handler to those dynamically added items.

for(int i = 0; i < grupiuKiekis; i++)
    {
        row2 = mysql_fetch_row(result2);
        System::String^ grupesName = gcnew String(row2[1]);
        pasirinktiGrupęToolStripMenuItem->DropDownItems->Add(grupesName);
    }

请告诉我正确的方法。

推荐答案

Add方法有几个重载。您可以使用重载,允许您明确指定EventHandler ,或您可以构建一个 ToolStripItem ,设置点击处理程序,然后添加ToolStripItem

The Add method has several overloads. You could use the overload that allows you to specify an EventHandler explicitly, or you could construct a ToolStripItem, set up a click handler on that, and then add the ToolStripItem.

这里基本上是你想要做的:

Here's basically what you want to do:

for(int i = 0; i < grupiuKiekis; i++)
{
    row2 = mysql_fetch_row(result2);
    System::String^ grupesName = gcnew String(row2[1]);
    ToolStripItem^ item = gcnew ToolStripItem();
    item->Text = grupesName;
    item->Click += gcnew EventHandler(this, &Form1::clickHander);
    pasirinktiGrupęToolStripMenuItem->DropDownItems->Add(item);
}

void clickHander(Object^ sender, EventArgs^ e)
{
    ToolStripItem^ item = (ToolStripItem^) sender;
    System::String^ grupesName = item->Text;
    // Do what you need to do.
}

这篇关于动态添加menuStrip项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆