TextBox: set text, select all, clear, prepend, insert, append, cut, paste, undo

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="300" Width="300">
    <StackPanel>
        <TextBox AcceptsReturn="True" Height="100" IsReadOnly="True" 
                 Name="textBox1" TextAlignment="Left" TextWrapping="Wrap" 
                 VerticalScrollBarVisibility="Auto">
            Default starting text.
        </TextBox>
        <WrapPanel Margin="10">
            <Button Margin="5" Name="textButton" Width="75" Click="TextButton_Click">Set Text</Button>
            <Button Margin="5" Name="selectAllButton" Width="75" Click="SelectAllButton_Click">Select All</Button>
            <Button Margin="5" Name="clearButton" Width="75" Click="ClearButton_Click">Clear</Button>
            <Button Margin="5" Name="prependButton" Width="75" Click="PrependButton_Click">Prepend</Button>
            <Button Margin="5" Name="insertButton" Width="75" Click="InsertButton_Click">Insert</Button>
            <Button Margin="5" Name="appendButton" Width="75" Click="AppendButton_Click">Append</Button>
            <Button Margin="5" Name="cutButton" Width="75" Click="CutButton_Click">Cut</Button>
            <Button Margin="5" Name="pasteButton" Width="75" Click="PasteButton_Click">Paste</Button>
            <Button Margin="5" Name="undoButton" Width="75" Click="UndoButton_Click">Undo</Button>
        </WrapPanel>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void AppendButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.AppendText("text");
        }
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Clear();
        }
        private void CutButton_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.SelectionLength == 0)
            {
                MessageBox.Show("Select text to cut first.", Title);
            }
            else
            {
                MessageBox.Show("Cut: " + textBox1.SelectedText, Title);
                textBox1.Cut();
            }
        }

        private void InsertButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = textBox1.Text.Insert(textBox1.CaretIndex, "text");
        }

        private void PasteButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Paste();
        }

        private void PrependButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = textBox1.Text.Insert(0, "Prepend");
        }
        private void SelectAllButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.SelectAll();
            textBox1.Focus();
        }

        private void TextButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = "new value";
        }

        private void UndoButton_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Undo();
        }
    }
}

   
    
     


Set TextBox to editable

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="300" Width="300">
    <StackPanel>
        <TextBox AcceptsReturn="True" Height="100" IsReadOnly="True" 
                 Name="textBox1" TextAlignment="Left" TextWrapping="Wrap" 
                 VerticalScrollBarVisibility="Auto">
            Default starting text.
        </TextBox>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <RadioButton Checked="EditableChecked" Grid.Column="0" 
                         HorizontalAlignment="Center" IsChecked="True"
                         Margin="5" Name="editableRadioButton" >
                Editable</RadioButton>
            <RadioButton Checked="EditableChecked" Grid.Column="1" 
                         HorizontalAlignment="Center" Margin="5"
                         Name="readonlyRadioButton">
                Read Only</RadioButton>
        </Grid>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void EditableChecked(object sender, RoutedEventArgs e)
        {
            RadioButton button = e.OriginalSource as RadioButton;

            if (e.OriginalSource == editableRadioButton)
            {
                textBox1.IsReadOnly = false;
            }
            else if (e.OriginalSource == readonlyRadioButton)
            {
                textBox1.IsReadOnly = true;
            }

            textBox1.Focus();
        }
    }
}

   
    
     


TextBox text changed event

image_pdfimage_print


   
  
<Window x:Class="Commands.TwoDocument"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TwoDocument" Height="300" Width="300">

  <Window.Resources>
    <CommandBinding x:Key="binding" Command="ApplicationCommands.Save" Executed="SaveCommand" CanExecute="SaveCommand_CanExecute" />
  </Window.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0">
      <MenuItem Header="File">
        <MenuItem Command="New"></MenuItem>
        <MenuItem Command="Open"></MenuItem>
        <MenuItem Command="Save"></MenuItem>
        <MenuItem Command="SaveAs"></MenuItem>
        <Separator></Separator>
        <MenuItem Command="Close"></MenuItem>
      </MenuItem>
    </Menu>

    <TextBox Margin="5" Grid.Row="2" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
      <TextBox.CommandBindings>
        <StaticResource ResourceKey="binding"></StaticResource>
      </TextBox.CommandBindings>
    </TextBox>

  </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;

namespace Commands
{
    public partial class TwoDocument : System.Windows.Window
    {
        public TwoDocument()
        {
            InitializeComponent();
        }

        private void SaveCommand(object sender, ExecutedRoutedEventArgs e)
        {            
        }
        private void txt_TextChanged(object sender, RoutedEventArgs e)
        {
        }
        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }
    }
}

   
    
     


