C#Winfoms Toolstripdropdown单击按钮关闭 [英] C# Winfoms Toolstripdropdown close on button click

查看:704
本文介绍了C#Winfoms Toolstripdropdown单击按钮关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体中使用toolstripdropdown来显示单击另一个按钮时的按钮列表。

I have used toolstripdropdown in my Windows form to show list of buttons on click of another button.

var td = new ToolStripDropDown
        {
            AutoSize = true,
            DropShadowEnabled = false,
            BackColor = Color.Transparent,
            Margin = Padding.Empty,
            Padding = Padding.Empty
        };

        var host = new ToolStripControlHost(panel)
        {
            BackColor = Color.Transparent,
            Margin = Padding.Empty,
            Padding = Padding.Empty
        };

        td.Items.Add(host);

面板包含要显示的按钮列表。要向用户显示面板,请在按钮(显示)上单击以下行。

The panel contains list of buttons to be displayed. To show the panel to user, on button(Show) click following line is called.

td.Show(pointonScreen);

默认情况下,自动关闭设置为true 。因此,每当用户单击表单中的任何位置时,toolstripdropdown都会关闭。还行吧。

By default, AutoClose is set to true. So whenever user clicks anywhere in the form, the toolstripdropdown is getting closed. This is ok.

我的要求:


  1. 单击显示按钮

  2. 通过调用td.show()显示工具下拉菜单,如果 td.Visible

  3. 再次关闭弹出窗口,请单击显示。按钮

  4. 工具条被淹死

  5. 单击表单中的任意位置,如果可见,应该关闭toolstripdropdown

  1. Click Show button
  2. Display the toolstripdropdown by calling td.show() and close the popup if td.Visible
  3. Again click the Show button
  4. toolstripdrown should be closed
  5. Click anywhere in the form, toolstripdropdown should be closed if it is visible

现在发生的是,在步骤3上,在引发按钮单击事件之前,toolstripdropdown正在关闭。所以再次显示下拉列表。

What is happening now is, on step 3, before the button click event is raised, toolstripdropdown is getting closed. So again the dropdown is being displayed.

还有其他方法可以满足我的要求吗?

Is there any other way to achieve my requirements?

推荐答案

您应该处理下拉菜单的 Closing 事件,如果下拉菜单通过单击关闭,则设置标志在打开它的按钮上然后,当您单击按钮时,检查标志,如果没有标志,则显示下拉菜单并设置标志,否则关闭下拉菜单并清除标志:

You should handle Closing event of the dropdown and set a flag if the dropdown is closing by click on the button which opened it. Then when you click on button, check the flag and if there wasn't a flag, show dropdown and set the flag, otherwise close the dropdown and clear the flag:

ToolStripDropDown td;
private void Form1_Load(object sender, EventArgs e)
{
    td = new ToolStripDropDown { /*...*/};
    var host = new ToolStripControlHost(this.panel1){ /*...*/};
    td.Items.Add(host);
    td.Closing += td_Closing;
}
void td_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
    if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked)
        if (this.button1.Bounds.Contains(this.PointToClient(MousePosition)))
        {
            td.Tag = true;
            return;
        }
    td.Tag = null;
}
private void button1_Click(object sender, EventArgs e)
{
    if (td.Tag == null)
    {
        td.Show(Cursor.Position);
        td.Tag = true;
    }
    else
    {
        td.Close();
        td.Tag = null;
    }
}

这篇关于C#Winfoms Toolstripdropdown单击按钮关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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