CSharp/C# >> Application.EnableVisualStyles() breaks my program!

by Michael A. Covington » Wed, 21 Jun 2006 12:46:39 GMT

I added Application.EnableVisualStyles() as the first statement in Main() of
my program, and didn't notice much change in visual appearance, but what I
*did* notice is that some of my buttons stopped working! They were enabled
but no longer called their OnClick methods.

I tried adding Application.DoEvents() right after EnableVisualStyles and
that didn't help.

What gives? In VS 2005/.NET 2.0, is EnableVisualStyles broken? This is
under Windows XP.



CSharp/C# >> Application.EnableVisualStyles() breaks my program!

by Marc Gravell » Wed, 21 Jun 2006 14:22:27 GMT


Works fine from everything I have seen... Does it fix itself if you
remove this line again?

I suspect that what has happened is that at some point you have gone
into the form designer, and it has decided to drop your events from the
init step; this always happens if you cut / paste a control around on
the form, but the designer is also notorious for doing freaky things at
the most inoportune times.

Just check the current (form) class against an old (working) version -
see what changed; I suspect a missing "this.{control}.{event} += {blah
handler};".

And if you think events are brittle in the designer, don't go *near*
data-binding in the designer ;-p

Marc

CSharp/C# >> Application.EnableVisualStyles() breaks my program!

by Tom Spink » Wed, 21 Jun 2006 23:28:50 GMT


Hi Michael,

Without more information, I'm unable to reproduce your bug. However, a
couple of things to look for is the FlatStyle property of your buttons. It
needs to be set to System in order for them to render in the System's
visual style. Are you using the Windows Forms Designer? Do the visual
styles show up there while you are designing your form?

Thanks,
-- Tom Spink

CSharp/C# >> Application.EnableVisualStyles() breaks my program!

by Michael A. Covington » Thu, 22 Jun 2006 00:39:04 GMT


Thanks for responding. I'm going to check out the FlatStyle property, and
in fact any other property that might distinguish the afflicted buttons from
other ones. Also, the way I create my initial form is rather complicated
(several forms pop up at once when the program starts) and I'm going to
experiment with cutting out all the complexity (even if I lose part of the
program) to see if I can get the problem to go away.

What is really bizarre is that one of the buttons works OK until I put data
into a DataGridView that is on the same form, and then it stops working
properly.

Similar Threads

1. (More info) EnableVisualStyles breaks my program!

Further to previous post...

I have a big, complex program which works fine as long as I don't call 
EnableVisualStyles.

If I do this:

Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new FormOpeningMenu());

then in one of my subordinate forms, which consits of buttons on other 
things on a tab control, the non-focused buttons cease to work.  Only the 
focused on will actually call its OnClick procedure, although all of them 
look, visually, as if they are being pressed.

My other forms are unafflicted.

