|
-
Jul 1st, 2010, 08:31 AM
#1
Thread Starter
Member
Problems with textbox.selstart
Guys is there a better method of getting the cursor position?
This may be a real easy question but its eluding me........
I am writing a program that requires some Central European characters in the textbox. Setting the font to allow these is easy enough, even to mapping the keys on the keyboard with a keypress event triggering a change of ascii char with something like this in the keypress trigger.
If KeyAscii = 39 Then
TextPolish.Text = stringA + Chr$(185) + stringB: KeyAscii = 0
End If
where stringA is the left part of the string before the cursor and stringB is the right part after the cursor (if exists)
BUT> i am having an issue with textbox.selstart , if the user wants to alter the text and presses the back arrow then the text.selstart function returns its position +1 position. If the user deletes the whole word and starts again then its ok.
when the user starts typing again,
how can i get the cursor position as it is within the textbox?
another question
how do i set the cursor after the last character?
textbox.selstart= len(textbox.text)
The above is putting the cursor before the last character
Many thanks
-
Jul 1st, 2010, 09:24 AM
#2
Re: Problems with textbox.selstart
Will this help you ? 
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 65 Then '~~~ ASCII for "A"
Text1.SelText = "aaa" '~~~ Substitution for "A"
KeyAscii = 0
ElseIf KeyAscii = 97 Then '~~~ ASCII for "a"
Text1.SelText = "XvE" '~~~ Substitution for "a"
KeyAscii = 0
End If
End Sub
......
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Jul 1st, 2010, 09:49 AM
#3
Re: Problems with textbox.selstart
I think selStart is 0-based... so selStart = 0 will put it at the begining of all the text... selStart = Len(Textbox1.Test) + 1 should put it at the end.
SelStart = 5 will put the cursor after the 5th character, before the 6th...
-tg
-
Jul 1st, 2010, 03:04 PM
#4
Thread Starter
Member
Re: Problems with textbox.selstart
Thanks guys for all your help - i knew it were something simple, i just couldnt figure it out on my own.
This is the code I now have
A textbox called TextPolish, set the font to Arial + Central European
Code:
Private Sub TextPolish_KeyPress(KeyAscii As Integer)
Dim a As Integer
a = TextPolish.SelStart
If a = Len(TextPolish.Text) Then ' check if cursor is at then end of textbox,
a = a + 1 ' if so add 1 to pointer to reset selstart to end
End If
If KeyAscii = 35 Then
TextPolish.SelText = Chr$(159): KeyAscii = 0 ' wrong character code - will fix later
ElseIf KeyAscii = 39 Then
TextPolish.SelText = Chr$(185): KeyAscii = 0
ElseIf KeyAscii = 59 Then
TextPolish.SelText = Chr$(179): KeyAscii = 0
ElseIf KeyAscii = 64 Then
TextPolish.SelText = Chr$(234): KeyAscii = 0
ElseIf KeyAscii = 91 Then
TextPolish.SelText = Chr$(191): KeyAscii = 0
ElseIf KeyAscii = 93 Then
TextPolish.SelText = Chr$(156): KeyAscii = 0
ElseIf KeyAscii = 113 Then
TextPolish.SelText = Chr$(113): KeyAscii = 0
ElseIf KeyAscii = 123 Then
TextPolish.SelText = Chr$(241): KeyAscii = 0
ElseIf KeyAscii = 125 Then
TextPolish.SelText = Chr$(230): KeyAscii = 0
ElseIf KeyAscii = 126 Then
TextPolish.SelText = Chr$(159): KeyAscii = 0 ' wrong character code - will fix later
End If
TextPolish.SelStart = a
End Sub
keyboard function inside my textbox is now more like this (its not done yet btw):

