如何使用另一个自定义控件在自定义控件中初始化事件

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

大家好

我正在尝试创建一个自定义组合框。我创建了一个自定义下拉按钮并使用 is 作为下拉按钮。用户按下此按钮后,将打开一个面板,其中包含可以选择的项目列表(按钮格式)。用户选择一个项目后,下拉按钮的文本需要更改为所选项目的文本。我知道这是普通 Windows 窗体组合框的基本功能,但我将向此自定义组合框添加一些功能,这些功能对于普通 Windows 窗体组合框不可用。我遇到的问题是,自定义下拉按钮文本的更新仅在打开包含可用项目的面板、关闭它并再次打开它之后发生。我确信我只是简单的事情。一些帮助将不胜感激。

请参阅下面的自定义下拉按钮代码:

namespace Example.CustomControls
{
    public class DropdownCheckBox : CheckBox
    {
        private int initialWidth;

        private string dropdownText;

        private Icon dropdownIcon = Properties.Resources.DropDownArrow;

        public string DropDownText
        {
            get { return dropdownText; }
            set
            {
                dropdownText = value;
                this.Text = value;
            }
        }

        public DropdownCheckBox()
        {
            TextAlign = ContentAlignment.MiddleLeft;
            Margin = new Padding(0);
            Padding = new Padding(0);
            FlatAppearance.BorderSize = 1;
            FlatStyle = FlatStyle.Flat;
            BackColor = SystemColors.Window;
            Appearance = Appearance.Button;
            FlatAppearance.MouseOverBackColor = SystemColors.Window;
            FlatAppearance.MouseDownBackColor = SystemColors.Window;
            initialWidth = this.Width;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Text = dropdownText;
            this.AutoSize = false;
            this.Height = 21;

            Rectangle dropDownIconRectangle = new Rectangle(this.Width - this.Height + 2, 2, this.Height - 4, this.Height - 4);

            e.Graphics.FillRectangle(new SolidBrush(SystemColors.Control), dropDownIconRectangle);
            
            e.Graphics.DrawIcon(dropdownIcon, dropDownIconRectangle);
        }
    }
}

我继承自复选框控件,因为我使用按钮的状态来打开和关闭包含可用项目列表(按钮格式)的面板。

请参阅下面为项目创建按钮的面板代码:

namespace Example.CustomControls
{
    public class ButtonsPanel : Panel
    {
        public bool createState = false;

        public string selectedItemButtonText;

        public string[] itemsList = new string[0];

        public string[] ItemsList
        {
            get { return itemsList; }
            set
            {
                this.Controls.Clear();

                itemsList = value;
                createState = true;

                Refresh();
            }
        }

        public ButtonsPanel()
        {
            Dock = DockStyle.Top;
            Margin = new Padding(0);
            Padding = new Padding(0);
            BorderStyle = BorderStyle.FixedSingle;
            BackColor = SystemColors.Window;
            AutoScroll = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            this.Height = 5 * 21;

            if (createState == true)
            {
                foreach (string item in itemsList.Reverse())
                {
                    Button itemButton = new Button
                    {
                        Text = item,
                        Height = 21,
                        Dock = DockStyle.Top,
                        TextAlign = ContentAlignment.MiddleLeft,
                        Margin = new Padding(0),
                        Padding = new Padding(0),
                        FlatStyle = FlatStyle.Flat,
                        BackColor = SystemColors.Window,
                    };

                    itemButton.FlatAppearance.BorderSize = 0;
                    itemButton.FlatAppearance.MouseOverBackColor = SystemColors.Highlight;
                    itemButton.FlatAppearance.MouseDownBackColor = SystemColors.Highlight;

                    this.Controls.Add(itemButton);
                }

                foreach (Control itemButton in this.Controls)
                {
                    itemButton.Click += ItemButton_Click;
                }

                createState = false;
            }
        }

        private void ItemButton_Click(object sender, EventArgs e)
        {
            Button itemButton = (Button)sender;

            selectedItemButtonText = itemButton.Text;
        }
    }
}

请参阅下面的自定义组合框代码:

namespace Example.CustomControls
{
    public class CustomComboBox : UserControl
    {
        private string dropdownCheckBoxText = "Select Items";

        private string[] dropDownitemsList = new string[0];

        private DropdownCheckBox dropdownCheckBox;

        private ButtonsPanel dropdownPanel;

        public string DropDownButtonText
        {
            get { return dropdownCheckBoxText; }
            set
            {
                dropdownCheckBoxText = value;
                dropdownCheckBox.DropDownText = value;
            }
        }

        public string[] DropDownItemsArray
        {
            get { return dropDownitemsList; }
            set
            {
                dropdownPanel.Controls.Clear();
                
                dropDownitemsList = value;
                dropdownPanel.itemsList = value;
                dropdownPanel.createState = true;

                Refresh();
            }
        }

        public CustomComboBox()
        {
            dropdownCheckBox = new DropdownCheckBox()
            {
                DropDownText = dropdownCheckBoxText,
                Dock = DockStyle.Top
            };

            dropdownPanel = new ButtonsPanel();

            this.Controls.Add(dropdownCheckBox);
            this.Controls.Add(dropdownPanel);

            dropdownPanel.Visible = false;

            dropdownCheckBox.CheckedChanged += DropDownCheckBox_CheckedChanged;
        }

