2
votes

I have seen few tutorials that claim to solve this issue online but they do not work. I would like to insert a TAB space when the TAB key is pressed, into my multiline TextBox.

A dudes response from Microsoft was that, by design, Metro apps will bring focus to the next control if you press TAB inside a TextBox. Now, this would make sense, if you were pressing TAB on a Single-line TextBox. But in a multiline TextBox? Don't you think it's more likely that the user will want to insert a TAB?

And yes, I know, you can insert a TAB space in a Metro TextBox by pressing Ctrl+TAB. But that is error prone, since most of us are used to just pressing TAB, and old habbits die hard sometimes.

Here is my issue. I have a text editor feature of my app where the user may need to enter large amounts of data. And you know what people are like, they like to separate things to make their text documents more readable and it's very uncomfortable and more tedious to use Ctrl+TAB. So I would like to know if anybody can help with a workaround for this (it can't involve a RichTextBox, though)?

Also, if I manage to find a workaround, will this increase the chances of my app release being rejected by the Store?

3
The reason you got the response you did is because it's standard behavior in plain text input fields for TAB to move you to the next field. Why is it that you cannot use a RichEditBox? When you want formatting as part of the input, that's the control to use. - devhammer
Hello @devhammer, In WindowsForms and WPF Desktop apps you can press TAB and expect a TAB space in a plain text box. And that's what people are going to expect. And some people won't even think to try the Ctrl+TAB combo. That being said, the response I received was expected, and I don't have a problem with this new design choice by Microsoft, it's the fact that there are many people who are complaining to me about this and if they're complaining, then chances are that people who download my app from the store may complain about it, and there doesn't appear to be anything I can do for them. - Tommy
Can't you just set the AcceptsTab property to true? - antonijn
@AntonieBlom AcceptsTab does not exist for TextBox controls in Metro style applications. - Tommy
@jAsOn Heh...no apologies necessary. I may not have been adequately clear. Looks like handling the TAB key in code is the way to get this done, so hopefully one of the solutions below will work for you. - devhammer

3 Answers

2
votes

Subscribe to the KeyPress event of your TextBox, capture the Tab key by inspecting the KeyCode of the pressed key, and then set the Handled property of the KeyEventArgs to true so the key isn't passed onto any other controls.

Use SendKeys to send a "Tab" character to the TextBox to mimic the behavior of pressing "Ctrl+Tab", like you said:

TextBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
      if (e.KeyCode == Keys.Tab)
      {
          e.Handled = true;
          SendKeys(^{TAB});
      }
}

The carrot (^) represents the CTRL key.

1
votes
richTextBox1.AcceptsTab = true;
0
votes

in your KeyPress event of your textbox control. Make sure that you have the property set true for multiline on the textbox control This would work if you are using a RichText Control which is what I would suggest

if (e.Key == Windows.System.VirtualKey.Tab) 
{
    e.Handled = true;
    string SelectionText = "";
    TextBox.Document.Selection.GetText(Windows.UI.Text.TextGetOptions.None, SelectionText);
    TextBox.Document.Selection.TypeText(char(9) + SelectionText);
}