In trực tiếp trong windows form

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

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

  • Bài viết
  • 07/20/2022
  • 2 phút để đọc

Trong bài viết này

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

  • Windows Forms Controls
  • How to: Add Controls to Windows Forms
  • How to: Add ActiveX Controls to Windows Forms
  • Putting Controls on Windows Forms
  • Labeling Individual Windows Forms Controls and Providing Shortcuts to Them
  • Controls to Use on Windows Forms
  • Windows Forms Controls by Function

Phản hồi

Gửi và xem ý kiến phản hồi dành cho

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Control.Enter Event

  • Reference

Definition

In this article

Occurs when the control is entered.

public:
 event EventHandler ^ Enter;
public event EventHandler Enter;
public event EventHandler? Enter;
member this.Enter : EventHandler 
Public Custom Event Enter As EventHandler 

Event Type

EventHandler

Examples

The following code example uses the Enter event to change the foreground and background colors of a TextBox under particular conditions.

private:
   void textBox1_Enter( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // If the TextBox contains text, change its foreground and background colors.
      if ( textBox1->Text != String::Empty )
      {
         textBox1->ForeColor = Color::Red;
         textBox1->BackColor = Color::Black;

         // Move the selection pointer to the end of the text of the control.
         textBox1->Select(textBox1->Text->Length,0);
      }
   }

   void textBox1_Leave( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Reset the colors and selection of the TextBox after focus is lost.
      textBox1->ForeColor = Color::Black;
      textBox1->BackColor = Color::White;
      textBox1->Select(0,0);
   }
private void textBox1_Enter(object sender, System.EventArgs e)
{
    // If the TextBox contains text, change its foreground and background colors.
    if (!string.IsNullOrEmpty(textBox1.Text))
    {
        textBox1.ForeColor = Color.Red;
        textBox1.BackColor = Color.Black;
        // Move the selection pointer to the end of the text of the control.
        textBox1.Select(textBox1.Text.Length, 0);
    }
}

private void textBox1_Leave(object sender, System.EventArgs e)
{
    // Reset the colors and selection of the TextBox after focus is lost.
    textBox1.ForeColor = Color.Black;
    textBox1.BackColor = Color.White;
    textBox1.Select(0,0);
}
    Private Sub textBox1_Enter(sender As Object, e As System.EventArgs) Handles textBox1.Enter
        ' If the TextBox contains text, change its foreground and background colors.
        If textBox1.Text <> [String].Empty Then
            textBox1.ForeColor = Color.Red
            textBox1.BackColor = Color.Black
            ' Move the selection pointer to the end of the text of the control.
            textBox1.Select(textBox1.Text.Length, 0)
        End If
    End Sub
   
   
    Private Sub textBox1_Leave(sender As Object, e As System.EventArgs) Handles textBox1.Leave
        ' Reset the colors and selection of the TextBox after focus is lost.
        textBox1.ForeColor = Color.Black
        textBox1.BackColor = Color.White
        textBox1.Select(0, 0)
    End Sub
End Class

Remarks

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

For more information about handling events, see Handling and Raising Events.

Applies to

See also

  • OnEnter(EventArgs)