Article ID: 814738
Article Last Modified on 11/14/2007
APPLIES TO
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft Visual Basic 2005
SYMPTOMS
The window style flags that you set when you inherit from the CreateParams property are not applied to the control after you call the Control.SetTopLevel method if you follow these steps:
- You inherit from the System.Windows.Forms.Control class to create a control.
- You override the CreateParams property.
RESOLUTION
To resolve this bug, reapply the styles to the custom control. To do this, add the following statement after you call the SetTopLevel method:
Visual Basic .NET or Visual Basic 2005 Code
' Reapplies the assigned styles to the control. myCustomControl.UpdateStyles()
Visual C# .NET Code
// Reapplies the assigned styles to the control. myCustomControl.UpdateStyles();
STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.
MORE INFORMATION
Steps to Reproduce the Behavior
- In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, start a new class library by using Microsoft Visual Basic .NET, Microsoft Visual Basic 2005, or Microsoft Visual C# .NET.
By default, Class1 is created. In Class1, replace the existing code with the following code:
Visual Basic .NET CodeOption Explicit On Option Strict On Imports System Imports System.ComponentModel Imports System.Drawing Imports System.Collections Imports System.Windows.Forms Public Class MyControl Inherits System.Windows.Forms.Control Public label1 As System.Windows.Forms.Label Public Sub New() MyBase.New() InitializeComponent() 'Add a label to the control. label1 = New System.Windows.Forms.Label() label1.Size = New Size(200, 200) Me.Controls.Add(label1) End Sub 'Dispose any unused resources. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ComponentModel.IContainer <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() components = New System.ComponentModel.Container() Me.Size = New System.Drawing.Size(300, 300) End Sub Public Const WS_CAPTION As Integer = &HC00000 Public Const WS_DLGFRAME As Integer = &H400000 'Override the CreateParams property to assign styles to the control. Protected Overrides ReadOnly Property CreateParams() As CreateParams Get Dim cp As CreateParams cp = MyBase.CreateParams cp.Style = cp.Style And (Not WS_CAPTION) cp.Style = cp.Style Or WS_DLGFRAME Return cp End Get End Property Shared Sub main() 'Create a control and set the visible property to true. Dim myCustomControl As New MyControl() myCustomControl.Visible = True 'Set the control as the top-level control. myCustomControl.SetTopLevel(True) myCustomControl.UpdateStyles() myCustomControl.label1.Text = "EXPECTED: Border not shown." & vbCrLf & "Alt+F4 to close" myCustomControl.Location = New Point(0, 0) Application.Run(New ApplicationContext()) End Sub End Class
Visual C# .NET Code
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class MyControl : System.Windows.Forms.Control { private System.ComponentModel.Container components = null; public System.Windows.Forms.Label label1; public MyControl() { InitializeComponent(); //Add the label to the control. label1 = new System.Windows.Forms.Label(); label1.Size = new Size(200,200); this.Controls.Add(label1); } // Clean up any resources that are being used. protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); } private static int WS_DLGFRAME = 0x00400000; private static int WS_CAPTION= 0x00c00000; //Override the CreateParams property to assign styles to the control. protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style &= ~WS_CAPTION; cp.Style |= WS_DLGFRAME; return cp; } } [STAThread] static void Main() { //Create a control and set the Visible property to true. MyControl myCustomControl = new MyControl(); myCustomControl.Visible = true; //Set the control as the top-level control. myCustomControl.SetTopLevel(true); myCustomControl.UpdateStyles(); myCustomControl.label1.Text = "EXPECTED: Border not shown.\r\nAlt+F4 to close"; myCustomControl.Location = new Point(0,0); Application.Run(new ApplicationContext()); } }
- In Solution Explorer, right-click ClassLibrary1, and then click Add Reference.
- Click the .NET tab, click to select System.Drawing.dll and System.Windows.Forms.dll, and then click Select.
- In Solution Explorer, right-click ClassLibrary1, and then click Properties.
- In Output type, click to select Windows Application.
- In Startup object, click to select Control1.
- On the Debug menu, click Start.
The control is displayed with the title bar. Add the following code after you call the SetTopLevel method:
Visual Basic .NET or Visual Basic 2005 Code' Reapplies the assigned styles to the control. myCustomControl.UpdateStyles()
Visual C# .NET Code
// Reapplies the assigned styles to the control. myCustomControl.UpdateStyles();
- On the Debug menu, click Start to run the application.
The control is displayed without a title bar.
REFERENCES
For more information, visit the following MSDN Web site:
Control.CreateParams Property
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.createparams(vs.71).aspx
Keywords: kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbstyle kbproperties kbctrl kbcontrol kbbug KB814738