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 TASK

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.
  4. In the Name text box, type BorderedUserControlSample, and then click OK.
Back to the top

Customize control properties

  1. Paste the following code before the BorderedUserControlSample namespace statement in the Form1.h file:
    #define WS_BORDER  0x800000
    #define WS_EX_CLIENTEDGE 0x200
    
  2. 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;
    
  3. 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);
    
  4. 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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

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

  1. On the File menu, point to Add Project, and then click New Project.
  2. 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.
  3. In the Name text box, type SampleApplication, and then click OK.
  4. In Solution Explorer, right-click SampleApplication, and then click Add Reference.
  5. On the Projects tab, click BorderedUserControlSample.
  6. Click Select, and then click OK.

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

    Two text boxes appear inside a Fixed3D border in Form1.
Back to the top

REFERENCES

For 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:MajorLast Reviewed:1/5/2006
Keywords:kbstyle kbCtrl kbControl kbProperties kbWindowsForms kbHOWTOmaster KB816178 kbAudDeveloper kbAudITPRO