Microsoft KB Archive/816178

= How to set the BorderStyle property for user controls by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 816178

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * 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 TASK

 * SUMMARY
 * Requirements
 * Create a user control
 * Customize control properties
 * Compile the project
 * Add a reference to the user control
 * REFERENCES



SUMMARY
This 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 top

Requirements
This 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 top

Create a user control

 * 1) Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) 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.
 * 1) In the Name text box, type BorderedUserControlSample, and then click OK.

Back to the top

Customize control properties
  Paste the following code before the BorderedUserControlSample namespace statement in the Form1.h file:  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;
 * 1) define WS_BORDER 0x800000
 * 2) define WS_EX_CLIENTEDGE 0x200 

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  Properties.

Note  is a placeholder for the name of the project. Expand Configuration Properties, and then click General.</li> 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.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li></ol>

Back to the top

Compile the project
To compile the project, click Build Solution on the Build menu. This generates a .dll file.

Back to the top

Add a reference to the user control
<ol> On the File menu, point to Add Project, and then click New Project.</li> 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.</li> In the Name text box, type SampleApplication, and then click OK.</li> In Solution Explorer, right-click SampleApplication, and then click Add Reference.</li> On the Projects tab, click BorderedUserControlSample.</li> Click Select, and then click OK.

Note In Visual Studio 2005, you do not have to click Select.</li> Open the Form1.h file.</li>  Paste the following code before the Form1 class declaration: using namespace BorderedUserControlSample; </li>  Use following code to declare one member variable in the Form1 class: private: BorderedUserControlSample::BorderedUserControlSampleControl *myUserControl; </li>  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); </li> In Solution Explorer, right-click SampleApplication, and then click Set as StartUp Project.</li> Press the CTRL+SHIFT+B key combination to build the solution.</li> Press the CTRL+F5 key combination to run the program.

Two text boxes appear inside a Fixed3D border in Form1.</li></ol>

Back to the top

<div class="references_section">