Results 1 to 8 of 8

Thread: Replace {TAB} indent with individual spaces

  1. #1

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Replace {TAB} indent with individual spaces

    I have a notepad I'm creating, but i've tried everything I could think of to replace a tab indent with spaces on keys.tab event.

    When I press the Tab key I want it to replace the tab indent with 4 individual spaces. Like as if I was to press the space bar 4 times, only that would be done automatically upon pressing the tab key on my keyboard.

    I'm using a richtextbox control. I have a tab settings form, where I will be able to give users an option to choose how many spaces the tab key will represent. I just need to figure out how to replace the {TAB}.

    so far, I have the number of spaces correct, but the last space in the series after pressing my tab key seems a bit larger than the rest of the spaces, and is not a real (normal) space character. That puts everything out of line when i'm typing on a new line. the characters don't line up vertically and when I press the space key however many times i've set the {Tab} key to represent, the tab key even though it should have the same number of spaces occupies a larger distance horizontally if that makes sense.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Replace {TAB} indent with individual spaces

    The first thing that comes to mind is handling the KeyDown event, cancelling it and then setting the SelectedText.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Replace {TAB} indent with individual spaces

    I've already tried:

    Code:
    Private Sub RichTextBox1_TabKey(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
            If e.KeyCode = Keys.Tab Then
    
                    RichTextBox1.SelectedText = "  "
                    e.Handled = True
           End If
    end sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Replace {TAB} indent with individual spaces

    vb.net Code:
    1. Private Sub RichTextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
    2.     If e.KeyData = Keys.Tab Then
    3.         e.SuppressKeyPress = True
    4.         Me.RichTextBox1.SelectedText = "    "
    5.     End If
    6. End Sub
    Make sure AcceptsTab is set to True for the control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Replace {TAB} indent with individual spaces

    Amazing, it worked Thankyou. I spent hours trying to figure this out with different methods like trimming that weird "space" off the end, and trying to limit the number of characters, etc..

    If I would have known how simple this was before...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Replace {TAB} indent with individual spaces

    One thing I probably should have pointed out earlier: note that I'm using KeyData rather than KeyCode. That ensures that the code only responds to Tab alone and not Ctrl+Tab or any other combination involving a modifier key.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Replace {TAB} indent with individual spaces

    alright, thanks for the help though I knew KeyData was there, I didn't change it though. I typed it all in myself trying to understand your code so that I could modify it to fit my needs. You've been a great help for me though, and I appreciate that a lot.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Replace {TAB} indent with individual spaces

    If you want to know more about keyboard events, the difference between KeyCode and KeyData, etc, follow the Blog link in my signature and check out my post on the subject.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width