TextBox PreviewKeyDown

image_pdfimage_print


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF " Height="300" Width="300">
    <DockPanel LastChildFill="True">
        <TextBox DockPanel.Dock="Top"
                 PreviewKeyDown="TextBox_KeyEvent" />
        <TextBox Name="txtLog" HorizontalAlignment="Stretch" 
                 IsReadOnly="True" VerticalScrollBarVisibility="Visible"/>
    </DockPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void TextBox_KeyEvent(object sender, KeyEventArgs e)
        {
            String msg = String.Format("{0} - {1}
",e.RoutedEvent.Name, e.Key);

            txtLog.Text += msg;
            txtLog.ScrollToEnd();
        }

    }
}

   
    
     


Programmatically change the way in which TextBlock is trimmed when it exceeds the outer boundaries of its containing box.

image_pdfimage_print


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.Window1"
  Title="TextTrimming Sample">
    <DockPanel Margin="10">
      <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="0,0,0,25">
        <Button Click="ttNone">TextTrimming="None"</Button>
        <Button Click="ttCE">TextTrimming="CharacterEllipsis"</Button>
        <Button Click="ttWE">TextTrimming="WordEllipsis"</Button>
      </StackPanel>
      <TextBlock Name="txt1" DockPanel.Dock="Bottom"/>
      <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" 
        Width="400" FontFamily="Arial" Name="tf1" DockPanel.Dock="Top"
        Background="SkyBlue">
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt
        laoreet dolore magna aliquam erat volutpat.
      </TextBlock>
    </DockPanel>
</Window>


//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WpfApplication1
{
  public partial class Window1 : Window
  {
        public void ttNone(object sender, RoutedEventArgs e)
        {
            tf1.TextTrimming = System.Windows.TextTrimming.None;
            txt1.Text = "TextTrimming is now " + tf1.TextTrimming.ToString();
        }
        public void ttCE(object sender, RoutedEventArgs e)
        {
            tf1.TextTrimming = System.Windows.TextTrimming.CharacterEllipsis;
            txt1.Text = "TextTrimming is now " + tf1.TextTrimming.ToString();
        }
        public void ttWE(object sender, RoutedEventArgs e)
        {
            tf1.TextTrimming = System.Windows.TextTrimming.WordEllipsis;
            txt1.Text = "TextTrimming is now " + tf1.TextTrimming.ToString();
        }
    }
}

   
    
     


Effects of the enumerated values of TextWrapping.

image_pdfimage_print


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.Window1" Height="480" Width="640"
  Title="TextWrap Sample">
  <StackPanel Margin="0,0,0,10" Background="LightSkyBlue">
    <StackPanel Orientation="Horizontal" Margin="0,0,0,20">
      <Button Name="btn1" Background="Silver" Width="100" Click="Wrap">Wrap</Button>
      <Button Name="btn2" Background="Silver" Width="100" Click="NoWrap">NoWrap</Button>
      <Button Name="btn4" Background="Silver" Width="100" Click="WrapWithOverflow">WrapWithOverflow</Button>
    </StackPanel>
    <TextBlock Name="txt2" TextWrapping="Wrap" Margin="0,0,0,20" Foreground="Black">
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, 
      consectetuer adipiscing elit.Lorem ipsum dolor sit aet, consectetuer adipiscing elit.
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    </TextBlock>
    <TextBlock FontSize="12" Name="txt1">
      The TextWrap property is currently set to Wrap.
    </TextBlock>    
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApplication1
{

  public partial class Window1 : Window
  {
        public void Wrap(object sender, RoutedEventArgs e)
        {
            txt2.TextWrapping = System.Windows.TextWrapping.Wrap;
            txt1.Text = "The TextWrap property is currently set to Wrap.";
        }
        public void NoWrap(object sender, RoutedEventArgs e)
        {
            txt2.TextWrapping = System.Windows.TextWrapping.NoWrap;
            txt1.Text = "The TextWrap property is currently set to NoWrap.";
        }
        public void WrapWithOverflow(object sender, RoutedEventArgs e)
        {
            txt2.TextWrapping = System.Windows.TextWrapping.WrapWithOverflow;
            txt1.Text = "The TextWrap property is currently set to WrapWithOverflow.";
        }
    }
}

   
    
     


Simple underline decoration

image_pdfimage_print


   
  

<Window x:Class="Decorations.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Decorations" Height="300" Width="300">
    <Grid>
      <TextBlock x:Name="text" Text="Decorations" />
    </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;


namespace Decorations
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
            text.TextDecorations = TextDecorations.Underline;
        }
    }
}