Setting focus to a particular screen object

Hi.

If I have a screen with several screen objects (eg. buttons, text boxes, data grids etc.) is there a way to programmatically set focus to a particular object, for example from within CodeBehind or a class function?

Can the object Control Name be used for this purpose? I have seen mention in the documentation of using the object Control Name within CodeBehind, but have only found an example relating to SparkLines.

There appears to be a huge amount of functionality accessible using the Control Name … but virtually no documentation or example code describing usage.

Hi Greg,

When you want to take control of an object you can use the following code:
image

In this case it is a TTextBox, for button you will use TButton…, just press Ctrl+Space to see all possibilities, also the “text1” is the uid of the object and “a” is the control name.

So if you want to access all the methods from this object you can type “a.” in a new line and it will open a whole list.
image

In what situation do you want to use this focus? You can use this code below to get focus when starting the application in DisplayOpening().
image

Let us know if it works for you please.

Best Regards,
Tatsoft Team.

Thanks for the response Eduardo, which has been useful … although I still must admit that I am finding the .NET environment overall to be extremely cryptic. There seems to me to be a lot of assumed knowledge required, and little on-line to help those starting out.

If you can offer any advice on a related question, it would be appreciated.

What you have described above allows setting of keyboard focus.

I assume there is also a simple method available to tell when a particular object receives focus … but I have not been able to stumble on a successful approach despite a lot of searching. In particular, I have a DataGridWindow with control name “teststbl_grid” which is generally accessed after tabbing through a series of text boxes used for data entry.

I would simply like to know when focus is first obtained by the data grid. When this happens I will take particular action. If this can be determined within the CodeBehind for the screen that would be great, but any options would be appreciated.

1 Like

Hi Greg,

Really sometimes you will need some online help, but our codes are in C# and VB.NET which are accessible languages, you can use Taks and Classes to executed some codes in parallel (multithreading concept), and you can also always think that it is Oriented Object Programming so you can treat everything as an object

About the focus, this code below shows you how to create an event that when DataGrid obtains focus a function is called (Got) and when lost focus another function is called (Lost). You can do the same to others objects.

Please, don’t hesitate to contact us if you have any questions.

Best Regards,
Tatsoft Team.

Thanks for your response Eduardo, as it has also been helpful in several ways. I am gradually becoming more familiar with VB .NET and FactoryStudio.

Combining your earlier response describing the setting of focus to an object, and your most recent describing the detection of getting and losing focus, I have been doing some testing.

I’ve found that if I apply focus to a TTextBox by clicking with the mouse, the ‘GotFocus’ event indicates correctly and focus is retained.

However … if I apply focus to the TTextBox by executing the ‘Keyboard.Focus()’ method through a button press … focus is first indicated as ‘Got’, but then immediately indicated as ‘Lost’, and the cursor does not appear in the TTextBox. At that point, according to TAB behavior, focus appears to have been retained by the button used to execute ‘Keyboard.Focus()’.

The relevant code is within the CodeBehind for the screen as indicated below:

image

image
So, while the Keyboard.Focus() method appears to work initially … it doesn’t stick.

There is a similar problem with scrolling through a DataGridWindow, in that focus on the grid is repeatedly indicated as ‘Got’ then ‘Lost’.

I suspect the indication of losing focus may relate to the execution of code within CodeBehind.
Could that be possible … although focus is never directed to any other objects?

Is the coding I have used not suitable for CodeBehind?
Do you have any other suggestions on what else could be happening or what additional steps I may need to take?

Regards,
Greg Shearer

Hi again.

I’ve just found that setting focus using Keyboard.Focus() works correctly when executed using the assigned Hotkey. Also, if the button is configured as NOT Focusable, focus is also set as hoped.

So … it look slike I have answered my own question :slight_smile:
Thanks so much for your help!

Hi Greg,

That’s good! So, just to confirm, do you still need some help with the facts mentioned above? Also, can we close the ticket “re Forum topic: TAB order and actions”?

Best Regards,
Tatsoft Team.

Hi Eduardo.

Please definitely close the ticket “re Forum topic: TAB order and actions”. I certainly understand default TAB order now, and thanks to your examples am also generally able to control keyboard focus … which has been my aim all along.

I keep thinking I am gaining a better understanding of the VB .NET environment, but still find the huge range of object properties and often very terse documentation makes progress quite slow.

I am still puzzled as to why scrolling through a TDataGridWindow results in repeated Got and Lost focus (as shown below), so if you have any advice on how that can be prevented it would be appreciated. The example below is just scrolling from one row to the next. There is code executed within the screen DisplayIsOpen() on change of grid index which I expect may be related to this behaviour, although as mentioned earlier, focus is not re-directed within that code.

image

Now that I am … at least initially … able to set focus to a TDataGridWindow, I am trying to determine how to restrict focus to a particular column. I am trying to determine if focus can be further specified as a particular column or row or cell … or whether this can be controlled through other object properties such as .SetColumn.

As always … any advice appreciated :slight_smile:

Hi Greg,

After a little searching we found that it is known in .NET, you can check the following link to see if it helps you:

