Hallo,
ich habe ein komisches Phänomen welches ich nicht ganz verstehe warum es so ist.
Eine Checkbox und ein Label sind in einer Form, bei beiden wird zur Laufzeit die BackColor gesetzt, wenn man nun Enabled auf false stellt dann haben die beiden Controls unterschiedliche Font-Farbe...
Im Anhang habe ich ein Screenshot zur Verdeutlichung angehängt.
und nun der Quellcode:
Form1
/// <summary>
/// Form1 test dialog
/// </summary>
public partial class Form1 : Form
{
/// <summary>
/// Initializes a new instance of the <see cref="Form1" /> class.
/// </summary>
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
checkBox1.Enabled = !checkBox1.Enabled;
label3.Enabled = !label3.Enabled;
}
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.BackColor = Color.FromArgb(255, 250, 240, 230);
label3.BackColor = Color.FromArgb(255, 250, 240, 230);
}
}
Form1.Designer
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(30, 54);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1_Click);
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.BackColor = System.Drawing.SystemColors.Control;
this.checkBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.checkBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.checkBox1.Location = new System.Drawing.Point(240, 32);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(129, 28);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = false;
//
// label3
//
this.label3.AutoSize = true;
this.label3.BackColor = System.Drawing.SystemColors.Control;
this.label3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label3.Location = new System.Drawing.Point(261, 75);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(66, 24);
this.label3.TabIndex = 1;
this.label3.Text = "label3";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(439, 144);
this.Controls.Add(this.label3);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Label label3;
}
Hat jemand eine Idee warum es so ist und wie man es verhindern kann, bzw. dass es sich gleich verhält...
danke
Steffen
Du könntest OnPaint überschreiben und dort die Farbe selber setzen, die das Label bei Enabled = False annehmen soll:
public class MyLabel : Label
{
public MyLabel()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
if (!Enabled)
{
SolidBrush brush = new SolidBrush(Color.Yellow);
e.Graphics.DrawString(this.Text, this.Font, brush, 0f, 0f);
}
else
{
base.OnPaint(e);
}
}
}
Alternativ könntest Du natürlich auch Deine Checkbox Teilen in die Checkbox ohne Text und ein extra Label mit dem Text für die Checkbox. Du hast dann eben nur 2 Controls zum Dis-/Enablen statt nur eins.
Hallo,
die Workarounds sind schön und gut, aber ich verstehe nicht warum sich diese Controls unterschiedlich verhalten!?
Vielleicht muss ich nur noch irgendeine Property setzen!?
Also auf die schnelle sehe ich z.b. folgendes:
this.checkBox1.UseVisualStyleBackColor = false;
diese Angabe fehlt beim Label.
Hilft das?
MfG
ProGamer*Der Sinn Des Lebens Ist Es, Den Sinn Des Lebens Zu Finden!
*"Wenn Unrecht zu Recht wird dann wird Widerstand zur Pflicht."
*"Ignorance simplifies ANY problem."
*"Stoppt die Piraterie der Musikindustrie"
diese Eigenschaft besitzt ein Windows.Forms.Label nicht
Label-Eigenschaften
Wenn ich die Eigenschaft UseVisualStyleBackColor der Checkbox ändere, dann verhält es sich genauso.
Hallo steffen_dec,
damit hast du dann doch sowohl Ursache als auch Lösung.
herbivore
Hallo,
ich habe es nun so gelöst dass ich beim deaktivieren einer Checkbox ein Label anzeige welches direkt darüber liegt. So habe ich identisches Verhalten wie bei einem Label.
Grüße
Steffen