C# Windows Forms TextBox Examples

Let us look at a simple C# Windows Forms TextBox example.

Example 1: TextBox

This example will comprise the following files:

  • TextBox.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). TextBox.cs

Create a file named TextBox.cs

Here is the full code

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.Run(new MainForm());

    public MainForm() {
      Text = "TextBox example";

      textBox1.Parent = this;
      textBox1.Text = "textBox1";
      textBox1.Location = new System.Drawing.Point(10, 10);

      textBox2.Parent = this;
      textBox2.Text = "textBox2";
      textBox2.Location = new System.Drawing.Point(10, 50);
      textBox2.GotFocus += delegate {
        Debug.WriteLine(string.Format("Got Focus"));
      textBox2.LostFocus += delegate {
        Debug.WriteLine(string.Format("Lost Focus"));

      ActiveControl = textBox2;

    private TextBox textBox1 = new TextBox();
    private TextBox textBox2 = new TextBox();


Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.


Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts