The RichTextBox control enables you to display or edit flow content including paragraphs, images, tables, and more. Both RichTextBox and TextBox allow users to edit text, however, the two controls are used in different scenarios. A RichTextBox is a better choice when it is necessary for the user to edit formatted text, images, tables, or other rich content. For example, editing a document, article, or blog that requires formatting, images, etc is best accomplished using a RichTextBox.
A TextBox requires less system resources then a RichTextBox and it is ideal when only plain text needs to be edited i. The code below shows how to create a RichTextBox that a user can edit rich content in. Specifically, the content edited in a RichTextBox is flow content.
Flow content can contain many types of elements including formatted text, images, lists, and tables. World ka sabse bada bandh konsa hai Flow Document Overview for in depth information on flow documents. In order to contain flow content, a RichTextBox hosts a FlowDocument object which in turn contains the editable content.
To demonstrate flow content in a RichTextBoxthe following code shows how to create a RichTextBox with a paragraph and some bolded text. As a user edits RichTextBox content, they change this flow content. To learn more about the features of flow content and how to work with it, see Flow Document Overview. Flow content inside a RichTextBox does not behave exactly like flow content contained in other controls.
For example, there are no columns in a RichTextBox and hence no automatic resizing behavior. Also, built in features like search, viewing mode, page navigation, and zoom are not available within a RichTextBox.
When spellchecking is turned on, a red line appears underneath any misspelled words see picture below. By default, both TextBox and RichTextBox 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 see illustration below. You can create your own custom context menu to override the default one. Editing commands enable users to format editable content inside a RichTextBox. Besides basic editing commands, RichTextBox includes formatting commands that TextBox does not support.
See EditingCommands for a complete list of commands available. In addition to using keyboard shortcuts, you can hook commands up to other controls like buttons. The following example shows how to create a simple tool bar containing buttons that the user can use to change text formatting.
The following example shows how to save content of a RichTextBox to a file, load that content back into the RichTextBoxand print the contents. Below is the markup for the example.Content within a user interface is often larger than a computer screen's display area. This topic introduces the ScrollViewer element and provides several usage examples. The ScrollViewer control encapsulates horizontal and vertical ScrollBar elements and a content container such as a Panel element in order to display other visible elements in a scrollable area.
You must build a custom object in order to use the ScrollBar element for content scrolling. However, you can use the ScrollViewer element by itself because it is a composite control that encapsulates ScrollBar functionality. The ScrollViewer control responds to both mouse and keyboard commands, and defines numerous methods with which to scroll content by predetermined increments. You can use the ScrollChanged event to detect a change in a ScrollViewer state. A ScrollViewer can only have one child, typically a Panel element that can host a Children collection of elements.
The Content property defines the sole child of the ScrollViewer. Physical scrolling is used to scroll content by a predetermined physical increment, typically by a value that is declared in pixels. Logical scrolling is used to scroll to the next item in the logical tree. Physical scrolling is the default scroll behavior for most Panel elements. WPF supports both types of scrolling. The IScrollInfo interface represents the main scrolling region within a ScrollViewer or derived control.
The interface defines scrolling properties and methods that can be implemented by Panel elements that require scrolling by logical unit, rather than by a physical increment. Casting an instance of IScrollInfo to a derived Panel and then using its scrolling methods provides a useful way to scroll to the next logical unit in a child collection, rather than by pixel increment.
By default, the ScrollViewer control supports scrolling by physical units. For layout controls that natively support logical scrolling, you can still achieve physical scrolling by wrapping the host Panel element in a ScrollViewer and setting the CanContentScroll property to false. The following code example demonstrates how to cast an instance of IScrollInfo to a StackPanel and use content scrolling methods LineUp and LineDown defined by the interface.
The following example creates a ScrollViewer in a window that contains some text and a rectangle. ScrollBar elements appear only when they are necessary.
Like all controls in Windows Presentation Foundation, the ScrollViewer can be styled in order to change the default rendering behavior of the control.
For additional information on control styling, see Styling and Templating. For document content, an alternative to scrolling is to choose a document container that supports pagination.
FlowDocument is for documents that are designed to be hosted within a viewing control, such as FlowDocumentPageViewerthat supports paginating content across multiple pages, preventing the need for scrolling. DocumentViewer provides a solution for viewing FixedDocument content, which uses traditional scrolling to display content outside the realm of the display area. For additional information about document formats and presentation options, see Documents in WPF. Skip to main content.
Contents Exit focus mode. Physical vs. Logical Scrolling Physical scrolling is used to scroll content by a predetermined physical increment, typically by a value that is declared in pixels. Resize the Window, making it larger and smaller.Vertical scrollbar to a Window in C. This article and code samples demo how to use a scroll bar in a Window app. The Width and Height properties represent the width and the height of a ScrollBar. The Name property represents the name of the control, which is a unique identifier of a control.
The Margin property tells the location of a ScrollBar on the parent control. The Orientation property sets the direction of scrolling that can be either horizontal or vertical. The following code snippet sets the name, height, width, orientation, margin, and background of a ScrollBar control. The Minimum and Maximum properties represent minimum and maximum range of a ScrollBar.
This class is defined in using System. Primitives namespace. Before you use this class, make sure to import this namespace. The following code snippet creates a ScrollBar at run-time and sets its orientation, width, height, background, minimum, maximum and value properties.
View All. Mahesh Chand Updated date Nov 27, Free Demo. Next Recommended Article. NET Core. Factory Design Pattern With Java. Using Records In C 9. Private Constructor - C. Abstract Class Vs Interface - C.Represents a rich editing control which operates on FlowDocument objects.
FlowDocument is the only supported child element for a RichTextBox. If you need to respond to these events, listen for the tunneling PreviewMouseUp and PreviewMouseDown events instead, or register the handlers with the HandledEventsToo argument this latter option is only available through code. Do not mark the event handled unless you deliberately want to disable RichTextBox native handling of these events, and be aware that this has notable effects on the control's UI. While RichTextBox supports copying and pasting of images, certain scenarios are currently unsupported and may not work properly.
The following table summarizes these scenarios and expected results. Text always wraps in a RichTextBox. However, once the page width is reached the text still wraps. Scrollbars are not visible on a RichTextBox by default. RichTextBox supports a variety of keyboard commands. For a list of keyboard commands, see EditingCommands. Dependency properties for this control might be set by the control's default style.
If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. Initializes a new, default instance of the RichTextBox class.
Initializes a new instance of the RichTextBox class, adding a specified FlowDocument as the initial content. Identifies the IsDocumentEnabled dependency property. Gets or sets a value that indicates how the text editing control responds when the user presses the ENTER key.
Gets or sets a value that indicates how the text editing control responds when the user presses the TAB key. Gets or sets a value indicating whether this element can be used as the target of a drag-and-drop operation. This is a dependency property. Gets a value that indicates whether at least one touch is captured to this element or to any child elements in its visual tree.
Its vertical scrollbar is working fine. But its horizontal scrollbar is not working. Your text box is wrapping the inner contents which is a default for the RichTextBox Control.
If you wish to explicitly enable it being scroll-able, you can do something similar to this StackOverflow Issue. Learn more. Asked 5 years, 1 month ago. Active 5 months ago. Viewed 6k times. Please help me. Active Oldest Votes. Matt Wilkinson Matt Wilkinson 2 2 silver badges 13 13 bronze badges.
All long lines that exceed the width of RichTextBox have been auto-line breaked, and no horizontal scroll bar appears. M Komaei M Komaei 1, 1 1 gold badge 6 6 silver badges 13 13 bronze badges. Sumit Sumit 1. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Ben answers his first question on Stack Overflow. The Overflow Bugs vs. Featured on Meta. Responding to the Lavender Letter and commitments moving forward.
Subscribe to RSS
You didn't do what SilverlightSantha said - Set the Xaml property. I did this in a button click handler and the problem is reproducible. However, if you are looking for a way to make the woraround of sv. ScrollToVerticalOffset 0 ; work, you can do.
I found one work around, I created another RichText box at top with Visibility set to false and set the Xaml propert at lastso that it reverse the scrolling. This is my xaml, put the richtexbox at the top with Width,Height and BorderThickness set to zero. Perhaps in the loaded event, you can set the properties. Code Behind. I've ran into this issue as well. And the myRTB.
Select is there to take care of another bug: The annoying scroll to the bottom of the RichTextArea's scrollbar. Please note that this patch only works if it's in between my Visibility calls. However, in our real application we manage the size of the RTB after an update, and one of the several steps in calculating the size triggers the scrollviewer to make the RTB wrapped in a larger UserControl visible.
Is there a way to suppress this behavior on the ScrollViewer, rather than by changing visibility on the RTB? This doesn't work when the browser is resized. The content you requested has been removed.
How to add Scrolling in WPF TextBox
Ask a question. Quick access. Search related threads.By default, the Windows Forms RichTextBox control displays horizontal and vertical scroll bars as necessary. There are seven possible values for the ScrollBars property of the RichTextBox control, which are described in the table below. Set the Multiline property to true. No type of scroll bar, including horizontal, will display if the Multiline property is set to false.
Set the WordWrap property to an appropriate value. Skip to main content. Contents Exit focus mode. Value Description Both default Displays horizontal or vertical scroll bars, or both, only when text exceeds the width or length of the control. None Never displays any type of scroll bar. Horizontal Displays a horizontal scroll bar only when the text exceeds the width of the control. For this to occur, the WordWrap property must be set to false.
Vertical Displays a vertical scroll bar only when the text exceeds the height of the control. ForcedHorizontal Displays a horizontal scroll bar when the WordWrap property is set to false. The scroll bar appears dimmed when text does not exceed the width of the control.WPF Tutorial 16 - The ScrollViewer
ForcedVertical Always displays a vertical scroll bar. The scroll bar appears dimmed when text does not exceed the length of the control.
ForcedBoth Always displays a vertical scrollbar. Displays a horizontal scroll bar when the WordWrap property is set to false. The scroll bars appear grayed when text does not exceed the width or length of the control. Value Description false Text in the control is not automatically adjusted to fit the width of the control, so it will scroll to the right until a line break is reached. Use this value if you chose horizontal scroll bars or both, above.
The horizontal scrollbar will not appear. Use this value if you chose vertical scroll bars or none, above, to display one or more paragraphs. Yes No. Any additional feedback?