【问题标题】:Add Custom Control To ComboBox将自定义控件添加到 ComboBox
【发布时间】:2010-10-06 20:41:32
【问题描述】:

大家好 我创建了一个自定义用户控件,我希望将此自定义控件放置在组合框中。 我正在使用此代码:

public class UserControlComboBox : ComboBox, IMessageFilter
{
    public readonly NimaDatePickerUC.MiladiNimaDatePickerUC UserControl = new NimaDatePickerUC.MiladiNimaDatePickerUC();

    protected override void WndProc(ref Message m)
    {
        if ((m.Msg == 0x0201) || (m.Msg == 0x0203))
        {
            if (DroppedDown)
                HideUserControl();
            else
                ShowUserControl();
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    public bool PreFilterMessage(ref Message m)
    {
        // intercept mouse events 
        if ((m.Msg >= 0x0200) && (m.Msg <= 0x020A))
        {
            if (this.UserControl.RectangleToScreen(this.UserControl.DisplayRectangle).Contains(Cursor.Position))
            {
                // clicks inside the user control, handle normally
                return false;
            }
            else
            {
                // clicks outside the user controlcollapse it. 
                if ((m.Msg == 0x0201) || (m.Msg == 0x0203))
                    this.HideUserControl();
                return true;
            }
        }
        else return false;
    }

    public new bool DroppedDown
    {
        get { return this.UserControl.Visible; }
    }

    protected void ShowUserControl()
    {
        if (!this.Visible)
            return;

        this.UserControl.Anchor = this.Anchor;
        this.UserControl.BackColor = this.BackColor;
        this.UserControl.Font = this.Font;
        this.UserControl.ForeColor = this.ForeColor;

        // you can be cleverer than this if you need to 
        this.UserControl.Top = this.Bottom;
        this.UserControl.Left = this.Left;
        this.UserControl.Width = Math.Max(this.UserControl.Width, this.Width);


        this.Parent.Controls.Add(this.UserControl);
        this.Parent.Controls[0].BringToFront();
        this.UserControl.Visible = true;
        this.UserControl.BringToFront();

        base.OnDropDown(EventArgs.Empty);

        // start listening for clicks 
        Application.AddMessageFilter(this);
    }

    protected void HideUserControl()
    {
        Application.RemoveMessageFilter(this);

        base.OnDropDownClosed(EventArgs.Empty);
        this.UserControl.Visible = false;
        this.Parent.Controls.Remove(this.UserControl);

        // you probably want to replace this with something more sensible 
        this.Text = this.UserControl.Text;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.UserControl.Dispose();
        }

        base.Dispose(disposing);
    }
} 

此代码运行良好,但如果我在 CONTAINER 中使用组合框并且容器小于组合框大小+我的自定义控件,我的自定义控件不会很好地显示并出现在组合框下方。 如何在所有控件和容器前显示我的自定义控件?

【问题讨论】:

  • 这是 .NET 3.5 的控件吗?

标签: combobox controls


【解决方案1】:

这可能看起来很直观,但您是否尝试过

[controlname].BringToFront();

还发现了here

【讨论】:

  • 您可能想了解如何使您的控件成为顶级控件。看起来这就是你想要做的。
猜你喜欢
  • 1970-01-01
  • 2022-07-14
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多