Edit

Share via


How to: Add Controls Without a User Interface to Windows Forms

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.

Add a component to a Windows Form

  1. Open the form in Visual Studio. For details, see How to: Display 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 because 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.

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.

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

    Public Sub CreateTimer()
       Dim timerKeepTrack As New System.Windows.Forms.Timer
       timerKeepTrack.Interval = 1000
    End Sub
    
    public void createTimer()
    {
       System.Windows.Forms.Timer timerKeepTrack = new
           System.Windows.Forms.Timer();
       timerKeepTrack.Interval = 1000;
    }
    
    public:
       void createTimer()
       {
          System::Windows::Forms::Timer^ timerKeepTrack = gcnew
             System::Windows::Forms::Timer();
          timerKeepTrack->Interval = 1000;
       }
    

    Important

    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