使用Windows窗体将子菜单链接到按钮 [英] Linking a Submenu to a Button using Windows Forms

查看:96
本文介绍了使用Windows窗体将子菜单链接到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2008和Windows窗体在C#中对应用程序进行编程,以生成一系列表示功能类别的按钮.按下每个按钮时,我需要显示列出参数的子菜单.工具箱中的MenuStrip提供了类似的功能,但我需要独立的按钮,可以将其放置在屏幕上的任何位置.

I''m programming an application in C# using Visual Studio 2008 and Windows Forms to generate a series of Buttons that represent catagories of features. When each Button is pressed, I need to display submenus which list parameters. The MenuStrip from the ToolBox provides a similar functionality, but I need independent Buttons which can be placed anywhere on the screen.

How can I make this work?

推荐答案

您说列表"将包含用户可以修改的参数".

有用的回答取决于用户可以修改"的含义.

如果这表示选择,则可以使用 ContextMenuStrip [ ^ ]并使用 ToolStripMenuItem [
You say that the ''list'' will contain ''parameters'' that the user can modify.

Answering helpfully depends on what you mean by ''that the user can modify''.

If it means either selecting or not you could use a ContextMenuStrip[^] and use the CheckOnClick property of the ToolStripMenuItem[^]s that you add to it.

If the user interaction is more involved than that, then a Panel on your form (as suggested earlier) or a separate Dialog would seem to be the way to go.


如果您不这样做"像SAKryukov的答案一样,请看以下内容:
XtraBars [
If you don''t like SAKryukov''s answer, take a look at this:
XtraBars[^] - It certainly has the required functionality - there is a free trial.

Regards
Espen Harlinn


编写如下代码:

Write code like this:

class ButtonHelper { /* ... */ }

void SetupButtonsVertically() {
    //you will need integer layout parameters like
    //buttonLeft, currentY (to of buttons), yGap (between buttons), etc.,
    //source of button names, data on generated list of options
    // (you call it sub-menu but it might be rather list or tree view)
    //...
    //in some loop:
    Button button = new Button();
    button.Click = delegate(object sender, EventArgs eventArgs) {
        //... code of some handler: show group of choices
        ButtonHelper buttonHelper = (ButtonHelper)((sender as Button).Tag);
        //...
    }; //button.Click
    button.Tag = new ButtonHelper(//...
    button.Left = buttonLeft;
    button.Top = currentY;
    currectY += button.Height + yGap;
    button.Text = //...
    this.PanelSomewhereOnMyForm.Control.Add(button);
//...
}



呼叫InitializeComponent之后立即呼叫SetupButtonsVertically.
您可以进行更复杂的布局;这只是个主意.

注意匿名方法的使用:编程事件处理程序的简便得多的方法.在Button.Click处理程序的范围内,您可以调用任何方法,但与(通常过时的)非匿名方法不同,您不必传递sendereventArgs的确切参数列表.
另一个有用的技巧是使用button.Tag.您可以在按钮功能(某些帮助程序类ButtonHelper的类)的Tag中放置一些有用的信息(请参见Control.Tag).可以从处理程序中的sender强制转换此Tag,以实现特定于按钮实例的点击效果.



Call SetupButtonsVertically right after a call to InitializeComponent.
You can make more complex layout; this is just the idea.

Note the use of anonymous method: much more convenient way of programming events handlers. From the scope of Button.Click handler you can call any method, but unlike (morally obsolete) non-anonymous approach you''re not bound to pass exact parameter list of sender and eventArgs.

Another useful trick is using button.Tag. You can put some useful information on the button function (of some helper class ButtonHelper) in its Tag (see Control.Tag). This Tag can be casted from sender in the handler to be used to implement click effect specific to a button instance.


这篇关于使用Windows窗体将子菜单链接到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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