C# Winfoms Toolstripdropdown close on button click

前端 未结 1 806
情深已故
情深已故 2021-01-03 06:27

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

var td = new ToolStripDropDown
        {
            Aut         


        
相关标签:
1条回答
  • 2021-01-03 06:43

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题