Can I disable the combo box in WinForms without sowing it?

Is there a way to disable the cost change for a combined field without highlighting it in windows? I saw some posts, but they were for WPF and did not help my situation.

+4
source share
4 answers

Setting these options on your comobobox will lead to the trick you are looking for, Combo is on, but no one can change or enter anything like that. Appearance = Enabled, Behavior = Disabled :)

  comboBox1.DropDownHeight = 1; comboBox1.KeyDown += (s, e) => e.Handled = true; comboBox1.KeyPress += (s, e) => e.Handled = true; comboBox1.KeyUp += (s, e) => e.Handled = true; 

If for some reason you cannot use lambdas, then subsequent handlers may be bound. Right click -> Paste if you have DropDownStyle = DropDown.

  //void comboBox1_KeyUp(object sender, KeyEventArgs e) //{ // e.Handled = true; //} //void comboBox1_KeyPress(object sender, KeyPressEventArgs e) //{ // e.Handled = true; //} //void comboBox1_KeyDown(object sender, KeyEventArgs e) //{ // e.Handled = true; //} 
+6
source

Then save its handlers as variables and simply - = after.

Example:

 var keyDown = (s, e) => e.Handled = true; var keyPress = (s, e) => e.Handled = true; var keyUp = (s, e) => e.Handled = true; 

Then replace in it:

 comboBox1.KeyDown += keyDown; comboBox1.KeyPress += keyPress; comboBox1.KeyUp += keyUp; 

Then when you want to remove:

 comboBox1.KeyDown -= keyDown; comboBox1.KeyPress -= keyPress; comboBox1.KeyUp -= keyUp; 
0
source

In the GotFocus hander (whatever it is called), set the focus to something else.

0
source

No, not enough. Turn it off and look exactly like the original so that the user is completely fooled. Add a new class and paste this code.

 using System; using System.Drawing; using System.Windows.Forms; class FakeComboBox : ComboBox { private PictureBox fake; public new bool Enabled { get { return base.Enabled; } set { if (!this.DesignMode) displayFake(value); base.Enabled = value; } } private void displayFake(bool enabled) { if (!enabled) { fake = new PictureBox(); fake.Location = this.Location; fake.Size = this.Size; var bmp = new Bitmap(fake.Size.Width, fake.Size.Height); this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); fake.Image = bmp; this.Parent.Controls.Add(fake); fake.BringToFront(); fake.Click += delegate { Console.Beep(); }; } else { this.Parent.Controls.Remove(fake); fake.Dispose(); fake = null; } } } 

The very small “glow” that you get on Win7 when you turn it back on is very interesting.

0
source

Source: https://habr.com/ru/post/1346147/


All Articles