By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

VB 6 Textbox gotfocus event

P: 3
Hi Everyone,

I know i can get the selstart when the textbox_click event fires. But I want to get the cursor position when the textbox_gotfocus event is fired. It seems to be returning a 0. I might be wrong.

can you guys give me any input on this situation

In simpler words, Can you please guide me as to how I can get the position of the cursor in the Textbox when the textbox gets focus.

Thanks for looking

Vinay
Sep 10 '07 #1
Share this Question
Share on Google+
7 Replies


pureenhanoi
100+
P: 175
Hi Everyone,

I know i can get the selstart when the textbox_click event fires. But I want to get the cursor position when the textbox_gotfocus event is fired. It seems to be returning a 0. I might be wrong.

can you guys give me any input on this situation

In simpler words, Can you please guide me as to how I can get the position of the cursor in the Textbox when the textbox gets focus.

Thanks for looking

Vinay
if the texbox already has text-value, the cursor always go to end possition of the text when textbox has forcus.
Sep 10 '07 #2

Robbie
100+
P: 180
if the texbox already has text-value, the cursor always go to end possition of the text when textbox has forcus.
Actually, on VB6, the cursor is always at the start (0) by default. It also changes back to 0 after you change the textbox's Text property through code.

Can you please guide me as to how I can get the position of the cursor in the Textbox when the textbox gets focus.
I don't know what you mean by 'when it gets focus' - I thought it would be the same way as ever:
CursorPosition = TextBox.SelStart

I tried it out, and yes, the initial value it gives back is 0. But if you move the cursor to a different position in the textbox, make it lose focus then bring focus back, it returns the new correct value of the cursor position, not 0.

In fact, the 0 at first is also correct - it just means that the cursor's at the far left, which it is the first time the textbox gets focus, unless you specifically change it in code beforehand, by doing something like this:
TextBox.SelStart = 4

See what I mean? Or was that not what you were asking? >_<
Sep 10 '07 #3

P: 3
Thank you to both for a quick reply.

I shall elaborate a little bit more.

I have a line of text in a Rich Textbox. When the user clicks on a particular portion of text ( enclosed in <<..>> ) , I pop up another regular text box for input into the field.

My program can only have one textbox on the screen at any one time. So when the user clicks on the "text", the Rich textbox loses focus and the normal textbox receives focus.

Now what i need is, when the user clicks back onto the rich textbox, the program has to check if the user clicked on a "text field". for this to happen, i need the selstart when the user clicks back onto the rich text box. At this stage, the textbox_focus fires and gives me a selstart of zero. but ideally , i need the selstart to be the position where the mouse clicked.

I hope i made myself clearer.

Thanks for looking.

Vinay
Sep 10 '07 #4

Expert 5K+
P: 8,434
When I tried a quick test, I got the correct value each time in SelStart. I even tried switching to another form and back, and it still worked. Maybe you had better show us the relevant code. Perhaps you have something else making a change before GotFocus fires. Perhaps in a MouseDown or Click event?
Sep 10 '07 #5

QVeen72
Expert 100+
P: 1,445
Hi,

For textBox as well as Rich TextBox, First Time When it gets the Focus, the SelStart /Cursor Position is always 0 (unless Explicitly Set).
after changing in the text, if u go back to the TextBox, the Cursor Position remains Same, till there is change in the Text.
If every this is very confusing, Just keep a FormLevel Variable of the Cursor Position, In LostFocus of the Rich textBox, Populate that Variable.
When again the RichtextBox, gets the Focus, Set SelStart to that Variable position(In GotFocus Event).

Regards
Veena
Sep 10 '07 #6

Expert 5K+
P: 8,434
For textBox as well as Rich TextBox, First Time When it gets the Focus, the SelStart /Cursor Position is always 0 (unless Explicitly Set). after changing in the text, if u go back to the TextBox, the Cursor Position remains Same, till there is change in the Text.
Unless you "go back" by clicking on the testbox (or RTB). In which case, the select position changes to where you clicked. At least, that's what happened in my testing.
Sep 10 '07 #7

P: 3
Thank you Killer and Qveen for your prompt replies.

I did not realise that i was changing the text which was reseting the selstart to zero. Thanks to your suggestions, i was able to pin point and fix the error.

So in simpler words, Selstart does not reset itself, but like QVeen said, it better to store the value and reset it.

Appreciate your help guys,

Vinay
Sep 10 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.