How to set the BorderStyle property for user controls by using Visual C++ .NET or Visual C++ 2005 (816178)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft .NET Framework 1.1
For a Microsoft Visual Basic .NET version of this
article, see
318821. For a Microsoft Visual C# .NET version of this
article, see
316574. This article refers to the following
Microsoft .NET Framework Class Library namespaces:
- System::ComponentModel
- System::Collections
- System::Windows::Forms
- System::Data
- System::Drawing
IN THIS TASKSUMMARYThis step-by-step article describes how to set a custom
border style for a user control in Microsoft Visual C++ .NET or in Microsoft Visual C++ 2005. By default, a user control does not have a BorderStyle property. The BorderStyle property specifies the border style for controls that have a
changeable border. Back to the
topRequirementsThis
article assumes that you are familiar with the following topics:
- Windows Forms Application
- User Controls
The following list outlines the recommended hardware, software,
network infrastructure, and service packs that you need: - One of the following operating systems with the Microsoft .NET Framework version 1.1 installed:
- Microsoft Windows 2000 Professional
- Microsoft Windows 2000
Server
- Microsoft Windows XP Professional
- Microsoft Windows Server 2003
- Microsoft Visual Studio .NET 2003 Enterprise Edition or Microsoft
Visual Studio .NET 2003 Enterprise Architect Edition
Back to the topCreate a user control - Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- Click Visual C++ Projects under
Project Types, and then click Windows Control Library
(.NET) under Templates.
Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates. - In the Name text box, type
BorderedUserControlSample, and then click
OK.
Back to the topCustomize control
properties- Paste the following code before the BorderedUserControlSample namespace statement in the Form1.h file:
#define WS_BORDER 0x800000
#define WS_EX_CLIENTEDGE 0x200
- Paste the following member declarations in the BorderedUserControlSampleControl class:
private:
System::Windows::Forms::BorderStyle borderStyleValue;
System::Windows::Forms::TextBox *textName;
System::Windows::Forms::TextBox *textAddress;
System::Enum *e;
System::Drawing::Size size;
- In the BorderedUserControlSampleControl() constructor, locate the call to the InitializeComponent() function, and then paste the following code after the InitializeComponent function:
this->textName = new System::Windows::Forms::TextBox();
this->textAddress = new System::Windows::Forms::TextBox();
textName->Location = System::Drawing::Point(50, 8);
textName->Size = System::Drawing::Size(100, 20);
textName->TabIndex = 0;
textAddress->Location = System::Drawing::Point(50, 32);
textAddress->Size = System::Drawing::Size(100, 20);
textAddress->TabIndex = 1;
// Add the controls to the user control.
this->Controls->Add(textName);
this->Controls->Add(textAddress);
// Size the user control.
size = System::Drawing::Size(200, 100);
- Paste the following code in BorderedUserControlSampleControl class after the Dispose() function:
public:
// Function to get for the borderStyleValue variable. This
// function is a getter function for borderStyleValue variable.
__property System::Windows::Forms::BorderStyle get_BorderStyle()
{
return borderStyleValue;
}
// Function to set for the borderStyleValue variable. This
// function is a setter function for borderStyleValue variable.
__property void set_BorderStyle(System::Windows::Forms::BorderStyle val)
{
if(borderStyleValue != val)
{
borderStyleValue = val;
UpdateStyles();
}
}
protected:
// Setter function for the CreateParams property. This is
// a overridden function.
__property System::Windows::Forms::CreateParams* get_CreateParams()
{
System::Windows::Forms::CreateParams *cp = System::Windows::Forms::UserControl::get_CreateParams();
cp->set_ExStyle((cp->get_ExStyle())&(~WS_EX_CLIENTEDGE)) ;
cp->set_Style((cp->get_Style()) & (~WS_BORDER));
switch(get_BorderStyle())
{
case System::Windows::Forms::BorderStyle::Fixed3D:
cp->ExStyle = cp->ExStyle | WS_EX_CLIENTEDGE;
break;
case System::Windows::Forms::BorderStyle::FixedSingle:
cp->Style = cp->Style | WS_BORDER;
break;
default:
;
}
return cp;
}
Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site: Back to the top Compile the projectTo compile the project, click Build Solution on the Build menu. This generates a .dll file. Back to the
topAdd a reference to the user control- On the File menu, point to Add
Project, and then click New Project.
- Click Visual C++ Projects under
Project Types, and then click Windows Forms
Application (.NET) under Templates.
Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates. - In the Name text box, type
SampleApplication, and then click
OK.
- In Solution Explorer, right-click
SampleApplication, and then click Add
Reference.
- On the Projects tab, click
BorderedUserControlSample.
- Click Select, and then click
OK.
Note In Visual Studio 2005, you do not have to click Select. - Open the Form1.h file.
- Paste the following code before the Form1 class declaration:
using namespace BorderedUserControlSample; - Use following code to declare one member variable in the Form1 class:
private:
BorderedUserControlSample::BorderedUserControlSampleControl *myUserControl;
- In the Form1() constructor, locate the call to the InitializeComponent() function, and then paste the following code after the InitializeComponent function:
myUserControl = new BorderedUserControlSample::BorderedUserControlSampleControl();
myUserControl->set_BorderStyle(System::Windows::Forms::BorderStyle::Fixed3D);
this->Controls->Add(myUserControl);
- In Solution Explorer, right-click
SampleApplication, and then click Set as
StartUp Project.
- Press the CTRL+SHIFT+B key combination to build the
solution.
- Press the CTRL+F5 key combination to run the
program.
Two text boxes appear inside a Fixed3D border in Form1. Back to the
topREFERENCESFor more information about the Systems.Windows.Forms.UserControl
class, visit the following Microsoft
Developer Network (MSDN) Web site: For more information about the BorderStyle property, visit the following MSDN Web site: Back to the top
| Modification Type: | Major | Last Reviewed: | 1/5/2006 |
|---|
| Keywords: | kbstyle kbCtrl kbControl kbProperties kbWindowsForms kbHOWTOmaster KB816178 kbAudDeveloper kbAudITPRO |
|---|
|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|