Common use of a Textbox is editing unformatted text in a form. suppose if you want to show simple data entry form like client contact form in which you need to enter some detail about client such as phone number, Name, address etc ., then we can use textbox control.
In this article we will discuss how to use textbox in XAML.
Creating a Textbox:
Single line and multiline
The following example shows how to create textbox in xaml.
<Window x:Class=”Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”Window1″ Height=”300″ Width=”500″>
<TextBox Height=”30″ Width=”200″ MaxLength=”100″></TextBox>
</Window>
this textbox accept only single line.you can see above textbox has height 30px, width 200px and with 100 character length.
We can create multiline textbox with wrapping text feature like this:
<Window x:Class=”Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”Window1″ Height=”300″ Width=”500″>
<TextBox Height=”100″ Width=”200″ MaxLength=”100″
Name=”txtTextBox”
TextWrapping=”Wrap”
AcceptsReturn=”True”
VerticalScrollBarVisibility=”Visible”
></TextBox>
</Window>
This Textbox will allow the user to enter multiple lines of text. When the enter key is pressed, or when typed text reaches the edge of the text box, a new line is automatically inserted.
We are using four attributes also in this textbox:
Name, we can set the name of the control
Textformatting that gives ability to enter mulitiline.
VerticalScrollBarVisibility that show the vertical scrollbar in the textbox and
Acccept return.
How to Set Background and Foreground Colors
<TextBox Height=”100″ Width=”200″ MaxLength=”100″
Name=”tbMultiLine” Background=”skyblue” Foreground=”Black”></TextBox>
How to enable Spellchecking
You can enable real-time spellchecking in a TextBox. When spellchecking is turned on, a red line appears underneath any misspelled words (see picture below).
<TextBox Height=”100″ Width=”200″ MaxLength=”100″
Name=”tbMultiLine” SpellCheck.IsEnabled=”True”></TextBox>
How to Set Font
<TextBox Height=”100″ Width=”200″ MaxLength=”100″
Name=”tbMultiLine”
FontFamily=”Calibri” FontSize=”11″ FontStyle=”Italic” FontWeight=”ExtraBold”></TextBox>
Context Menu
By default, TextBox have a context menu that appears when a user right-clicks inside the control. The context menu allows the user to cut, copy, or paste.
AutoWordSelection
<TextBox Height=”100″ Width=”200″ MaxLength=”100″
Name=”tbMultiLine” AutoWordSelection=”True”></TextBox>
TextAlignment
<TextBox Height=”30″ Width=”200″ MaxLength=”100″
Name=”tbMultiLine” TextAlignment=”Right”></TextBox>