WPF TextBox and preserving tabs












1















Is there some elegant way how to allow pasting into WPF TextBox that has AcceptsTab set to false while preserving tabs in the pasted text ?



When AcceptsTab is true, then user can't use tabs to move to next control, which isn't desired by my users. But they want to have tabs that are pasted, which currently are replaced by spaces.



Thank you










share|improve this question





























    1















    Is there some elegant way how to allow pasting into WPF TextBox that has AcceptsTab set to false while preserving tabs in the pasted text ?



    When AcceptsTab is true, then user can't use tabs to move to next control, which isn't desired by my users. But they want to have tabs that are pasted, which currently are replaced by spaces.



    Thank you










    share|improve this question



























      1












      1








      1


      1






      Is there some elegant way how to allow pasting into WPF TextBox that has AcceptsTab set to false while preserving tabs in the pasted text ?



      When AcceptsTab is true, then user can't use tabs to move to next control, which isn't desired by my users. But they want to have tabs that are pasted, which currently are replaced by spaces.



      Thank you










      share|improve this question
















      Is there some elegant way how to allow pasting into WPF TextBox that has AcceptsTab set to false while preserving tabs in the pasted text ?



      When AcceptsTab is true, then user can't use tabs to move to next control, which isn't desired by my users. But they want to have tabs that are pasted, which currently are replaced by spaces.



      Thank you







      wpf xaml






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 20:33









      Erno de Weerd

      44.8k96795




      44.8k96795










      asked Nov 23 '18 at 20:16









      B JB J

      1871212




      1871212
























          2 Answers
          2






          active

          oldest

          votes


















          2














          I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).



          See Paste Event in a WPF TextBox



          Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:



          XAML:



          <TextBox AcceptsTab="False"
          Height="200"
          PreviewKeyDown="TextBox_PreviewKeyDown"
          KeyUp="TextBox_KeyUp"/>


          C#:



          private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
          {
          if (!(sender is TextBox textbox))
          {
          return;
          }

          if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
          {
          textbox.AcceptsTab = true;
          }
          }

          private void TextBox_KeyUp(object sender, KeyEventArgs e)
          {
          if (!(sender is TextBox textbox))
          {
          return;
          }

          textbox.AcceptsTab = false;
          }


          This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.






          share|improve this answer

































            1














            Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.



            The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.



            private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
            {
            if (e.Key == Key.Tab)
            {
            var textBox = sender as TextBox;
            if (textBox != null)
            {
            var direction =
            e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
            ? FocusNavigationDirection.Previous
            : FocusNavigationDirection.Next;
            textBox.MoveFocus(new TraversalRequest(direction));
            }
            }
            }





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452506%2fwpf-textbox-and-preserving-tabs%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).



              See Paste Event in a WPF TextBox



              Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:



              XAML:



              <TextBox AcceptsTab="False"
              Height="200"
              PreviewKeyDown="TextBox_PreviewKeyDown"
              KeyUp="TextBox_KeyUp"/>


              C#:



              private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
              {
              if (!(sender is TextBox textbox))
              {
              return;
              }

              if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
              {
              textbox.AcceptsTab = true;
              }
              }

              private void TextBox_KeyUp(object sender, KeyEventArgs e)
              {
              if (!(sender is TextBox textbox))
              {
              return;
              }

              textbox.AcceptsTab = false;
              }


              This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.






              share|improve this answer






























                2














                I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).



                See Paste Event in a WPF TextBox



                Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:



                XAML:



                <TextBox AcceptsTab="False"
                Height="200"
                PreviewKeyDown="TextBox_PreviewKeyDown"
                KeyUp="TextBox_KeyUp"/>


                C#:



                private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
                {
                if (!(sender is TextBox textbox))
                {
                return;
                }

                if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
                {
                textbox.AcceptsTab = true;
                }
                }

                private void TextBox_KeyUp(object sender, KeyEventArgs e)
                {
                if (!(sender is TextBox textbox))
                {
                return;
                }

                textbox.AcceptsTab = false;
                }


                This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.






                share|improve this answer




























                  2












                  2








                  2







                  I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).



                  See Paste Event in a WPF TextBox



                  Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:



                  XAML:



                  <TextBox AcceptsTab="False"
                  Height="200"
                  PreviewKeyDown="TextBox_PreviewKeyDown"
                  KeyUp="TextBox_KeyUp"/>


                  C#:



                  private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
                  {
                  if (!(sender is TextBox textbox))
                  {
                  return;
                  }

                  if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
                  {
                  textbox.AcceptsTab = true;
                  }
                  }

                  private void TextBox_KeyUp(object sender, KeyEventArgs e)
                  {
                  if (!(sender is TextBox textbox))
                  {
                  return;
                  }

                  textbox.AcceptsTab = false;
                  }


                  This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.






                  share|improve this answer















                  I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).



                  See Paste Event in a WPF TextBox



                  Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:



                  XAML:



                  <TextBox AcceptsTab="False"
                  Height="200"
                  PreviewKeyDown="TextBox_PreviewKeyDown"
                  KeyUp="TextBox_KeyUp"/>


                  C#:



                  private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
                  {
                  if (!(sender is TextBox textbox))
                  {
                  return;
                  }

                  if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
                  {
                  textbox.AcceptsTab = true;
                  }
                  }

                  private void TextBox_KeyUp(object sender, KeyEventArgs e)
                  {
                  if (!(sender is TextBox textbox))
                  {
                  return;
                  }

                  textbox.AcceptsTab = false;
                  }


                  This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 20:50

























                  answered Nov 23 '18 at 20:33









                  Erno de WeerdErno de Weerd

                  44.8k96795




                  44.8k96795

























                      1














                      Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.



                      The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.



                      private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
                      {
                      if (e.Key == Key.Tab)
                      {
                      var textBox = sender as TextBox;
                      if (textBox != null)
                      {
                      var direction =
                      e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
                      ? FocusNavigationDirection.Previous
                      : FocusNavigationDirection.Next;
                      textBox.MoveFocus(new TraversalRequest(direction));
                      }
                      }
                      }





                      share|improve this answer




























                        1














                        Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.



                        The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.



                        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
                        {
                        if (e.Key == Key.Tab)
                        {
                        var textBox = sender as TextBox;
                        if (textBox != null)
                        {
                        var direction =
                        e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
                        ? FocusNavigationDirection.Previous
                        : FocusNavigationDirection.Next;
                        textBox.MoveFocus(new TraversalRequest(direction));
                        }
                        }
                        }





                        share|improve this answer


























                          1












                          1








                          1







                          Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.



                          The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.



                          private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
                          {
                          if (e.Key == Key.Tab)
                          {
                          var textBox = sender as TextBox;
                          if (textBox != null)
                          {
                          var direction =
                          e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
                          ? FocusNavigationDirection.Previous
                          : FocusNavigationDirection.Next;
                          textBox.MoveFocus(new TraversalRequest(direction));
                          }
                          }
                          }





                          share|improve this answer













                          Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.



                          The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.



                          private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
                          {
                          if (e.Key == Key.Tab)
                          {
                          var textBox = sender as TextBox;
                          if (textBox != null)
                          {
                          var direction =
                          e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
                          ? FocusNavigationDirection.Previous
                          : FocusNavigationDirection.Next;
                          textBox.MoveFocus(new TraversalRequest(direction));
                          }
                          }
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 29 '18 at 8:22









                          Erno de WeerdErno de Weerd

                          44.8k96795




                          44.8k96795






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452506%2fwpf-textbox-and-preserving-tabs%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Wiesbaden

                              Marschland

                              Dieringhausen