Export (0) Print
Expand All

Adding Controls Without a User Interface to Windows Forms

Visual Studio .NET 2003

A nonvisual control (or component) provides functionality to your application. Unlike other controls, components do not provide a user interface to the user and thus do not need to be displayed on the Windows Forms Designer surface. When a component is added to a form, the Windows Forms Designer displays a resizable tray at the bottom of the form where all components are displayed. Once a control has been added to the component tray, you can select the component and set its properties as you would any other control on the form.

To add a component to a Windows Form

  1. Open the form. For details, see Displaying Windows Forms in the Designer.
  2. In the Toolbox, click a component and drag it to your form.

    Your component appears in the component tray.

Furthermore, components can be added to a form at run time. This is a common scenario, especially in light of the fact that components do not have a visual expression, unlike controls that have a user interface. In the example below, a Timer component is added at run time. (Note that Visual Studio contains a number of different timers; in this case, use a Windows Forms Timer component. For more information about the different timers in Visual Studio, see Introduction to Server-Based Timers.)

Caution   Components often have control-specific properties that must be set for the component to function effectively. In the case of the Timer component below, you set the Interval property. Be sure, when adding components to your project, that you set the properties necessary for that component.

To add a component to a Windows Form programmatically

  1. Create an instance of the Timer class in code.
  2. Set the Interval property to determine the time between ticks of the timer.
  3. Configure any other necessary properties for your component. In the case of the Timer class, see Timer Members.

    The following code shows the creation of a Timer object with its Interval property set.

    ' Visual Basic
    Public Sub CreateTimer()
       Dim timerKeepTrack As New System.Windows.Forms.Timer
       timerKeepTrack.Interval = 1000
    End Sub
    
    // C#
    public void createTimer()
    {
       System.Windows.Forms.Timer timerKeepTrack = new
           System.Windows.Forms.Timer();
       timerKeepTrack.Interval = 1000;
    }
    
    // C++
    public:
       void createTimer()
       {
          System::Windows::Forms::Timer *timerKeepTrack = new
             System::Windows::Forms::Timer();
          timerKeepTrack->Interval = 1000;
       }
    
    Security Note   You might expose your local computer to a security risk through the network by referencing a malicious UserControl. This would only be a concern in the case of a malicious person creating a damaging custom control, followed by you mistakenly adding it to your project.

See Also

Windows Forms Controls | Adding Controls to Windows Forms | Adding ActiveX Controls to Windows Forms | Copying Controls Between Windows Forms | Putting Controls on Windows Forms | Labeling Individual Windows Forms Controls and Providing Shortcuts to Them | Controls You Can Use on Windows Forms | Windows Forms Controls by Function | Creating Windows Forms

Show:
© 2014 Microsoft