Recent

Author Topic: How do I set the first control in a tab order at startup?  (Read 2157 times)

AMJF

  • Jr. Member
  • **
  • Posts: 53
How do I set the first control in a tab order at startup?
« on: April 25, 2021, 09:20:06 am »
I have a form with many controls and I've arranged them so they focus in the order I tab them in, but I would like to know how to set one of the controls to be in focus at program startup?

egsuh

  • Hero Member
  • *****
  • Posts: 1819
Re: How do I set the first control in a tab order at startup?
« Reply #1 on: April 25, 2021, 09:35:51 am »
You can force it focused setting control.focused := true, at OnShow or OnActivate event handler.

AMJF

  • Jr. Member
  • **
  • Posts: 53
Re: How do I set the first control in a tab order at startup?
« Reply #2 on: April 25, 2021, 09:49:01 am »
I notice that it's always the most recently added control that has the initial focus, isn't there a property that I can set the focus on rather than writing code to do so?

egsuh

  • Hero Member
  • *****
  • Posts: 1819
Re: How do I set the first control in a tab order at startup?
« Reply #3 on: April 25, 2021, 10:30:22 am »
You will be able to do that by setting the taborder property of the control you want to be focused initially to 0.

AMJF

  • Jr. Member
  • **
  • Posts: 53
Re: How do I set the first control in a tab order at startup?
« Reply #4 on: April 25, 2021, 10:37:11 am »
I don't think so, not in my experience.

I have the tab order set as I want it, but the control at 0 is NOT in focus at startup, but the control at 3 is. The control at 3 is the last one I added to the form, BTW.

dseligo

  • Hero Member
  • *****
  • Posts: 1686
Re: How do I set the first control in a tab order at startup?
« Reply #5 on: April 25, 2021, 10:46:06 am »
You have ActiveControl property in Object Inspector where you can choose your active control without writing code.

wp

  • Hero Member
  • *****
  • Posts: 13628
Re: How do I set the first control in a tab order at startup?
« Reply #6 on: April 25, 2021, 11:08:14 am »
I recently discovered that the "Tab Order" command in the context menu opens a dialog with a sort button and a checkbox "Recursively". When this checkbox is checked and the sort button is clicked the tab order of all controls is set in the order top/left to right/bottom, and the first control in the resulting list becomes the one which is automatically focused - without having to set the ActiveControl of the form.

AMJF

  • Jr. Member
  • **
  • Posts: 53
Re: How do I set the first control in a tab order at startup?
« Reply #7 on: April 25, 2021, 11:43:01 am »
I've solved the problem now, and have implemented ActiveControl anyway, just in case. Thanks!

Background:

I wrote a program in Lazarus last year that had no issues with the tab order and I didn't use or even know about ActiveControl then, but for this current case I think I know what went wrong:

My program needs a configuration file to function, so I thought until it finds a valid one, I should disable the controls like buttons, list boxes and edit boxes. However, I chose not to do it in code at first, but to change them in the properties in Object Inspector instead, then enable it in code later. Afterwards, I added the control that was in focus at startup and forgot to disable it, so that's why it was in focus. I assume if a control is disabled, it will move to the first enabled one at startup?

Anyway, I've fixed all the bugs and it's now working.

 

TinyPortal © 2005-2018