Enable WPF Textbox Multiline Text

The WPF TextBox has the ability to accept multiline input. The blog post "Enable WPF TextBox Multiline Text" provides code snippets to enable a WPF TextBox for multiline text and to read text from the TextBox.

WPF Textbox

Enable WPF Textbox Multiline Text

Getting Started

There are two textbox attributes which makes the WPF textbox to to accommodate multiple lines of text. Those are TextWrapping and AcceptsReturn. The TextWrapping attribute allows warp to intered text to a new line when the edge of the textbox control is reached by expanding the height of the textbox control to include room for a new line, if necessary. The AcceptReturn attribute inserts a new line at the current cursor position

To enable a WPF TextBox for multiple lines, you need to set the TextWrapping property to Wrap and the AcceptReturn property to True. To allow the content of the TextBox to scroll, set the VerticalScrollBarVisibility property to Visible.

Enable WPF Textbox Multiline Text

The following code example shows how to use Extensible Application Markup Language (XAML) to define a TextBox control that automatically expands to accommodate multiple lines of text.

XAML Code
 <TextBox 
	Name="txtmulitiline" Grid.Row="1"
	TextWrapping="Wrap"
	AcceptsReturn="True"
	BorderBrush="Black"
	BorderThickness="1"
	VerticalScrollBarVisibility="Visible"
	HorizontalScrollBarVisibility="Disabled"
	FontWeight="Bold">
 </TextBox>  

C# Code
 this.txtmulitiline.TextWrapping = TextWrapping.Wrap;  
 this.txtmulitiline.AcceptsReturn = true;  
 this.txtmulitiline.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;  
 this.txtmulitiline.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;  

Adding Text to WPF Multiline TextBox

To add text to a multiline TextBox in WPF, you can do it programmatically from your C# code-behind by using its Text property.

Adding Text
  txtmulitiline.Text = "This is a new message in multiline textbox.";  

This replaces any existing content

Append Text
txtmulitiline.Text += "New line of text added." + Environment.NewLine;  

Above code will keep existing content and add more,use he += operator and add a newline:

Retrive Line Text from WPF Multiline TextBox

To retrieve text from a multiline TextBox in WPF, you simply access its Text property — just like you would with a single-line TextBox.

Get Text
string userInput =txtmulitiline.Text ;  

This will retrive full text

Get Line Text
 // lineCount may be -1 if TextBox layout info is not up-to-date.  
 int lineCount = txtmulitiline.LineCount;  
 for (int line = 0; line < lineCount; line++)  
 {  
      // GetLineText takes a zero-based line index.  
      MessageBox.Show(txtmulitiline.GetLineText(line));  
 }  

Related Articles

  1. WPF Textbox Numeric Only OR WPF Numeric Textbox
  2. Round Corner TextBox in WPF
  3. Round Corner TextBox With Border Effect
  4. Data Validation in WPF

Summary

To enable multiline functionality in a WPF TextBox, you mainly need to set AcceptsReturn="True" and TextWrapping="Wrap". Combine this with scroll bar settings and adequate size to create a user-friendly multiline input field. With these simple tweaks, your WPF application can effectively handle complex user input.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال