Recent

Author Topic: When code gives the focus to a TEdit that contains some text ..  (Read 7872 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
Good people,

When a short procedure completes giving the focus to a Tedit box which already contains some text, the text appears SHADED or highlighted, meaning that it is selected.

Now, at this point the TEdit is ready to take characters from the keyboard.

However, the first character that is typed WIPES out the existing text, and then that first character typed appears, and it is NOT highlighted, so more characters can be typed.

So, how to pass the focus to that TBox WITHOUT the text appear selected, so that the next char typed only ADDS to the existitng text?

I suppose it'd be a command like "unselect" the Text so it is right then unselected?

Thanks!

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: When code gives the focus to a TEdit that contains some text ..
« Reply #1 on: August 17, 2011, 01:24:18 pm »
  Edit1.SetFocus;
  Edit1.SelLength := 0;
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: When code gives the focus to a TEdit that contains some text ..
« Reply #2 on: August 17, 2011, 02:26:01 pm »
  Edit1.SetFocus;
  Edit1.SelLength := 0;

Thank you, Zoran.

This gives me an unselected text, except that the next char one types is added to the left.

What you code is fine, but if only the cursor were at the end of the string.

Now, I tried using the Length function, which figures the length of the string in the box, and put that instead of the zero. Yes, the cursor is now at the end, but the next char I type DELETES THE entire string.

In other words, with the Length functin applied, I am back to the original question. :'(

Any suggestions?

Thanks!

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: When code gives the focus to a TEdit that contains some text ..
« Reply #3 on: August 17, 2011, 02:44:39 pm »
What you code is fine, but if only the cursor were at the end of the string.

  Edit1.SetFocus;
  Edit1.SelStart := Length(Edit1.Text);
  Edit1.SelLength := 0;
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: When code gives the focus to a TEdit that contains some text ..
« Reply #4 on: August 17, 2011, 03:08:58 pm »
What you code is fine, but if only the cursor were at the end of the string.

  Edit1.SetFocus;
  Edit1.SelStart := Length(Edit1.Text);
  Edit1.SelLength := 0;

Hi Zoran,

to let you know that this above code worked EXACTLY as needed and I had been trying to solve this for the last 3 weeks or so.

So, I will put it my treasure box!

 ;)

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: When code gives the focus to a TEdit that contains some text ..
« Reply #5 on: May 20, 2012, 01:49:14 am »
I had this exactly same problem except the code provided didn't work.  Is there a bug or anything else that should be considered?

Code upon hitting enter in another edit on Form4:

Form4.Hide;
Form1.Edit1.SetFocus;
Form1.Edit1.SelStart := Length(Form1.Edit1.Text);
Form1.Edit1.SelLength := 0; 

After executed all of the text in Form1.Edit1 remains selected.

Bart

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
Re: When code gives the focus to a TEdit that contains some text ..
« Reply #6 on: May 21, 2012, 11:46:50 am »
TEdit has a property AutoSelect.
When set to False, it should not select text when the control gets focus.

Bart

 

TinyPortal © 2005-2018