The TextBox control in Windows WPF programs is used to allow users to type in one or more lines of text. The content of the TextBox is stored in a string property called Text. Most of the other controls in WPF store their content in a generic System.Object.
TextBox
The TextBox control is the most basic text-input control found in WPF. It does however have a lot of features. The user can type in their text either on a single line or multiple lines. They can also Copy it in because TextBox supports Cut, Copy, Paste, Undo and Refdo commands. If you set the AcceptsReturn and/or AcceptsTab then the user many enter these.
The content of a TextBox is not stored as a generic System.Object. The content is stored in a string property called Text. On the GUI the TextBox grows and shrinks in size to accommodate the content unless you constrain the size of the TextBox.
It’s not very interesting to look at, but here are a few TextCom controls and one RichTextBox control at the bottom. There are a lot of things you can do with the TextBox.
Below is the XAML code that produces the above screenshot.
<Window x:Class="TextBoxControls.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TextBoxControls" mc:Ignorable="d" WindowStartupLocation="CenterScreen" Title="TextBoxControls" Height="350" Width="600"> <StackPanel Margin="10"> <WrapPanel HorizontalAlignment="Left" Margin="3"> <Label><TextBox/></Label> <TextBox/> <!-- very simple markup --> </WrapPanel> <WrapPanel HorizontalAlignment="Left" Margin="3"> <Label>TextBox Width=150</Label> <TextBox Width="150"/> </WrapPanel> <WrapPanel HorizontalAlignment="Left" Margin="3"> <Label>TextBox Width=150 Wrap</Label> <TextBox Width="150" TextWrapping="Wrap"/> </WrapPanel> <WrapPanel HorizontalAlignment="Left" Margin="3"> <Label>TextBox Width=150 Wrap Accepts Tab and Return</Label> <TextBox Width="150" TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True"/> </WrapPanel> <WrapPanel HorizontalAlignment="Left" Margin="3"> <Label>TextBox Width=150 SpellCheck.IsEnabled = True</Label> <TextBox Width="150" SpellCheck.IsEnabled="True"/> </WrapPanel> <WrapPanel HorizontalAlignment="Left" Margin="3"> <Label>TextBox press A or B</Label> <TextBox Name="txtBoxKey" Width="150" KeyDown="TxtBoxKey_KeyDown"/> </WrapPanel> <WrapPanel HorizontalAlignment="Left"> <Label>Rich Text Box</Label> <RichTextBox></RichTextBox> </WrapPanel> </StackPanel> </Window>
Below is the code-behind.
using System.Windows; using System.Windows.Input; namespace TextBoxControls { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void TxtBoxKey_KeyDown(object sender, KeyEventArgs e) { // This code is here just for illustration. // keyboard events are a part of validating user input. // This is here to just get us started thinking about it. switch (e.Key) { case Key.A: MessageBox.Show("You pressed A or a."); break; case Key.B: MessageBox.Show("You pressed B or b."); break; } } } }