Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a custom combo box class.
In some cases I need it to be disabled without being grayed and in some others I need it to be enabled with loaded data of a data source of sql server database through a sql query.
So I thought about this, to go inside my custom class
and create a custom MouseMove event in order to make it loose focus when I want to disable it. But I don't know how to override it, because I have an additional argument of bool type the Disable.
C#
private void CustomCbo_MouseMove(object sender, MouseEventArgs e, bool Disable)
{
     if (Disable)
     {
         this.Parent.Focus();
         this.SelectionLength = 0;
     }
}


However I found on another site the following, it's ok, but I don't understand it very well and I need the oppossite too, in order to enable the
CustomCombo so that to have the appropriate values of datasource of my database.
C#
public void DisableComboWithoutGrayedOut()
    {
          //Appearence Enable, behavior Disabled
          this.DropDownHeight = 1;
          this.KeyDown += (s, e) => e.Handled = true;
          this.KeyPress += (s, e) => e.Handled = true;
          this.KeyUp += (s, e) => e.Handled = true;

     }

In case I use the Enabled property it's ok, but I don't like to be grayed out. And ComboBox doesn't have a ReadOnly property like textboxes.
Any suggestion and explanation for both ways please, will be much appreciated. Thank you so much in advanced.

What I have tried:

private void CustomCbo_MouseMove(object sender, MouseEventArgs e, bool Disable)
 {
      if (Disable)
      {
          this.Parent.Focus();
          this.SelectionLength = 0;
      }
 }

  public void DisableComboWithoutGrayedOut()
      {
            //Appearence Enable, behavior Disabled
            this.DropDownHeight = 1;
            this.KeyDown += (s, e) => e.Handled = true;
            this.KeyPress += (s, e) => e.Handled = true;
            this.KeyUp += (s, e) => e.Handled = true;
           
       }
Posted

The object is that the ComboBox is not actually disable but the dropdown is set to 1 so that it drops down but since only 1 pixel you wont see it.

The other KeyDown, KeyPress and KeyUp handlers e.Handled properties are set to true meaning that the event has been handled and nothing more is done.
 
Share this answer
 
Comments
Aggeliki Asimakopoulou 12-Mar-24 10:08am    
The default DropDownHeight value is actually 106, but if we set it to 1, we can only retrieve it with a const variable. Thank you!!!
Create an enable method. By commenting out the KeyDown, KeyPress and KeyUp handlers they will function normally!

C#
public void EnableComboWithoutGrayedOut()
      {
            //Appearence Enable, behavior Enabled
            this.DropDownHeight = ??;  // Whatever it was before or what you want it to be
            //this.KeyDown += (s, e) => e.Handled = true;
            //this.KeyPress += (s, e) => e.Handled = true;
            //this.KeyUp += (s, e) => e.Handled = true;
           
       }
 
Share this answer
 
Comments
Aggeliki Asimakopoulou 12-Mar-24 10:08am    
The default DropDownHeight value is actually 106, but if we set it to 1, we can only retrieve it with a const variable. Thank you!!!
Here is my customCombo class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace AngelTechShop
{
    public class CustomCombo : ComboBox
    {
        public CustomCombo()
        {
            BorderColor = Color.DimGray;
        }

        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue(typeof(Color), "DimGray")]
        public Color BorderColor { get; set; }

        private const int WM_PAINT = 0xF;
        private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;

        private bool mDisable;

        public bool Disable
        {
            get { return mDisable; }
            set { mDisable = value; }
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_PAINT)
            {
                using (var g = Graphics.FromHwnd(Handle))
                {
                    // Uncomment this if you don't want the "highlight border".

                    using (var p = new Pen(this.BorderColor, 1))
                    {
                        g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
                    }
                    using (var p = new Pen(this.BorderColor, 2))
                    {
                        g.DrawRectangle(p, 0, 0, Width, Height);
                    }
                }
            }
        }
        
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900