The TextBox control is used to display text that the user can interact with, performing actions such as editing the text or copying it to the clipboard. It is used whenever a free-form response is needed from the user, such as answering the question “What is your name?” In the touch-first world of a Windows app, it is important to realize that the end user may not have a physical keyboard. This means you should expect the user to have to exert
greater effort to type an answer than to pick one from a list, and therefore you should not require typing when picking is an option.
These are the properties that you will most commonly use with TextBox:
• FontFamily: This property is used to specify the font that will be used to render the text
within the control.
• FontSize: This property is used to specify the size of the text within the control.
• IsEnabled: This property indicates whether the user can interact with the control in
any way.
• IsReadOnly: This property indicates whether the contents in the control should be
protected from editing.
• IsSpellCheckEnabled: This property indicates whether the text entered into the control
should have spell-checking performed upon it.
• IsTextPredictionEnabled: This property indicates whether the user should be presented
with autocompletion options for words while they are typing. Using this feature can be
a great help to reducing the effort your user has to exert when typing on an on-screen
keyboard or with their thumbs.
• MaxLength: This property controls the maximum length of the text that can be typed into
the control.
• Text: This property contains the actual text value in the control.
• Width: This property specifies the width of the control. If the length of the text entered
causes it to exceed the width of the control, the portion beyond the edge will not
be displayed.The following XAML can be used to display the TextBox shown in Figure :
<TextBox Text="The quick brown fox..." FontFamily="Segoe UI" FontSize="48" />
greater effort to type an answer than to pick one from a list, and therefore you should not require typing when picking is an option.
These are the properties that you will most commonly use with TextBox:
• FontFamily: This property is used to specify the font that will be used to render the text
within the control.
• FontSize: This property is used to specify the size of the text within the control.
• IsEnabled: This property indicates whether the user can interact with the control in
any way.
• IsReadOnly: This property indicates whether the contents in the control should be
protected from editing.
• IsSpellCheckEnabled: This property indicates whether the text entered into the control
should have spell-checking performed upon it.
• IsTextPredictionEnabled: This property indicates whether the user should be presented
with autocompletion options for words while they are typing. Using this feature can be
a great help to reducing the effort your user has to exert when typing on an on-screen
keyboard or with their thumbs.
• MaxLength: This property controls the maximum length of the text that can be typed into
the control.
• Text: This property contains the actual text value in the control.
• Width: This property specifies the width of the control. If the length of the text entered
causes it to exceed the width of the control, the portion beyond the edge will not
be displayed.The following XAML can be used to display the TextBox shown in Figure :
<TextBox Text="The quick brown fox..." FontFamily="Segoe UI" FontSize="48" />
You can prevent the user from interacting with the control in any way by changing the IsEnabled property to
False. This not only prevents interaction but also changes the way that the control is drawn by giving it a “grayedout”
appearance that users are accustomed to in order to indicate a disabled control. The following XAML
produces the disabled TextBox shown in Figure :
<TextBox Text="The quick brown fox..." IsEnabled="False" FontFamily="Segoe UI" FontSize="48" />
False. This not only prevents interaction but also changes the way that the control is drawn by giving it a “grayedout”
appearance that users are accustomed to in order to indicate a disabled control. The following XAML
produces the disabled TextBox shown in Figure :
<TextBox Text="The quick brown fox..." IsEnabled="False" FontFamily="Segoe UI" FontSize="48" />
When you want to prevent editing of the text within the control but still want to maintain the look of the
enabled control and allow users to copy the contents of the control to their clipboard, setting the IsReadOnly to
True can be used to achieve that effect.Because the TextBox control also supports interaction with the end user, events become important.
These are some of the events you may find yourself responding to for the TextBox:
• GotFocus: This event fires when the TextBox becomes the target of focus on the page.
One common use for GotFocus with TextBox controls is to cause all of the text within
the control to be selected. This allows the user to overwrite the current contents
just by starting to type. This is less effective in the Windows 8 environment because the
touch-optimized TextBox in Windows provides a button to clear the text that the user
can just tap or click.
• TextChanged: This event fires when the Text property of the control is updated. It can be
used for purposes such as validation and triggering the recalculation of computed fields.
enabled control and allow users to copy the contents of the control to their clipboard, setting the IsReadOnly to
True can be used to achieve that effect.Because the TextBox control also supports interaction with the end user, events become important.
These are some of the events you may find yourself responding to for the TextBox:
• GotFocus: This event fires when the TextBox becomes the target of focus on the page.
One common use for GotFocus with TextBox controls is to cause all of the text within
the control to be selected. This allows the user to overwrite the current contents
just by starting to type. This is less effective in the Windows 8 environment because the
touch-optimized TextBox in Windows provides a button to clear the text that the user
can just tap or click.
• TextChanged: This event fires when the Text property of the control is updated. It can be
used for purposes such as validation and triggering the recalculation of computed fields.