Describing the Use of TextBox Control in WPF

Introduction

In this article we will describe the use of the TextBox Control. WPF Textboxes can be used for entering a single line of text or for multiline editing. Besides this we can also put restrictions on the information entered, such as limiting the length of characters or character casing.

Background

 We have used the Text property of the TextBox class. This String property holds the text that is currently displayed in the Control.

Solution

Whatever information we will enter in the TextBox will be displayed on the label, in other words while writing in the TextBox the words will be automatically written in the label. And after clicking on the OK button the TextBox and the label will be cleared.

Procedure

Step 1: First we will define a label, TextBox and and OK button Control. We have provided values for the TextChanged property of the TextBox Class to the TextBox named as MyTextBox. We have also added the button Click property and passed the values for button_click event, the value passed is nothing but the name of the event, Button_Click. And the Label is named MyLabel.

<Window x:Class="TextBoxDemo.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="TextBox Demo"

        Height="200"

        Width="250">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition/>

            <RowDefinition/>

            <RowDefinition/>

            <RowDefinition/>

        </Grid.RowDefinitions>

 

        <Label Content="Enter some text" Margin="5"/>

        <TextBox Grid.Row="1"

                 Name="MyTextBox"

                 Margin="5"

                 MaxLength="15"

                 CharacterCasing="Upper"

                 TextChanged="MyTextBox_TextChanged"/>

        <Button Grid.Row="2" Content="OK" Margin="5" Click="Button_Click"/>

        <Label Grid.Row="3" Name="MyLabel" Margin="5"/>

    </Grid>

</Window>

 

Step 2 : Here, whatever text will is entered into the TextBox will be stored in the Content property of the label named MyLabel that is an object of the label class.

 private void MyTextBox_TextChanged(object sender, TextChangedEventArgs e)

        {

            MyLabel.Content = MyTextBox.Text;

        }

 

Step 3 : On Button_Click event all the TextBox text is cleared. For this, we have used the MyTextBox.Clear() method where MyTextBox is the name of the TextBox. Also with this all the label content will also be cleared because we have already assigned the text of MyTextBox to the content of MyLabel.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace TextBoxDemo

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

 

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            MyLabel.Content = MyTextBox.Text;

            MyTextBox.Clear();

        }

     

    }

}

Output

1. On running the appliaction you will get the following window.

 te1

2. Now when you write some text into the TextBox, it will simultaneously appear on the label shown below the OK button.
 
te2 
 
3. Similarly here, the label content goes on increasing with writing the text in the TextBox, so whatever you will write in the TextBox will be shown in the label also.
 
te3 

  

4. On pressing the OK button all the text of the TextBox Control and all the content of the Label Control will be cleared up simultaneously as shown below.
 
te4 
  

Up Next
    Ebook Download
    View all
    Learn
    View all