TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to WPF

WPF TextBox Example

Use the WPF TextBox control with the TextChanged event handler and the Text property.
TextBox, WPF. A TextBox holds user input or program output. With TextBoxes, we present a user-editable box for input. And in some cases, a TextBox is an effective way to display program output. It is easy to add to a WPF program.
To start, please create a C# WPF project. From the Toolbox panel, drag a TextBox control to the designer window. Now, in the XAML markup, changed the Text attribute to be an empty string.

Tip: The Text property indicates the string displayed (or typed by the user) into the TextBox.

Also, add a TextChanged attribute. The Visual Studio 2012 editor will offer to insert a new event handler (TextBox_TextChanged) after you type "TextChanged" in the XAML. We show the C# event handler code after the XAML.
Example markup: XAML <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/> </Grid> </Window> Example code: C# using System.Windows; using System.Windows.Controls; namespace WpfApplication2 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { // ... Get control that raised this event. var textBox = sender as TextBox; // ... Change Window Title. this.Title = textBox.Text + "[Length = " + textBox.Text.Length.ToString() + "]"; } } }
Text. Every TextBox has a Text property. When accessed, this property always returns a string that indicates the characters currently inside the TextBox. It can also be assigned—this changes the current contents.

Info: In the example, we access the Text property twice—first to assign it to the Title, and then to get its length.

PropertyString Length
TextChanged. The TextChanged event is hooked up through the XAML file with the "TextChanged" attribute. This is triggered whenever the Text property of the TextBox changes. It is triggered at startup if a default Text attribute is set.

Tip: In TextChanged, you can access the source of the event by casting the "sender" object parameter.

AsObject

Also: The TextChangedEventArgs argument contains details of the changes made on the TextBox.

AcceptsReturn. TextBox has two important properties that determine how it may be used. The AcceptsReturn attribute, when set to true, allows you to use the TextBox as a multiline input field. This enables users to enter longer blocks of text.

AcceptsTab: Normally the TextBox does not accept tabs. Instead, focus changes to the next control. AcceptsTab changes this behavior.

Tip: In my experience, these two "accepts" properties are important in many programs. They expand the domain of uses for this control.

Padding. For some TextBoxes, such as ones that only show one line, padding is not needed. But for larger controls, such as multiline ones, adjusting the Padding attribute is helpful. This makes an important visual improvement.

Screenshot: The image shows a TextBox, from a real program, that has padding of 4. This is inner padding.

And: Padding helps when the TextBox is right on the window edge. A margin can sometimes alleviate the need for this much Padding.

Summary. We created a TextBox in a WPF program. And we discovered how to capture user input into this TextBox (with TextChanged). We finally read the Text property, and assigned the Window Title.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf