有没有办法禁用组合框的值更改而不在Windows窗体中将其变灰?我看到了一些帖子,但他们是为WPF而且没有帮助我的情况。
答案 0 :(得分:6)
在你的comobobox
上设置这些将完成您正在寻找的技巧,Combo已启用,但没有人可以更改或输入任何内容,因此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;
如果由于某种原因你不能使用lambdas,则可以关联跟随处理程序。右键单击 - >如果您有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;
//}
答案 1 :(得分:0)
然后将他的处理程序保存为变量,简单地 - = after after。
示例:
var keyDown = (s, e) => e.Handled = true;
var keyPress = (s, e) => e.Handled = true;
var keyUp = (s, e) => e.Handled = true;
然后替换为:
comboBox1.KeyDown += keyDown;
comboBox1.KeyPress += keyPress;
comboBox1.KeyUp += keyUp;
然后当你想删除时:
comboBox1.KeyDown -= keyDown;
comboBox1.KeyPress -= keyPress;
comboBox1.KeyUp -= keyUp;
答案 2 :(得分:0)
在GotFocus手中(不管它叫什么),将焦点设置为其他东西。
答案 3 :(得分:0)
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;
}
}
}
当您重新启用它时,您在Win7上获得的非常轻微的“发光”非常有趣。