0
votes

I want to add a "Clear" button on the WinForms ComboBox. So I created a custom control that inherits from ComboBox and added a Label to it. Here's the entire code:

public class ComboBoxClear : ComboBox
{
    private readonly Label lblClear;

    public ComboBoxClear()
    {
        lblClear = new Label
        {
            Location = new Point(0, 0),
            AutoSize = true,
            Text = "✖",
            ForeColor = Color.Gray,
            Visible = false,
            Font = new Font("Tahoma", Font.Size),
            Cursor = Cursors.Hand,
        };

        Controls.Add(lblClear);
        lblClear.Click += (s, e) =>
        {
            lblClear.Visible = false;
            SelectedIndex = -1;
        };

        lblClear.BringToFront();
        SetLocation();
    }

    [DefaultValue(true)]
    [Category("Appearance")]
    public bool ShowClearButton { get; set; } = true;

    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        lblClear.Visible = ShowClearButton && !string.IsNullOrEmpty(Text);
    }

    protected override void OnFontChanged(EventArgs e)
    {
        base.OnFontChanged(e);
        lblClear.Font = new Font("Tahoma", Font.Size);
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        SetLocation();
    }

    private void SetLocation() =>
        lblClear.Location = new Point(Width - (lblClear.Width * 2), ((Height - lblClear.Height) / 2) - 3);
}

However, this does not work as expected. When I type, the label shows for a moment then goes hidden if I type again or move the mouse. Interestingly when I hover the mouse over it, the cursor changes but it's like the intersection of the label and the ComboBox is cleared.

I tried overriding the "OnPaint" or handling "Paint" event, none seem to be triggered.

1

1 Answers

0
votes

I'm not sure if I fully understand your issue, but you are instantiating a label, adding it to your controls, setting a click event on it, and bringing it to the front inside your combo box constructor, and that could be the problem.

I'm not sure if a Form.Label can be instantiated and used from a Form.ComboBox or not, and I'm not sure if you want to use some parent Label property of combo box and add it to some parent Controls property of combo box, or if you want to use Form.Label, but unless you want your label to somehow be part of your combo box, then you should put your label somewhere next to your combo box as part of your Form and try setting up your label within your form instead of your combo box constructor. For example:

public partial class Form1 : Form {
  
    public Form1()
    {
        InitializeComponent();
    }
  
    private void Form1_Load(object sender, EventArgs e)
    {
        Label label = new Label();
        label .Location = new Point(50, 50);
        label .AutoSize = true;
        label .Text = "Whatever";
  
        this.Controls.Add(label );
  
        ComboBox mybox = new ComboBox();
        mybox.Location = new Point(50, 100);
        mybox.Size = new Size(200, 25);
        mybox.Items.Add("beep");
        mybox.Items.Add("bop");
        mybox.Items.Add("boop");
        mybox.Items.Add("bleep");
        mybox.Items.Add("blop");
  
        this.Controls.Add(mybox);
    }
}