似乎當您設定 TextBox 的 BackColor 時,Readonly 從那時起不再影響 BackColor。我有用戶控制,裝飾引擎跟蹤驗證,并在驗證值時以某種顏色突出顯示文本框。但是,一旦我將 Color.Yellow 或 SystemColors.Window 分配給 TextBox.BackColor,設定 TextBox.Readonly 就什么也不做。
這非常容易重現:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.ReadOnly = !textBox1.ReadOnly;
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.ReadOnly = !textBox2.ReadOnly;
}
private void button3_Click(object sender, EventArgs e)
{
textBox2.BackColor = Color.Blue;
}
private void textBox1_ReadOnlyChanged(object sender, EventArgs e)
{
label1.Text = $"Readonly: {textBox1.ReadOnly}{Environment.NewLine}Color: {textBox1.BackColor}";
}
private void textBox2_ReadOnlyChanged(object sender, EventArgs e)
{
label2.Text = $"Readonly: {textBox2.ReadOnly}{Environment.NewLine}Color: {textBox2.BackColor}";
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(138, 286);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click = new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(219, 286);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click = new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(300, 286);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 4;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click = new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(108, 190);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 22);
this.textBox1.TabIndex = 5;
this.textBox1.ReadOnlyChanged = new System.EventHandler(this.textBox1_ReadOnlyChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(308, 190);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 22);
this.textBox2.TabIndex = 6;
this.textBox2.ReadOnlyChanged = new System.EventHandler(this.textBox2_ReadOnlyChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(105, 215);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(76, 17);
this.label1.TabIndex = 7;
this.label1.Text = "Readonly: ";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(305, 215);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 17);
this.label2.TabIndex = 8;
this.label2.Text = "Readonly:";
//
// Form1
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
只需運行該表單,重復單擊 button1 和 button2,隨著只讀屬性的更改,兩個文本框將從默認視窗回圈到控制背景顏色。Button3,將textbox2的背景色設定為藍色,從那時起,點擊button2對背景色沒有任何影響。
我不想要這種行為。我想影響背景色,但我希望 readonly 取代我分配的任何自定義背景色。
我如何實作這一目標?
uj5u.com熱心網友回復:
據我所知,您的問題是如何實作一種視覺狀態,其中文本框顏色使用處于狀態時的默認值,而SystemColors.Control不是使用不同的自定義顏色。TextBoxReadOnly
我知道的最簡單的方法是處理控制元件的ReadOnlyChanged事件。TextBox例如:
public Form1()
{
InitializeComponent();
textBox2.ReadOnlyChanged = (sender, e) =>
{
textBox2.BackColor = textBox2.ReadOnly ? SystemColors.Control : Color.Blue;
};
}
并在設定自定義背景顏色時首先檢查狀態:
private void button3_Click(object sender, EventArgs e)
{
textBox2.BackColor = textBox2.ReadOnly ? SystemColors.Control : Color.Blue;
}
uj5u.com熱心網友回復:
好吧,顯然,由于“原因”,沒有“光滑”的方式來實作這一點,但我確實發現如果你設定 BackColor = Color.Empty,只讀行為會回傳。這是由于 TextBoxBase 中不可更改的代碼:
public override Color BackColor {
get {
if (ShouldSerializeBackColor()) {
return base.BackColor;
}
else if (this.ReadOnly) {
return SystemColors.Control;
} else {
return SystemColors.Window;
}
}
set {
base.BackColor = value;
}
}
如果將背景色設定為 Color.Empty,ShouldSerializeBackColor() 將回傳 false,從而允許處理 if-else 條件的第二部分,瞧。它不漂亮,但至少它有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/497120.html
下一篇:CefSharp顯示奇數??邊距