About the focus in specific parts of the grid, have you tried to use this 2 fields in DataGrid component?

Selected Values will show to you the row value in DataGrid, and Selected Index will show you the number of the row.

Best Regards,
Tatsoft Team.

Hi again Eduardo.

Thanks once more for your response, and in particular tracking down the suggestion on Stackoverflow! It does look like a rather technical work-around, but I will use it if necessary.

We are already making use of the ‘Selected Values:’ and ‘Selected Index:’ fields, so we are aware of the grid row. If possible I would like to restrict focus/data entry to a single column within the grid. If not, the grid will still work ok as is, but the operator will need to arrow or mouse to the editable field.

As mentioned earlier, I had hoped to us a function along the lines of:

grid_object.SetColumn()

to specify the active column … but when compiled it gives …

image

Which is correct, as it is not documented as a RunControl for TDataGridWindow … however it is selectable within the CodeBehind editor. So I continue to be a little confused :slight_smile: as I don’t understand what else grid_object.SetColumn() would be used for?

I will continue to investigate a little further and post any improvements I find here.

Regards,
Greg Shearer

Hi Greg,

We found a solution for you using a method called “IsKeyboardFocusWithin”. A project is attached in version 9.2.26-sp2, so you can see how it works and find a workaround for you. When you select a specific column defined in the CodeBehind a tag goes its value from 0 to 1, when you move to another column this value goes from 1 to 0.

Best Regards,
Tatsoft Team.

Hi Eduardo.

Although … as before … I couldn’t open your project, I’ve since found that IsKeyboardFocusWithin works well to determine when focus of an object is gained or lost.

I have also found that while the Keyboard.Focus(a) method works well for directing focus to text boxes … it does not appear to work to set focus to a DataGrid. I have been looking for some time now, but have so far been unable to determine how this can be done.

In my project, while the DataGrid is active, another screen will be opened for periods before being closed and returning to the DataGrid screen. This works well … except that on return focus does not return to the DataGrid. This will require the operator to interrupt their workflow in order to locate, position and click on the mouse just in order to return focus to the required location within the DataGrid.

Also, I have yet to find a method of programmatically setting keyboard focus to a nominated cell in the DataGrid. As mentioned earlier, functions such as SetColumn() and SetRow() appear to be known of … but are not accepted.

Given the myriad of features provided through the .NET environment … I had not expected basic keyboard focus and navigation requirements to pose so many problems :slight_smile:

Hi Greg,

1 - What FactoryStudio version are you using?

2 - In the following link you can see an example we did, let us know if it is what you want

OBS: In this video we didn’t use mouse to do anything, we just used the TAB to move from one object to others and the arrows to move inside the DataGrid.
In the left part we have the example focusing in a specific column, in righ part we have an example Got and Lost focus in a Grid.

Best Regards,
Tatsoft Team.

Hi Eduardo.

My FactoryStudio version is the same as yours … although the Product Family (EdgeHMI) is almost certainly much inferior to yours. My understanding is EdgeHMI only supports VB .NET. Could that be an issue?

If you can figure out why I can’t open your projects it would be great!

image

Hi again Eduardo.

As mentioned earlier, I can now detect when the grid gains and lose focus … although I haven’t been able to determine which column has focus as in your application … but I am still looking :slight_smile:

I can navigate the grid fine using arrow and TAB keys, but am still looking for functions that will allow full coded/programmatic control.

Hopefully you will find attached a video showing the loss of focus that takes place when another screen is opened and then closed. In order to return focus to the grid with the fewest keystrokes you need to use:

 Shift-TAB
 Right-arrow

or

 Shift-TAB
 TAB

https://drive.google.com/file/d/11UcYejOB1A3zhcA_LkuLStvfNRgQIYhR/view?usp=sharing

Hi Greg,

We recreated the project in EdgeHMI, try to open this project:

If it is not opening you may disable file security by doing Right click > Properties > General > Unblock > Apply:

image

About the video you sent us, do you need something or how it works is good?
If you can’t open this project we can send you the Code and print screens to reproduce, it is a very simple project.

Best Regards,
Tatsoft Team.

Hi Eduardo.

Unfortunately, the same result whether or not the project file is unblocked :confused:

Whatever the problem is … it must be rather subtle …
I notice that our license ‘Product Model’ is 150 versus the project’s 1500.
Could this be relevant?
My assumption is that this just refers to the maximum communication points allowed … which wouldn’t be a problem with the sample project.

I realise that our licenses are quite small in terms of points etc., and suspect this will be somehow related. The comment displayed permanently at the top of the ‘Info’ page for projects you have sent is:

image

Hi Greg,

Can you send us a print screen of Info > Project > Settings?

Also a print screen of Welcome(where you open a project) > License.

Best Regards,
Tatsoft Team.

I’ve found that I might be able to avoid the loss of grid focus I described earlier, by setting the display type of the opened screen to PopupWindow. This does not appear to automatically result in changed focus to the underlying display. Loss of focus can still occur if there is keyboard/mouse interaction with the PopupWindow, but this will generally be avoided within our application.

Until I develop the application further, I won’t know if this there might be any other unwanted affects.