On the afflicted form, the focus is being set programmatically by things 
like button1.Select() or button1.Focus() (it doesn't matter which I use).

Does this ring any bells for anyone?

Many thanks!


2. EnableVisualStyles breaks PropertyGrid.. - .NET Windows Forms

3. bug using Application.EnableVisualStyles() with datetimepicker control

I have extended the datetimepicker control to incorporate 
a ReadOnly property.
I have used the new keyword to implement my own version 
of the value property, so that if readonly == true then 
it will not set the value of the control and will leave 
the checked status of the checkbox to false when a user 
selects a new date.

this works fine when using the control on a win2k machine 
but if we use it on a win XP box and call 
Application.EnableVisualStyles() then it seems to ignore 
my code and check the checkbox and set the value. this is 
extremely buggy behaviour! and also the value property 
gets called twice (only once on a win2k box).

i have also noticed behaviour changes in the 
label.textalignment property when using XP Visual styles 
aswell.  Below is my implementation of the extended 
datetimepicker and also the new data type 
(OptionalDateTime) that is used for the value property...

#####LockableDateTimePicker Source###########

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using HCS.DataTypes;

namespace HCS.Generic.UI.Controls
{
	/// <summary>
	/// LockableDateTimePicker is for selecting 
dates. It manipulates the OptionalDateTime 
	/// class to allow null dates to be handled.
	/// </summary>
	public class LockableDateTimePicker : 
DateTimePicker
	{
		#region Clean Up Code

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool 
disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose
();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		#region Component 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()
		{
			components = new 
System.ComponentModel.Container();
		}
		#endregion

		#region Fields

		private System.ComponentModel.Container 
components = null;
		private bool mReadOnly;
		private DateTime mDateTime;
		private Color mBackColor;
		private Color mLockedColor;
		private bool mChecked;

		#endregion

		#region Events

		public event OnReadOnlyChangedDelegate 
OnReadOnlyChanged;

		#endregion

		#region EventArgs and Delegates

		public class OnReadOnlyChangedEventArgs : 
EventArgs
		{
			private bool mReadOnly;

			public OnReadOnlyChangedEventArgs
(bool ReadOnly)
			{
				mReadOnly = ReadOnly;
			}
			public bool ReadOnly
			{
				get
				{
					return mReadOnly;
				}
			}
		}
		public delegate void 
OnReadOnlyChangedDelegate(object sender, 
OnReadOnlyChangedEventArgs e);
		
		#endregion

		#region Constructor

		public LockableDateTimePicker()
		{
			// This call is required by the 
Windows.Forms Form Designer.
			InitializeComponent();

			mBackColor = 
base.CalendarMonthBackground;
			mLockedColor = 
base.CalendarMonthBackground;

			//Set defaults for this control
			base.Format = 
DateTimePickerFormat.Short;

			//Make sure that our date backup 
is populated
			mDateTime = base.Value;
			mChecked = base.Checked;
		}

		#endregion

		#region Properties

		[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
		public bool ReadOnly
		{
			get
			{
				return mReadOnly;
			}
			set
			{
				if(value)
				{
				
	base.CalendarMonthBackground = mLockedColor;
				}
				else
				{
				
	base.CalendarMonthBackground = mBackColor;
				}

				mReadOnly = value;
				if(OnReadOnlyChanged != 
null)
				{
					OnReadOnlyChanged
(this, new OnReadOnlyChangedEventArgs(value));
				}
			}
		}

		[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
		public Color LockedColor
		{
			get
			{
				return mLockedColor;
			}
			set
			{
				mLockedColor = value;
			}
		}
	
		#endregion

		#region Public Overridden Properties
		public override Color BackColor
		{
			get
			{
				return base.BackColor;
			}
			set
			{
				mBackColor = value;
				if(!mReadOnly)
				{
					base.BackColor = 
value;
				}
			}
		}
		public new OptionalDateTime Value
		{
			set
			{
				if(value.GetValue() == "")
				{
					base.Checked = 
false;
				}
				else
				{
					base.Value = 
DateTime.Parse(value.GetValue());
				}

			}
			get
			{
				if(base.Checked)
				{
					return new 
OptionalDateTime(base.Value);
				}
				else
				{
					return new 
OptionalDateTime();
				}
			}
		}
		#endregion

		#region Public Overridden Events

		protected override void OnValueChanged
(EventArgs eventargs)
		{
			base.OnValueChanged (eventargs);

			if(mReadOnly)
			{
				//We need to set the 
value of the control back to
				//the stored value, since 
it is read only
				if(base.Value != 
mDateTime)
				{
					base.Value = 
mDateTime;
				}
				if(base.Checked != 
mChecked)
				{
					base.Checked = 
mChecked;
				}
			}
			else
			{
				//Store the value for 
when it's read only
				mDateTime = base.Value;
				mChecked = base.Checked;
			}
		}
	
		#endregion

		#region Public Methods
		public void Initialise(OptionalDateTime 
Value)
		{
			//Temporarily set the control to 
not ReadOnly.
			bool mTempReadOnly = mReadOnly;
			if(mReadOnly)
			{
				mReadOnly = false;
			}	

			if(Value.GetValue() == "")
			{
				base.Checked = false;
				mChecked = false;
			}
			else
			{
				base.Value = 
DateTime.Parse(Value.GetValue());
				base.Checked = true;
				mDateTime = base.Value;
				mChecked = true;
			}

			//Make sure the ReadOnly value is 
returned to normal
			mReadOnly = mTempReadOnly;
		}
		public void Initialise(DateTime Value)
		{
			Initialise(new OptionalDateTime
(Value));
		}
		public void Initialise()
		{
			Initialise(new OptionalDateTime
());
		}
		#endregion		
	}
}

##############OptionalDateTime Source##########

	[Serializable()]
	public class OptionalDateTime
	{
		#region Enum
		/// <summary>
		/// Formats available - extend as 
required, but remember to update <see cref="GetValue"/>.
		/// </summary>
		public enum enumDateTimeFormat
		{
			/// <summary>
			/// LongDateFormat
			/// </summary>
			LongDateFormat,
			/// <summary>
			/// LongTimeFormat
			/// </summary>
			LongTimeFormat,
			/// <summary>
			/// ShortDateFormat
			/// </summary>
			ShortDateFormat,
			/// <summary>
			/// ShortTimeFormat
			/// </summary>
			ShortTimeFormat
		}
		#endregion

		#region Fields
		private DateTime mDate;
		private bool mIsNull;
		#endregion

		#region Constructor
		/// <summary>
		/// Constructor - initialises a null 
OptionalDateTime
		/// </summary>
		public OptionalDateTime()
		{
			mIsNull = true;
		}
		/// <summary>
		/// Constructor - initialise an 
OptionalDateTime to contain the value of a string.
		/// If the string is not a valid 
DateTime, the object is set to contain a null date.
		/// </summary>
		/// <param name="value">A string 
representing a valid date.</param>
		public OptionalDateTime(string value)
		{
			SetValue(value);
		}
		/// <summary>
		/// Constructor - initialise an 
OptionalDateTime to contain the value of a DateTime.
		/// </summary>
		/// <param name="value">A DateTime value 
type.</param>
		public OptionalDateTime(DateTime value)
		{
			SetValue(value);
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Set the value of the object to equal 
that of a DateTime.
		/// </summary>
		/// <param name="value">A 
DateTime.</param>
		public void SetValue(DateTime value)
		{
			mDate = value;
			mIsNull = false;
		}
		/// <summary>
		/// Set the value of the object to equal 
that of a string. If the string is not a valid
		/// DateTime, the object is set to 
contain a null date.
		/// </summary>
		/// <param name="value">A string 
representing a valid date.</param>
		public void SetValue(string value)
		{
			if(value == null || value == "")
			{
				mIsNull = true;
			}
			else
			{
				try
				{
					mDate = 
DateTime.Parse(value);
					mIsNull = false;
				}
				catch
				{
					throw new 
ArgumentException("The string entered cannot be converted 
to a DateTime", "value");
				}
			}
		}
		/// <summary>
		/// Return the value of the object as a 
string with optional formatting.
		/// </summary>
		/// <param name="Format">The format to 
return.</param>
		/// <returns>A string containing the 
correctly formatted date.</returns>
		public string GetValue(enumDateTimeFormat 
Format)
		{
			if(mIsNull)
			{
				return "";
			}
			else
			{
				switch(Format)
				{
					case 
enumDateTimeFormat.LongDateFormat:
						return 
mDate.ToLongDateString();		
					case 
enumDateTimeFormat.LongTimeFormat:
						return 
mDate.ToLongTimeString();
					case 
enumDateTimeFormat.ShortDateFormat:
						return 
mDate.ToShortDateString();
					case 
enumDateTimeFormat.ShortTimeFormat:
						return 
mDate.ToShortTimeString();
					default:
						throw new 
UnhandledDateFormatException(Format);
				}
			}
		}
		/// <summary>
		/// Return the value of the object as a 
ShortDateString.
		/// </summary>
		/// <returns></returns>
		public string GetValue()
		{
			return GetValue
(enumDateTimeFormat.ShortDateFormat);
		}	
		#endregion

		#region Public Override Methods
		/// <summary>
		/// Passes a string containing the date 
in <see cref="enumDateTimeFormat.ShortDateFormat"/>.
		/// </summary>
		/// <returns>The date returned. Passes an 
empty string for blank (null) dates.</returns>
		public override string ToString()
		{
			return GetValue();
		}
		#endregion

		#region Properties
		/// <summary>
		/// Returns the date of the 
OptionalDateTime as a DateTime type. Raises the 
		/// <see 
cref="OptionalDateTimeIsNullException"/> if the 
OptionalDateTime is 
		/// blank (null). Check the <see 
cref="IsNull"/> property before calling this
		/// method to avoid the exception.
		/// </summary>
		public DateTime GetDateTime
		{
			get
			{
				if(mIsNull)
				{
					throw new 
OptionalDateTimeIsNullException();
				}
				return mDate;
			}
		}
		/// <summary>
		/// Gets a boolean value indicating 
whether the OptionalDateTime is blank (null).
		/// </summary>
		public bool IsNull
		{
			get
			{
				return mIsNull;
			}
		}
		#endregion
	}

thanks in advance

4. .NET 2.0 bug? Application.EnableVisualStyles destroys ListView icons

5. Image Gone when apply Application.enablevisualstyles()

Hi,

I found that when I apply application.enablevisualstyles. all the button
image and listview image are not shown up.

pls help me, anywhere to show the image when i apply that features

Thank you


6. Application.EnableVisualStyles() - .NET Windows Forms

7. Item image lost in TreeView using Application.EnableVisualStyles();

I want to let my app support XP theme,since I use .Net 
1.1 so I and the Application.EnableVisualStyles();
like this
static void Main() 
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}

but after that,my Treeview lost the item image!
Why?
Thank you! 

8. Application.EnableVisualStyles and no image in Tab control - .Net Framework