Answered by:
tab size in the textbox

Question
-
Hey, is there any way to adjust the width in pixels of a tab character in the textbox?Tuesday, June 10, 2008 7:32 PM
Answers
-
Here you go:
public class MyTextBox : TextBox
{
public MyTextBox()
{
//Defaults to 4
TabSize = 4;
}
public int TabSize
{
get;
set;
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
String tab = new String(' ', TabSize);
int caretPosition = base.CaretIndex;
base.Text = base.Text.Insert(caretPosition, tab);
base.CaretIndex = caretPosition + TabSize + 1;
e.Handled = true;
}
}
}
<cc:MyTextBox AcceptsReturn="True" TabSize="10" x:Name="textBox"/>
Hope this helps
- Marked as answer by Will Sullivan Monday, June 16, 2008 10:59 AM
Monday, June 16, 2008 4:57 AM
All replies
-
You could wire up to TextBox.PreviewKeyDown event to insert the specified number of white characters or tab characters as following code demonstrates:
<UserControl x:Class="AnswerHarness2.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBox AcceptsTab="True" AcceptsReturn="True" x:Name="textBox"/>
</UserControl>
public partial class UserControl1 : UserControl
{
private int TabSize = 4;
public UserControl1()
{
InitializeComponent();
textBox.PreviewKeyDown += (sender, e) =>
{
if (e.Key == Key.Tab)
{
String tab = new String(' ', TabSize);
textBox.Text.Insert(textBox.CaretIndex, tab);
}
};
}
}
Hope this helps
Friday, June 13, 2008 5:31 AM -
Thanks for the hint, but I was hoping this could be controlled via a property (or a property of a property or something) without using codebehind.Friday, June 13, 2008 5:35 PM
-
Here you go:
public class MyTextBox : TextBox
{
public MyTextBox()
{
//Defaults to 4
TabSize = 4;
}
public int TabSize
{
get;
set;
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
String tab = new String(' ', TabSize);
int caretPosition = base.CaretIndex;
base.Text = base.Text.Insert(caretPosition, tab);
base.CaretIndex = caretPosition + TabSize + 1;
e.Handled = true;
}
}
}
<cc:MyTextBox AcceptsReturn="True" TabSize="10" x:Name="textBox"/>
Hope this helps
- Marked as answer by Will Sullivan Monday, June 16, 2008 10:59 AM
Monday, June 16, 2008 4:57 AM