[Solved] Setting focus in table grid with a macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
void64
Posts: 30
Joined: Sun Jun 21, 2009 9:57 pm

[Solved] Setting focus in table grid with a macro

Post by void64 »

I have a form with a table (grid) control. How can I move the input focus to a particular column in the table from within a macro? Thanks!
Last edited by void64 on Sat Jan 30, 2010 10:00 pm, edited 1 time in total.
Dream of Space? Help make it Real.
http://www.open-aerospace.org/
User avatar
Villeroy
Volunteer
Posts: 31270
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Setting focus in table grid with a macro

Post by Villeroy »

The table (grid) control bundles other controls in columns. Focus a control within the grid.
 Edit: I'm wrong. It is not that simple. 
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
void64
Posts: 30
Joined: Sun Jun 21, 2009 9:57 pm

Re: Setting focus in table grid with a macro

Post by void64 »

Villeroy wrote:The table (grid) control bundles other controls in columns. Focus a control within the grid.
Can you be a little more specific? I'm still not that well-versed in the ooBasic object model. I tried using the code snippet shown here as an example, but I'm having difficulties because my table is in a sub-sub-form.
Clipboard02.jpg
Clipboard02.jpg (17.05 KiB) Viewed 14723 times
I want to place the focus on a specific column (named "Cause") in the very lowest TableControl.
Thanks for the help.
Dream of Space? Help make it Real.
http://www.open-aerospace.org/
User avatar
Villeroy
Volunteer
Posts: 31270
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Setting focus in table grid with a macro

Post by Villeroy »

While in edit mode, select a column header of the grid control and call the properties dialog. Watch the dialog title and the properties. Right-click the column and see the control types to replace the current column control.
A table control is not a control in its own right. It can not be bound. Most event routines do not work. It consists of other controls, one per column, showing one instance of the same control bundle per row.
 Edit: Anyhow, I can't select any of them by macro. 
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
void64
Posts: 30
Joined: Sun Jun 21, 2009 9:57 pm

Re: Setting focus in table grid with a macro

Post by void64 »

Looks like there is no method to set the focus specifically to a random column ... bummer. As an alternative, I figured if I can send a "tab" key-stroke than this would allow me to walk my way to the control I want. I used the macro recorder to capture a code snippet that does just that (send a tab) and got:

Code: Select all

sub Tab

	rem ----------------------------------------------------------------------
	rem define variables
	dim document   as object
	dim dispatcher as object
	rem ----------------------------------------------------------------------
	rem get access to the document
	document   = ThisComponent.CurrentController.Frame
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

	dim args1(0) as new com.sun.star.beans.PropertyValue
	args1(0).Name = "Text"
	args1(0).Value = CHR$(9)

	dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())

end sub
But when executing it nothing seems to happen - where did I go wrong?
Dream of Space? Help make it Real.
http://www.open-aerospace.org/
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Setting focus in table grid with a macro

Post by RPG »

Hello

read this for understanding a grid control.

and this for setting focus

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
void64
Posts: 30
Joined: Sun Jun 21, 2009 9:57 pm

Re: Setting focus in table grid with a macro

Post by void64 »

RPG wrote:Hello

read this for understanding a grid control.

and this for setting focus

Romke
Thanks, that was helpful! I tried using this code snippet to set the focus:

Code: Select all

oController = ThisComponent.CurrentController
oForm = ThisComponent.drawpage.forms.GetByName("FailureModes")
cControl = oForm.GetByName("CauseName")
oControlView = oController.GetControl(cControl)
oControlView.SetFocus  'sets the focus
However the 2nd line fails ("no such component") because the form containing the table grid is a sub-form. Can you tell me how to modify this code so I can access the table in the sub-form (rather than the main form)?
Dream of Space? Help make it Real.
http://www.open-aerospace.org/
User avatar
Villeroy
Volunteer
Posts: 31270
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Setting focus in table grid with a macro

Post by Villeroy »

