Microsoft KB Archive/818812

= BUG: The BackColor and ForeColor Properties Do Not Show New Values After You Change the Colors =

Article ID: 818812

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-





SYMPTOMS
After you change the colors of the Background and Foreground menu items in the display settings in Control Panel, the default Windows colors appear for those menu items.

Incorrect values are contained in the BackColor property and the ForeColor property of the DrawItemEventArgs class that is received by the DrawItem event handler of a MenuItem.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
To reproduce the behavior on a Windows 2000-based system, follow these steps:   Open a text editor such as Notepad, and then paste the following code in a new file: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text;

namespace WindowsApplication1 {   public class Form1 : System.Windows.Forms.Form {       private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.ComponentModel.Container components = null;

public Form1 {                      InitializeComponent; }

protected override void Dispose( bool disposing ) {           if( disposing ) {               if (components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

private void InitializeComponent {           this.mainMenu1 = new System.Windows.Forms.MainMenu; this.menuItem1 = new System.Windows.Forms.MenuItem; this.menuItem2 = new System.Windows.Forms.MenuItem; this.textBox1 = new System.Windows.Forms.TextBox; this.textBox2 = new System.Windows.Forms.TextBox; this.menuItem3 = new System.Windows.Forms.MenuItem; this.menuItem4 = new System.Windows.Forms.MenuItem; this.SuspendLayout;

this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1}); this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem2,                                                                                   this.menuItem3,                                                this.menuItem4}); this.menuItem1.Text = &quot;File&quot;; this.menuItem2.Index = 0; this.menuItem2.OwnerDraw = true; this.menuItem2.Text = &quot;Open&quot;; this.menuItem2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem2_DrawItem); this.menuItem2.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem2_MeasureItem); this.textBox1.Location = new System.Drawing.Point(8, 136); this.textBox1.Name = &quot;textBox1&quot;; this.textBox1.Size = new System.Drawing.Size(272, 20); this.textBox1.TabIndex = 0; this.textBox1.Text = &quot;&quot;; this.textBox2.Location = new System.Drawing.Point(8, 160); this.textBox2.Name = &quot;textBox2&quot;; this.textBox2.Size = new System.Drawing.Size(272, 20); this.textBox2.TabIndex = 1; this.textBox2.Text = &quot;&quot;; this.menuItem3.Index = 1; this.menuItem3.Text = &quot;Close&quot;; this.menuItem4.Index = 2; this.menuItem4.Text = &quot;Exit&quot;; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(284, 185); this.Controls.AddRange(new System.Windows.Forms.Control[] {                                                                         this.textBox2,                                                                          this.textBox1}); this.Menu = this.mainMenu1; this.Name = &quot;Form1&quot;; this.Text = &quot;Form1&quot;; this.ResumeLayout(false); }

[STAThread] static void Main {           Application.Run(new Form1); }

private void menuItem2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)       { textBox1.Text = &quot;BackColor: &quot; + ColorToString(e.BackColor); textBox2.Text = &quot;ForeColor: &quot; + ColorToString(e.ForeColor); e.DrawBackground ; SolidBrush sb = new SolidBrush (e.ForeColor); e.Graphics.DrawString (&quot;Open&quot;, e.Font, sb, e.Bounds.Left + 2, e.Bounds.Top + 2); sb.Dispose ; }

private string ColorToString(Color clr) {           StringBuilder sb = new StringBuilder(100); sb.Append (clr.Name); sb.Append (&quot; A:&quot;); sb.Append (clr.A); sb.Append (&quot; R:&quot;); sb.Append (clr.R); sb.Append (&quot; G:&quot;); sb.Append (clr.G); sb.Append (&quot; B:&quot;); sb.Append (clr.B); return sb.ToString; }

private void menuItem2_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)       { e.ItemHeight = 20; e.ItemWidth = 50; }   } }  Save the file as Sample.cs . Click Start, point to Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt. From a command prompt, run the following command to compile the C# code:csc sample.cs Open Control Panel, and then double-click Display. Click the Appearance tab, and then click the Advanced button.</li> In the Item combo box, select Menu, and then set the background color to green and the foreground color to pink.</li> Click OK to close the dialog box.</li> From a command prompt, run the following executable (generated in step 2): sample.exe</li> On the File menu, click DrawItemEventArgs to view the BackColor and ForeColor properties.

You expect to see the RGB color values that you set (green for the background and pink for the foreground) for the menu. However, the colors that appear are the default Windows colors that are associated with that window item.</li></ol>

<div class="references_section">