Page 1 of 1

[Solved] Setting focus in table grid with a macro

Posted: Thu Jan 28, 2010 10:20 pm
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!

Re: Setting focus in table grid with a macro

Posted: Thu Jan 28, 2010 10:33 pm
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. 

Re: Setting focus in table grid with a macro

Posted: Thu Jan 28, 2010 11:37 pm
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 14819 times
I want to place the focus on a specific column (named "Cause") in the very lowest TableControl.
Thanks for the help.

Re: Setting focus in table grid with a macro

Posted: Fri Jan 29, 2010 12:02 am
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. 

Re: Setting focus in table grid with a macro

Posted: Fri Jan 29, 2010 1:55 am
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?

Re: Setting focus in table grid with a macro

Posted: Fri Jan 29, 2010 4:19 am
by RPG
Hello

read this for understanding a grid control.

and this for setting focus

Romke

Re: Setting focus in table grid with a macro

Posted: Fri Jan 29, 2010 6:19 pm
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)?

Re: Setting focus in table grid with a macro

Posted: Fri Jan 29, 2010 6:32 pm
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).

Re: Setting focus in table grid with a macro

Posted: Fri Jan 29, 2010 6:44 pm
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 

Re: Setting focus in table grid with a macro

Posted: Sat Jan 30, 2010 2:11 am
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

Re: Setting focus in table grid with a macro

Posted: Sat Jan 30, 2010 3:24 am
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?

Re: Setting focus in table grid with a macro

Posted: Sat Jan 30, 2010 4:39 am
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

Re: Setting focus in table grid with a macro

Posted: Sat Jan 30, 2010 8:40 am
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.

Re: Setting focus in table grid with a macro

Posted: Sat Jan 30, 2010 2:33 pm
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

Re: Setting focus in table grid with a macro

Posted: Sat Jan 30, 2010 9:27 pm
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.

Re: Setting focus in table grid with a macro

Posted: Sat Jan 30, 2010 9:59 pm
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

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

Posted: Tue Mar 21, 2017 4:50 am
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:

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

Posted: Tue Mar 21, 2017 2:07 pm
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

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

Posted: Sat Mar 25, 2017 2:28 am
by Koa
:-) Very good. Solved! Thank you.