        private void DropDownCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (dropdownCheckBox.Checked == true)
            {
                Console.WriteLine("Eina");
                this.Height = dropdownCheckBox.Height + dropdownPanel.Height;

                dropdownPanel.Visible = true;
                dropdownPanel.BringToFront();

                foreach (Button itemButton in dropdownPanel.Controls)
                {
                    itemButton.MouseClick += CustomComboBox_MouseClick;
                }
            }

            else
            {
                this.Height = dropdownCheckBox.Height;

                dropdownPanel.Visible = false;
                dropdownPanel.SendToBack();

                foreach (Button itemButton in dropdownPanel.Controls)
                {
                    itemButton.MouseClick -= CustomComboBox_MouseClick;
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (dropdownCheckBox.Checked == true)
            {
                this.Height = dropdownCheckBox.Height + dropdownPanel.Height;
            }

            else
            {
                this.Height = dropdownCheckBox.Height;
            }
        }

        private void CustomComboBox_MouseClick(object sender, MouseEventArgs e)
        {
            dropdownCheckBoxText = dropdownPanel.selectedItemButtonText; ;
            dropdownCheckBox.DropDownText = dropdownPanel.selectedItemButtonText;
        }
    }
}

据我所知,当我创建“CustomComboBox_MouseClick”事件时,最初报告了 0 个控件,但我不确定为什么。

c# winforms combobox event-handling user-controls
1个回答
0
投票
namespace WindowsFormsApp2
{
    public class CustomComboBox : FlowLayoutPanel
    {
        private CheckBox dropDownButton;

        private FlowLayoutPanel dropDownFlowLayoutPanel;

        private Button itemButton;

        private Icon dropdownIcon = Properties.Resources.DropDownArrow;

        public CustomComboBox(string initialDropDownButtonText, string[] itemsList)
        {
            dropDownButton = new CheckBox
            {
                Text = initialDropDownButtonText,
                Height = 23,
                Margin = new Padding(1, 1, 1, 0),
                Appearance = Appearance.Button,
                FlatStyle = FlatStyle.Flat,
                TextAlign = System.Drawing.ContentAlignment.MiddleLeft,
                BackColor = SystemColors.Window
            };

            dropDownButton.FlatAppearance.MouseOverBackColor = SystemColors.Window;
            dropDownButton.FlatAppearance.MouseDownBackColor = SystemColors.Window;
            dropDownButton.FlatAppearance.CheckedBackColor = SystemColors.Window;

            dropDownButton.Paint += DropDownButton_Paint;

            dropDownFlowLayoutPanel = new FlowLayoutPanel
            {
                Height = 10 * dropDownButton.Height,
                BorderStyle = BorderStyle.FixedSingle,
                Margin = new Padding(1, 0, 1, 1),
                BackColor = SystemColors.Window,
                AutoScroll = true
            };

            foreach (string item in itemsList)
            {
                itemButton = new Button
                {
                    Text = item,
                    Height = 23,
                    Width = dropDownFlowLayoutPanel.Width,
                    Margin = new Padding(0),
                    FlatStyle = FlatStyle.Flat,
                    TextAlign = System.Drawing.ContentAlignment.MiddleLeft,
                    BackColor = SystemColors.Window
                };

                itemButton.FlatAppearance.BorderSize = 0;

                itemButton.FlatAppearance.MouseOverBackColor = SystemColors.Highlight;
                itemButton.FlatAppearance.MouseDownBackColor = SystemColors.Highlight;

                dropDownFlowLayoutPanel.Controls.Add(itemButton);
            }

            foreach (Button button in dropDownFlowLayoutPanel.Controls)
            {
                button.Click += Button_Click;
            }

            this.Controls.Add(dropDownButton);
            this.Controls.Add(dropDownFlowLayoutPanel);

            dropDownButton.CheckedChanged += DropDownButton_CheckedChanged;
        }

        private void DropDownButton_CheckedChanged(object sender, EventArgs e)
        {
            if (dropDownButton.Checked)
            {
                this.Height = dropDownButton.Height + dropDownFlowLayoutPanel.Height + 2;
            }

            else
            {
                this.Height = dropDownButton.Height + 1;
            }
        }

        private void Button_Click(object sender, EventArgs e)
        {
            Button clickedItem = (Button)sender;

            dropDownButton.Text = clickedItem.Text;
        }

        private void DropDownButton_Paint(object sender, PaintEventArgs e)
        {
            Rectangle dropDownButtonIconRectangle = new Rectangle(dropDownButton.Width - dropDownButton.Height + 2, 2, dropDownButton.Height - 4, dropDownButton.Height - 4);

            e.Graphics.FillRectangle(new SolidBrush(SystemColors.Control), dropDownButtonIconRectangle);

            e.Graphics.DrawIcon(dropdownIcon, dropDownButtonIconRectangle);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            dropDownButton.Width = this.Width - 2;
            dropDownFlowLayoutPanel.Width = this.Width - 2;

            if (dropDownButton.Checked)
            {
                this.Height = dropDownButton.Height + dropDownFlowLayoutPanel.Height + 1;
            }

            else
            {
                this.Height = dropDownButton.Height + 1;
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.