void64 wrote:Can you tell me how to modify this code so I can access the table in the sub-form (rather than the main form)?
It's all in Romke's first link: http://user.services.openoffice.org/en/ ... ol#p109535 (last snippet).
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
void64
Posts: 30
Joined: Sun Jun 21, 2009 9:57 pm

Re: Setting focus in table grid with a macro

Post by void64 »

Almost, except the "GetControl" method returns an empty object when used on the oField object, so I get an error when I try to use the "SetFocus" method. I can xray the oField object just fine though, and it is the correct object that I'm trying to set the focus to ...?

Code: Select all

oDoc            = thiscomponent.drawpage.forms
oForm           = oDoc.getbyname("Steps")
oSubForm        = oForm.getbyname("CompList")
oSubSubForm     = oSubForm.getbyname("FailureModes")
oSubSubFormGrid = oSubSubForm.getbyname("TableControl")
oField          = oSubSubFormGrid.GetByName("CauseName")

'xray oField

oControlView    = ThisComponent.CurrentController.GetControl(oField) 'THIS LINE RETURNS EMPTY
oControlView.SetFocus 
Dream of Space? Help make it Real.
http://www.open-aerospace.org/
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Setting focus in table grid with a macro

Post by RPG »

Hello

You have to modify the code to your needs. you can not complete copy the code.


Maybe this link is better.
Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
void64
Posts: 30
Joined: Sun Jun 21, 2009 9:57 pm

Re: Setting focus in table grid with a macro

Post by void64 »

RPG wrote:Hello

You have to modify the code to your needs. you can not complete copy the code.


Maybe this link is better.
Romke
Hi Romke, thank you for trying to help! I did write the last code snippet I posted myself, using the links you provided as an example. The problem is not finding the object, but the fact that there seems to be no control associated with elements that are embedded in a table grid. At least that's my guess why "ThisComponent.CurrentController.GetControl(oField)" returns an empty object, and then the "SetFocus" fails because it has no object to work with.

If you want to try it, simple create a form, a table control with some named field in it, and then try to set the focus to it using a macro. I cannot find a way to do this ... I'm starting to think this is a limitation/bug in ooBase that has no work around?
Dream of Space? Help make it Real.
http://www.open-aerospace.org/
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Setting focus in table grid with a macro

Post by RPG »

Hello

I did not have seen the line of Villeroy he say that it is not possible. I think it is true if Villeroy tells that.

I have never tried to select a field in a gridtable. But you can select a column in a grid table
ogrid.select(ogrid.getbyname("TextField1"))

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
Villeroy
Volunteer
Posts: 31270
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Setting focus in table grid with a macro

Post by Villeroy »

RPG wrote:Hello

I did not have seen the line of Villeroy he say that it is not possible. I think it is true if Villeroy tells that.
I was just trying to say that I'm not able to do what I suggested in the first place because I had dialog controls in mind which are slightly different from bound form controls.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Setting focus in table grid with a macro

Post by RPG »

Hello Villeroy

I make the words to free and you did not say it is impossible. It is a mistake from me.
On this moment I do also not have a solution but I come a little shorter to a solution.

For every body
Gridtable
A table in a form give for a lot of people problems. A table control is not a form but a normal control who can contain more controls.
The controls in a table control can have little other properties then they have outside a table control. I have notice that once with a listbox in a table control. There the table control is a control in his own it have also special properties.
You can select a column. get all the values from that column.

Viewmodel of grid
You can move from one column to an other column and set the focus to a column. The only problem I have on this moment is: It seems that you delete the data

Code: Select all

oTableView = GetControlview(thiscomponent,thiscomponent.getCurrentController,oTablemodel.name)
oTableView.CurrentColumnPosition=1
oTableView.setFocus 'CurrentColumnPosition=2
Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
Arineckaig
Volunteer
Posts: 828
Joined: Fri Nov 30, 2007 10:58 am
Location: Scotland

Re: Setting focus in table grid with a macro

Post by Arineckaig »

You may find some helpful code by QuazzieEvil at:

http://www.oooforum.org/forum/viewtopic ... 178#281178

The key line for setting the focus on a field in a Grid control is use of the index to the columns position:

Code: Select all

