ToolStripDropDown在基础控件之前捕获MouseeDown

问题描述 投票:1回答:1

我创建了自己的类似ComboBox的控件,其中下拉部分包含一棵树。我已经看到这些解决方案使用普通的ComboBox并覆盖WndProc,但总是有一些奇怪的行为,尽管有很多代码。所以我决定简单一点:只有一个带有ToolStripDropDown / ToolStripControlHost的标签,当鼠标在标签上显示时会打开该标签。丢失的ComboBox三角形不会受到伤害。

一切都很完美,除了一件很小的东西:就像库存ComboBox一样,当我再次点击标签时,我希望隐藏下拉列表。但是当我点击它时,下拉隐藏了一瞬间,只是再次出现。如果我在标签外面点击,下拉列表就会隐藏,就像它应该的那样。

public class RepoNodeComboBox: Label
{
    RepoTreeView repoTreeView;
    ToolStripControlHost treeViewHost;
    ToolStripDropDown dropDown;
    bool isDropDownOpen;

    public int DropDownHeight;

    public RepoNodeComboBox()
    {
        repoTreeView = new RepoTreeView();
        repoTreeView.BorderStyle = BorderStyle.None;
        repoTreeView.LabelEdit = false;

        treeViewHost = new ToolStripControlHost(repoTreeView);
        treeViewHost.Margin = Padding.Empty;
        treeViewHost.Padding = Padding.Empty;
        treeViewHost.AutoSize = false;

        dropDown = new ToolStripDropDown();
        dropDown.CanOverflow = true;
        dropDown.AutoClose = true;
        dropDown.DropShadowEnabled = true;
        dropDown.Items.Add(treeViewHost);
        dropDown.Closing += dropDownClosing;

        TextAlign = ContentAlignment.MiddleLeft;
        BackColor = SystemColors.Window;
        BorderStyle = BorderStyle.FixedSingle;

        DropDownHeight = 400;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing) 
        {
            if (dropDown != null) 
            {
                dropDown.Dispose();
                dropDown = null;
            }
        }
        base.Dispose(disposing);
    }

    // when mouse goes down on the label, this is executed first            
    void dropDownClosing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // just to test if I can get more out of it than with dropdown.Visible
        isDropDownOpen = false; 
    }

    // this is subsidiary to the Closing event of the dropdown
    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (dropDown != null) 
        {
            if (isDropDownOpen)
            {
                dropDown.Hide();
            }
            else
            {
                repoTreeView.Size = new Size(Width, DropDownHeight);
                treeViewHost.Width = Width;
                treeViewHost.Height = DropDownHeight;
                dropDown.Show(this, 0, Height);
                isDropDownOpen = true;
            }
        }

        base.OnMouseDown(e);
    }
}

据我所知(断点),下拉列表首先捕获MOUSEDOWN事件以便自行关闭。只有在那之后我的标签才会通过MOUSEDOWN事件,并且由于它看到下拉列表已关闭,它认为标签已经像第一次点击一样 - 并再次打开下拉列表。

因此,标签似乎无法知道MOUSEDOWN是否是关闭下拉项目的结果。我可以在每个其他事件中打开它,但这不需要其他关闭事件发生。

有没有办法确保即使我点击标签,打开的下拉项目也会关闭?

c# winforms mouseevent toolstripdropdown
1个回答
1
投票

浮动主机关闭时,请尝试检查鼠标位置:

void dropDownClosing(object sender, CancelEventArgs e) {
  isDropDownOpen = this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition));
}

在MouseDown代码中,确保在true块中将isDropDownOpen设置为false:

protected override void OnMouseDown(MouseEventArgs e) {
  if (dropDown != null) {
    if (isDropDownOpen) {
      isDropDownOpen = false;
      dropDown.Hide();
    } else {
      isDropDownOpen = true;
      repoTreeView.Size = new Size(Width, DropDownHeight);
      treeViewHost.Width = Width;
      treeViewHost.Height = DropDownHeight;
      dropDown.Show(this, 0, Height);
    }
  }
  base.OnMouseDown(e);
}
© www.soinside.com 2019 - 2024. All rights reserved.