Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义组合框未正确关闭_C#_Combobox_Listbox - Fatal编程技术网

C# 自定义组合框未正确关闭

C# 自定义组合框未正确关闭,c#,combobox,listbox,C#,Combobox,Listbox,我正试图使我的自定义组合框继承自ContainerControl。我用文章作为基础,但重写了它,但我使用了ToolStripControlHost,我自己的自定义列表框&ToolStripDropDown 现在,组合框是一个按钮,您可以在其中单击以显示包含my ListBox的下拉列表,它可以很好地覆盖OnMouseClick 当我尝试关闭下拉列表时,问题就开始了,下拉列表的“自动关闭”属性为true,如果单击下拉列表之外的某个位置(包括按钮),下拉列表将关闭 这是单击按钮的代码。。如果你点击它

我正试图使我的自定义组合框继承自ContainerControl。我用文章作为基础,但重写了它,但我使用了ToolStripControlHost,我自己的自定义列表框&ToolStripDropDown

现在,组合框是一个按钮,您可以在其中单击以显示包含my ListBox的下拉列表,它可以很好地覆盖OnMouseClick

当我尝试关闭下拉列表时,问题就开始了,下拉列表的“自动关闭”属性为true,如果单击下拉列表之外的某个位置(包括按钮),下拉列表将关闭

这是单击按钮的代码。。如果你点击它会发生什么? 如果显示下拉列表,它首先关闭下拉列表,然后触发OnMouseClick事件。含义:listboxControl.Visible已为false&它将再次显示下拉列表。所有这些都会导致快速关闭和打开

我已经被这个问题困扰了一段时间,而谷歌似乎对这个问题不太了解(关于CodeProject的那篇文章也有同样的bug)

我尝试过的是在显示下拉列表后禁用自动关闭并捕获鼠标,这在一定程度上起作用,但会影响托管列表框的工作。列表框包含一组控件(项),这些项具有悬停绘制效果。在ListBox控件中捕获鼠标可防止触发OnMouseCenter


所有的投入将不胜感激

当下拉菜单关闭时,您需要一个变量来跟踪光标位置

下面是一个快速而肮脏的示例控件:

public class CustomDropBox : Control {
  private ListBox box = new ListBox() { IntegralHeight = false };
  private ToolStripControlHost host;
  private ToolStripDropDown drop;
  private bool wasShowing = false;

  public CustomDropBox() {
    box.MinimumSize = new Size(120, 120);
    box.MouseUp += box_MouseUp;
    box.KeyPress += box_KeyPress;
    box.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });
    host = new ToolStripControlHost(box) { Padding = Padding.Empty };
    drop = new ToolStripDropDown() { Padding = Padding.Empty };
    drop.Closing += drop_Closing;
    drop.Items.Add(host);
  }

  private Rectangle GetDownRectangle() {
    return new Rectangle(this.ClientSize.Width - 16, 0, 16, this.ClientSize.Height);
  }

  void drop_Closing(object sender, ToolStripDropDownClosingEventArgs e) {
    if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked) {
      wasShowing = GetDownRectangle().Contains(this.PointToClient(Cursor.Position));
    } else {
      wasShowing = false;
    }
  }

  void box_KeyPress(object sender, KeyPressEventArgs e) {
    if (e.KeyChar == (char)Keys.Enter && box.SelectedIndex > -1) {
      drop.Close();
    }
  }

  void box_MouseUp(object sender, MouseEventArgs e) {
    int index = box.IndexFromPoint(e.Location);
    if (index > -1) {
      drop.Close();
    }
  }

  protected override void OnMouseDown(MouseEventArgs e) {
    if (e.Button == MouseButtons.Left && GetDownRectangle().Contains(e.Location)) {
      if (wasShowing) {
        wasShowing = false;
      } else {
        drop.Show(this, new Point(0, this.Height));
      }
    }      
    base.OnMouseDown(e);
  }

  protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.Clear(Color.White);
    ControlPaint.DrawComboButton(e.Graphics, GetDownRectangle(), ButtonState.Normal);
    base.OnPaint(e);
  }
}
public class CustomDropBox : Control {
  private ListBox box = new ListBox() { IntegralHeight = false };
  private ToolStripControlHost host;
  private ToolStripDropDown drop;
  private bool wasShowing = false;

  public CustomDropBox() {
    box.MinimumSize = new Size(120, 120);
    box.MouseUp += box_MouseUp;
    box.KeyPress += box_KeyPress;
    box.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });
    host = new ToolStripControlHost(box) { Padding = Padding.Empty };
    drop = new ToolStripDropDown() { Padding = Padding.Empty };
    drop.Closing += drop_Closing;
    drop.Items.Add(host);
  }

  private Rectangle GetDownRectangle() {
    return new Rectangle(this.ClientSize.Width - 16, 0, 16, this.ClientSize.Height);
  }

  void drop_Closing(object sender, ToolStripDropDownClosingEventArgs e) {
    if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked) {
      wasShowing = GetDownRectangle().Contains(this.PointToClient(Cursor.Position));
    } else {
      wasShowing = false;
    }
  }

  void box_KeyPress(object sender, KeyPressEventArgs e) {
    if (e.KeyChar == (char)Keys.Enter && box.SelectedIndex > -1) {
      drop.Close();
    }
  }

  void box_MouseUp(object sender, MouseEventArgs e) {
    int index = box.IndexFromPoint(e.Location);
    if (index > -1) {
      drop.Close();
    }
  }

  protected override void OnMouseDown(MouseEventArgs e) {
    if (e.Button == MouseButtons.Left && GetDownRectangle().Contains(e.Location)) {
      if (wasShowing) {
        wasShowing = false;
      } else {
        drop.Show(this, new Point(0, this.Height));
      }
    }      
    base.OnMouseDown(e);
  }

  protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.Clear(Color.White);
    ControlPaint.DrawComboButton(e.Graphics, GetDownRectangle(), ButtonState.Normal);
    base.OnPaint(e);
  }
}