Author Topic: Keep focus in textbox.  (Read 3596 times)

0 Members and 1 Guest are viewing this topic.

mr_nick

  • Guest
Keep focus in textbox.
« on: January 11, 2007, 06:41:01 AM »
I have a form which contains a textbox alongside which there is a counter which tells me how many characters I have typed. This works well when typing from start to finish but if I try to go back and change something by clicking into previous text, I can only enter one character and then the focus flicks to the end of the string. I assume this is because the focus goes from the textbox to the counter and then back and when the focus returns, it simply goes to the end og the text.

How can I keep the focus in my textbox whilst updating the other control? I have tried using DoEvents but it doesn't seem to work for me - though this could very easily be due to me using it all wrong  :oops:.

Can anybody please point me in the right direction?

hendie

  • Guest
Re: Keep focus in textbox.
« Reply #1 on: January 11, 2007, 06:48:53 AM »
something like
Code: [Select]
TextBox2.Text = Len(TextBox1.Text)will report the number of characters typed anywhere into textbox1.
or have I misunderstood

We would really need to see your code to see how you are doing it as it sounds like you have some other events going on there
« Last Edit: January 11, 2007, 06:50:11 AM by hendie »

mr_nick

  • Guest
Re: Keep focus in textbox.
« Reply #2 on: January 11, 2007, 07:01:51 AM »
This is what I'm currently using. The theory is that I want to use a maximum of 280 characters so this caption shows me a counter in the format of characters available left to use i.e a decreasing counter.

Code: [Select]
Private Sub txtRevText_Change()
  Count = 280 - txtRevText.TextLength
  lblTxtCntr.Caption = Count & "/280"
End Sub

I'm probably doing this all wrong (generally my normal approach  :-( ) so anything you can throw me would be greatly appreciated.

hendie

  • Guest
Re: Keep focus in textbox.
« Reply #3 on: January 11, 2007, 08:06:03 AM »
I don't see anything there which would throw your cursor to the end of the string when entering data.
Are you sure there is no other control referencing txtRevText which may be acusing this ?

Are you using the maxlength property at all ?

mr_nick

  • Guest
Re: Keep focus in textbox.
« Reply #4 on: January 11, 2007, 08:42:53 AM »
There is a MaxLength value set for the text box - I'm assuming that as you asked the question, this is where the problem lies.

hendie

  • Guest
Re: Keep focus in textbox.
« Reply #5 on: January 11, 2007, 08:52:49 AM »
to be honest I'm not sure.
I just tried setting the maxlength property in my test here and it had no effect other than stopping when the character limit was reached. It didn't throw the cursor to the end of the string or anything like that.

You could try setting the maxlength to 0 and see if that has any effect.
The code you've posted does not change the focus from the textbox at any time. You could further reduce the code to
Code: [Select]
Private Sub txtRevText_Change()
  lblTxtCntr.Caption = (280 - txtRevText.TextLength) & "/280"
End Sub
but I don't see that having any effect either, sorry.

mr_nick

  • Guest
Re: Keep focus in textbox.
« Reply #6 on: January 11, 2007, 09:18:33 AM »
I am now more puzzled. I first changed, the MaxLength to 0 and this had no effect. I then reduced the code as you showed and this of course had no effect.  I have just closed and restarted AutoCAD and now the cursor is no longer jumping. I have tried every which way to try and replicate the problem but it just won't happen now. I guess I should be pleased that the problem appears to have gone away but I always like to know what causes these things so I can avaoid the problem occurring again.

Thanks for your input on this one but sorry there hasn't been a true resolution.

hendie

  • Guest
Re: Keep focus in textbox.
« Reply #7 on: January 11, 2007, 10:03:35 AM »
hey, at least you got the problem resolved even if it was by the CAD fairy