4

The width of Tab (\t) character in silverlight TextBox is not equal to 4 spaces or 8 spaces. It's too short. Is it possible to change the width of the TAB (\t) character displayed in a silverlight TextBox?

Note that I want to avoid replacing TABs with spaces. Any ideas on how to do this?

3 Answers 3

2

Silverlight does not allow you to change the tab character length in a TextBox.

If your reading in a string (from a file or something) and setting the Text to it then if you look at your Text Property you'll see the escaped tab (\t). Searching for a \t is easy

TabTextBox.Text = TabTextBox.Text.Replace("\t", "    ");

So this will replace all tabs with 4 spaces.

Besides pressing tab in a TextBox will not tab the text. It will focus to the next UIElement within the parent UIElement.

Sign up to request clarification or add additional context in comments.

5 Comments

If you are doing what Sinaesthetic suggests make sure you cancel the tab event to focus back on your textbox. Not sure what you want to achieve.
The tab character is from the TextBox data source. I bind the Text property of the Textbox to a string, and the string contain Tab. If i replace the '\t' with four spaces, it'll work not very well. As when i cut or copy the text from the TextBox, it'll not get the real '\t' but some spaces instead.
And when I copy some string from other editor to the Textbox, if the string contains spaces, the result seems strange.
Unfortunately Silverlight does not support TabStops. The only thing you can do is make sure the Text is parsed to a finite number of white-spaces instead of \t. Obviously when you copy the Text to an application that supports TabStops (Like Microsoft Office) it will not look like a tab, instead it will look like n space. I even looked at Silverlight 5 so it looks like its not possible.
Thanks for reply. Yes, SL5 has the same problem. I check it in WPF and it seems WPF has the same problem.
1

Or maybe on tab key press event, append the string literal to the text box. Similar to what MyK is suggesting.

1 Comment

I want to avoid replacing TABs with spaces. As if a replace it, if the datasouce with Tab, after saved, the datasource will be changed.
1

If you're trying to get this to display, write a converter. the syntax is around--just grab an example, gut it, rename to something like 'tabstoptexttospacedtextconverter', add a reference to your local controls in app.xaml, then create an instance of it and give it an x:name to use it. Bind the data for your text box and assign it your new converter.

It'll be a little bit of hassle because you'll have to determine the appropriate width of the final display TB, and then probably use a converterparameter to make that work. But long story short, split on \t, then foreach(string str in splitSourceText) do something like this:

for (int i = 0; i < (str.Length % 8 > 0 ? str.Length % 8 : 8); i++) str+= " ";

You can limit the characters on the split string arrays with the parameter or split on \r\n first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.