Is it possible to add padding into a Rich Text Box control between the text and the border?
I tried docking a rich text box inside of a panel, with its padding for all f
Here is a simple solution for setting the left and right margins of a RichTextBox that worked well for me in a C# Windows app. It sets both the right and left margins to 4 pixels.
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, UIntPtr wParam, IntPtr lParam);
// both left and right margins set to 4 pixels with the last parm: 0x00040004
SendMessage(richtextbox1.Handle, 0xd3, (UIntPtr)0x3, (IntPtr)0x00040004);
I tried to make the SendMessage more proper below:
const int EM_SETMARGINS = 0xd3;
const int EC_LEFTMARGIN = 0x1;
const int EC_RIGHTMARGIN = 0x2;
SendMessage(richtextbox1.Handle, EM_SETMARGINS,
(UIntPtr)(EC_LEFTMARGIN | EC_RIGHTMARGIN), (IntPtr)0x00040004);
I figure this solution was not possible when the question was originally posted.
In the controltemplate the contentpart is this:
<ScrollViewer x:Name="PART_ContentHost"
Style="{StaticResource ScrollViewerTextBox}"
VerticalAlignment="Top"/>`
Create a style (and controltemplate) for the scrollviewer; Set the MinWidth on the Grid column similar to the width of your scrollbar and adjust the margin to your preference on the ScrollContentPresenter.
<Style x:Key="ScrollViewerTextBox" TargetType="{x:Type ScrollViewer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto" MinWidth="18"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollContentPresenter Grid.Column="0" Margin="3 3 3 0" CanVerticallyScroll="True" CanContentScroll="True"/>
<ScrollBar x:Name="PART_VerticalScrollBar"
Grid.Row="0"
Grid.Column="1"
Value="{TemplateBinding VerticalOffset}"
Maximum="{TemplateBinding ScrollableHeight}"
ViewportSize="{TemplateBinding ViewportHeight}"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}">
<ScrollBar.ContextMenu>
<ContextMenu Visibility="Hidden" />
</ScrollBar.ContextMenu>
</ScrollBar>
<ScrollBar x:Name="PART_HorizontalScrollBar"
Orientation="Horizontal"
Grid.Row="1"
Grid.Column="0"
Value="{TemplateBinding HorizontalOffset}"
Maximum="{TemplateBinding ScrollableWidth}"
ViewportSize="{TemplateBinding ViewportWidth}"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}">
<ScrollBar.ContextMenu>
<ContextMenu Visibility="Hidden" />
</ScrollBar.ContextMenu>
</ScrollBar>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Maybe this is what you need?
richTextBox.SelectionIndent += 20;
A quick and easy way is to offset text from vertical scroll, by calling this example method at form load and at form/control resize events:
private void AdjustTextBoxRMargin()
{
richTextBox1.RightMargin = richTextBox1.Size.Width - 35;
}
The value of 35 seems to work for Win7, but may differ on other versions of Windows.
The RichTextBox has no padding property.
Quick and dirty padding can be achieved by putting the RichTextBox in a Panel, which has the same BackColor
property as the RichTextBox (usually Color.White
).
Then, set the Dock
property of the RichTextBox to Fill
, and play with the Padding
properties of the Panel control.