GridControl.setCurrentColumnPosition(1) 
This method and the SetFocus() method both require the View of the Grid control. The view is obtained from a combination of the CurrentController of the Writer Form Document and the Model of the Grid control.

You do not specify how the macro is to be triggered. The example is to be triggered by a double click event in the Grid control itself but whatever source event you use, you have to obtain the Model of the grid control and its ancestral Writer Form Document object. The latter is ofter supplied by

Code: Select all

FormDoc=ThisComponent
If the event source is not linked to the Grid control, I suggest it should always be possible to get that Model of the required Grid control by repeated use of the GetByName() method working down from the Writer Form Document through its Draw Page to the Forms Collection and so on down through named forms and sub-forms.

I should mention that the code also assumes the required record has already been selected in the Grid control.

Though probably not relevant to your need, I should also confess that I find QuazzieEvil's code works better if adjusted merely to use a single click in the Grid control, but I hesitate to question the Master.
When this issue has been resolved, it would help other users of the forum if you add the word - [Solved] - to the Subject line of your 1st post (edit button top right).
AOOo 4.1.5 & LO 6 on MS Windows 10 MySQL and HSQLDB
void64
Posts: 30
Joined: Sun Jun 21, 2009 9:57 pm

Re: Setting focus in table grid with a macro

Post by void64 »

That was the solution! The mistake was trying to get the control for the Field (which returned empty) rather than getting the control for the Table and then setting the current column position to the desired field - the field object is not used at all. In my particular case (where the table resides in a sub-sub-form), the final working code is:

Code: Select all

oDoc            = thiscomponent.drawpage.forms
oForm           = oDoc.getbyname("Steps")
oSubForm        = oForm.getbyname("CompList")
oSubSubForm     = oSubForm.getbyname("FailureModes")
oSubSubFormGrid = oSubSubForm.getbyname("TableControl")

oControlView    = ThisComponent.CurrentController.GetControl(oSubSubFormGrid)        
oControlView.SetFocus
oControlView.setCurrentColumnPosition(2)  
Thank you very much :D
Dream of Space? Help make it Real.
http://www.open-aerospace.org/
Koa
Posts: 12
Joined: Sun Jan 15, 2017 11:53 pm

Re: [Solved] Setting focus in table grid with a macro

Post by Koa »

You can also move to a column relative rather than absolute, with this:

Code: Select all

oControlView.setCurrentColumnPosition(oControlView.getCurrentColumnPosition +4	)
Still looking for how to move to the column by name, starting with something like:

Code: Select all

oControlView.model.getByName("To")

... Thanks, very much for this old thread, still very much useful! :bravo:
LibreOffice 5.2.5.1 on Linux Jessie 8.6
Arineckaig
Volunteer
Posts: 828
Joined: Fri Nov 30, 2007 10:58 am
Location: Scotland

Re: [Solved] Setting focus in table grid with a macro

Post by Arineckaig »

Still looking for how to move to the column by name, starting with something like:

Code: Select all

 oControlView.model.getByName("To")
To move the focus to a particular column in a Grid form control it is necessary to use its View, which has a setCurrentColumnPosition() method but that requires the index number of the column. Only the Model of the Grid form control can get to its columns both by Name as well as by Index. Thus search for the index number of the column where its Model has the required name, so that the View can then use that number for its setCurrentColumnPosition(Index#) method. Some code like this might suit:

Code: Select all

For i = 0 to oControlView.Count-1
  If oControlView.GetByIndex(i).Model.Name = "To" THEN
     oControlView.setCurrentColumnPosition(I)      
     Exit For
  End if
Next
When this issue has been resolved, it would help other users of the forum if you add the word - [Solved] - to the Subject line of your 1st post (edit button top right).
AOOo 4.1.5 & LO 6 on MS Windows 10 MySQL and HSQLDB
Koa
Posts: 12
Joined: Sun Jan 15, 2017 11:53 pm

Re: [Solved] Setting focus in table grid with a macro

Post by Koa »

:-) Very good. Solved! Thank you.
LibreOffice 5.2.5.1 on Linux Jessie 8.6
Post Reply