|
-
May 20th, 2011, 12:40 AM
#1
Thread Starter
Fanatic Member
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.
-
May 20th, 2011, 01:02 AM
#2
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.
-
May 20th, 2011, 04:12 AM
#3
Thread Starter
Fanatic Member
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
-
May 20th, 2011, 05:43 AM
#4
Re: Replace {TAB} indent with individual spaces
vb.net Code:
Private Sub RichTextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown If e.KeyData = Keys.Tab Then e.SuppressKeyPress = True Me.RichTextBox1.SelectedText = " " End If End Sub
Make sure AcceptsTab is set to True for the control.
-
May 20th, 2011, 02:55 PM
#5
Thread Starter
Fanatic Member
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...
-
May 21st, 2011, 12:01 AM
#6
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.
-
May 23rd, 2011, 02:24 AM
#7
Thread Starter
Fanatic Member
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.
-
May 23rd, 2011, 02:48 AM
#8
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|