so my semi-colon key etc are remapped.
Thanks Sooooo much!!!
Last edited by mickle026; Jul 1st, 2010 at 03:13 PM.
-
Jul 1st, 2010, 03:30 PM
#5
Re: Problems with textbox.selstart
I wouldn't do this:
Code:
Dim a As Integer
a = TextPolish.SelStart
If a = Len(TextPolish.Text) Then ' check if cursor is at then end of textbox,
a = a + 1 ' if so add 1 to pointer to reset selstart to end
End If
Comment all that out, and then change this:
Code:
TextPolish.SelStart = a
to this:
Code:
TextPolish.SelStart = TextPolish.SelStart + 1
The reason I suggest that is because, what happens if the user selects let's say 10 characters, then hits a key... that's going to shorten your string, and cause the cursor to jump to the end (since a could potentially exceed your limit of the string) ... and then it is no longer where it should be. Instead, I think all you want to do is simply advance the cursor one space from where it currently is.
-tg
-
Jul 1st, 2010, 04:06 PM
#6
Thread Starter
Member
Re: Problems with textbox.selstart
I totally see your point so I tried as suggested, but this alteration is behaving strangely.
Type a load of characters, then try back arrow and insert some letters, you see the cursor jumping a space past where it needs to be. So typing two characters doesn't put them where you intend them.
My code above would only trigger if the cursor was at the end of the text box as it gets the selstart property into an integer and then checks it against the length of the string. Advances the 'a' pointer by 1 only if it is at the end of the text box, thus avoiding the jumping a space behavior when not desired.
I will look at this tomorrow - time for some sleep, thanks for the help though.
Appreciated.
-
Jul 1st, 2010, 04:29 PM
#7
Re: Problems with textbox.selstart
let me get this straight...
Let's say I type "123456" into the text box... then use the left arrow key (or "back arrow" as you put it) and the cursor is now between the 3 & 4.... at this point SelStart = 3. If I then type "9" it should insert the "9" between the 3 & the 4.... giving me 1239456 .... AND the cursor should be advancing so that SelStart = 4 ....with your original code, as soon as I hit the "9".... it uses .SelStart to set a... so now a = 3 .... since it isn't at the end, a does not increment... and at the end, selstart gets reset back to 3.... keeping the cursor where it is.... which... I don't think it's right... what happens when you take out ALL of the code that deals with SelStart?
-tg
-
Jul 2nd, 2010, 12:18 AM
#8
Thread Starter
Member
Re: Problems with textbox.selstart
Yes, i see what you are saying... but putting this at the end of the code
Code:
TextPolish.SelStart = TextPolish.SelStart + 1
causes this scenario...
if you type as you say "123456" and press back arrow to insert 9 between 3 and 4 this puts the first 9 after the 4 - between 4 and 5??, but if you type "99" it actually puts "12349596", so jumps another space and puts a 9 before and after the 5.
Code:
Private Sub TextPolish_KeyPress(KeyAscii As Integer)
If KeyAscii = 35 Then
TextPolish.SelText = Chr$(159): KeyAscii = 0
ElseIf KeyAscii = 39 Then
TextPolish.SelText = Chr$(185): KeyAscii = 0
ElseIf KeyAscii = 59 Then
TextPolish.SelText = Chr$(179): KeyAscii = 0
ElseIf KeyAscii = 64 Then
TextPolish.SelText = Chr$(234): KeyAscii = 0
ElseIf KeyAscii = 91 Then
TextPolish.SelText = Chr$(191): KeyAscii = 0
ElseIf KeyAscii = 93 Then
TextPolish.SelText = Chr$(156): KeyAscii = 0
ElseIf KeyAscii = 113 Then
TextPolish.SelText = Chr$(113): KeyAscii = 0
ElseIf KeyAscii = 123 Then
TextPolish.SelText = Chr$(241): KeyAscii = 0
ElseIf KeyAscii = 125 Then
TextPolish.SelText = Chr$(230): KeyAscii = 0
ElseIf KeyAscii = 126 Then
TextPolish.SelText = Chr$(159): KeyAscii = 0
End If
TextPolish.SelStart = TextPolish.SelStart + 1
End Sub
Reading the code you would think that the .SelStart event is only incremented after the character insertion, but this doesn't 'seem' to be so.
So in theory you type a character between 3 and 4 like 9 and get 394, but you don't you get 349. A long string like a sentence with a full word typed in the middle like this:
and then athe the start type "he ran" gives you
It just isn't behaving like you would think it should. With your example you would think that if you inserted the 9 after the 3 you would get 394 and the cursor then being between the 9 and 4 but this is not so. (interesting encryption though - hmm!)
This has been interesting to say the least, it seems to increment the SelStart property before the insertion of the character.
If you take out the SelStart altogether it behaves like i wanted but initially as I did not insert the text with the code
Code:
textbox.seltext = "text to be inserted"
I was initially reading the string, splitting it into left and right parts then joining left+inserted+right and replacing the string. Seems to have been a bad idea at the offset. Still, you guys put me to rights there.
by using the the code with seltext it does what I need without extra code and i don't need the selstart at all now, but it was interesting to learn that it does not behave as you would expect - i'll remember that if i ever need it in the future.
This is the code now - text box to "arial" font in central european characters
Code:
Private Sub TextPolish_KeyPress(KeyAscii As Integer)
If KeyAscii = 35 Then
TextPolish.SelText = Chr$(243): KeyAscii = 0
ElseIf KeyAscii = 39 Then
TextPolish.SelText = Chr$(185): KeyAscii = 0
ElseIf KeyAscii = 59 Then
TextPolish.SelText = Chr$(179): KeyAscii = 0
ElseIf KeyAscii = 64 Then
TextPolish.SelText = Chr$(234): KeyAscii = 0
ElseIf KeyAscii = 91 Then
TextPolish.SelText = Chr$(191): KeyAscii = 0
ElseIf KeyAscii = 93 Then
TextPolish.SelText = Chr$(156): KeyAscii = 0
ElseIf KeyAscii = 113 Then
TextPolish.SelText = Chr$(113): KeyAscii = 0
ElseIf KeyAscii = 123 Then
TextPolish.SelText = Chr$(241): KeyAscii = 0
ElseIf KeyAscii = 125 Then
TextPolish.SelText = Chr$(230): KeyAscii = 0
ElseIf KeyAscii = 126 Then
TextPolish.SelText = Chr$(159): KeyAscii = 0
End If
End Sub
Thanks for you help
Last edited by mickle026; Jul 2nd, 2010 at 12:26 AM.
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
|