[Solved] Disable the focus on a form

Discuss the database features

[Solved] Disable the focus on a form

Postby longi » Fri May 06, 2016 6:09 pm

Hi!
Simple problem, (if you do not consider my English)
After copying and adapting some code from ‘UnklDonald418’ I put it in a database. It runs in a form and all subforms it has.
I adapted it to show the name of form /subform and the field in which we put the focus previously.
The macro is launched by a key combination (Ctrl+x).
If you open the form and press the key combination, you won’t have any answer, but, if you click on a field (it doesn’t matter which), you’ll have an answer: the name of the form (or subform name) and the name of the field.
The problem is that if you click in other field belonging to another subform or form, the answer include the previous result and also the new. So we have several answers. A cascade of responses.
I’m wondering if it is possible disconnect the form ('Deactivated form') each time the cycle starts, in order to have only as a result the last field (and also the last form) which has been touched.
I’m attaching the database example.

Best regards!
Attachments
CONTROL X2.7z
(17.37 KiB) Downloaded 55 times
Last edited by longi on Sat May 07, 2016 9:29 pm, edited 1 time in total.
OpenOffice 4.1.5 on Windows 10
LibreOffice 5.1 on Windows 7
LibreOffice 6.0.1 on Windows10
longi
 
Posts: 107
Joined: Mon Jul 15, 2013 5:04 pm

Re: Disable the focus on a form

Postby RPG » Fri May 06, 2016 8:43 pm

Hello

The methode currentcontrol cannot be deactivate. So you cannot use it in that way you do now.

I can not point you to a single line but I did have test it before you asked it here. I think the method getcurrentcontrol must be used when you are already work in a form. If you are not in that form then you get a wrong result. Maybe it is possible when you test if the currentcontrol does have focus. I have not played with it.

I think the best method is start with an event. This event must be of the form or from a control in that form. When you knew the form then you can get the form control.

[Solved] Get currently selected form control from a macro
Read link at the end

It is not so clear to me what you want.
Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2164
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Disable the focus on a form

Postby UnklDonald418 » Sat May 07, 2016 4:46 pm

As Romke noted
The methode currentcontrol cannot be deactivate.

However, if you add one more test ( hasFocus() ) to your code it will return a single answer.

Code: Select all   Expand viewCollapse view
             if not IsNull (oFormController.CurrentControl) Then
               if oFormController.CurrentControl.hasFocus() Then
                 msgbox "Some field has been selected in the " & oForm.name                            ' It show us the name of the form/subform
                 msgbox "And its name is " & oFormController.CurrentControl.model.name 
                            ' It show us the name of the field
                End if
             End if

This will work for most of the elements on your form, with one exception. The question I posted where you found my code has to do with the unexpected behavior of hasFocus() when looking at a table grid. On your form if you select a row on your table grid everything works as expected, but if you select a single element on the table grid hasFocus() always returns FALSE.

If you need to include grid elements then Romke's suggestion
I think the best method is start with an event

is your best bet.

My solution is to tie each Form Control Event “When receiving focus” to the following macro.
Code: Select all   Expand viewCollapse view
Global FocusdElement as variant

sub FindFocusdElement(oEvent)
dim oElement as object
dim stest as string

'Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
'oMRI = CreateUnoService( "mytools.Mri" )

oElement=oEvent.source
stest = oElement.model.parent.getName()
'oMRI.inspect(oElement)
if stest = FocFormElement  then ' oFocusdGrid.model.parent.getName() then
' print "already have this name"
  exit sub
else
   FocusdElement= oElement
end if
end sub             'FindFocusdElement

Each time an element is selected this macro will run, storing the oElement object in a Global variable (FocusdElement ).

To get what you are after you would need another macro (called by Ctrl-X) that reads the names from the Global variable and prints the messages.

Code: Select all   Expand viewCollapse view
sub PrintFocusdNames

Dim oElement as object
        oElement= FocusdElement
        msgbox "Some field has been selected in the " & oElement.model.parent.name       ' It show us the name of the form/subform
        msgbox "And its name is " & oElement.model.name
end sub


This isn't as convenient as the function because you have to tie each control to the macro "sub FindFocusdElement(oEvent)", but at least it works even if an element on the table grid is selected
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.6 & LibreOffice 6.1.5.2 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 1159
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Disable the focus on a form

Postby longi » Sat May 07, 2016 9:28 pm

Hi!
Thanks to Romke and UnklDonald418. I'm really glad because you has been using your time in my mad code.
To clarify something, I have no idea to program, however, as you have seen, I copy a lot from everywhere. In this case from your code.
I didn't see any simulation for base of the function 'Ctrl+"', which Access has, to copy the previous record for only a field, so I'm trying it. So, I want to use a key combination in ordert to use the code for every forms in a database ( So I can't use it for every control of an specific form).
I read about the concept "currentform", which doesn't exist (As Villeroy said), however, this solution which has been prepared by UnklDonald418, makes a simulation of this (which I think is very interesting). Now, I can try to put all my rest of code into the function running through all forms/subforms, and using it only in the field previously focused.
I tried to translate all my notes of this job, but it will take me some time (I,m really clumsy at English too), but I promise to put you the example, an also it could be tested by people who really know how things work about it.

The table control is something that I didn't try til now (I know it is almost impossible to deal with them), but all is step by step.
The fields that didn't respond are two, which are date fields. If you put the thirth item in their properties (format control) as 'no', the problem is solved.

Thanks!, and I'll put the threat as solved, but I promise to put here the final job if it works OK. :super:

Regards!
OpenOffice 4.1.5 on Windows 10
LibreOffice 5.1 on Windows 7
LibreOffice 6.0.1 on Windows10
longi
 
Posts: 107
Joined: Mon Jul 15, 2013 5:04 pm

Re: [Solved]Disable the focus on a form

Postby UnklDonald418 » Sat May 07, 2016 10:32 pm

If this is your goal
I didn't see any simulation for base of the function 'Ctrl+"', which Access has, to copy the previous record for only a field, so I'm trying it.

I have written two versions that work on a table grid for most data types. I could post it here if you are interested.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.6 & LibreOffice 6.1.5.2 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 1159
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: [Solved]Disable the focus on a form

Postby RPG » Sun May 08, 2016 8:54 pm

Hello

I looked again to the problem of copy a value of the record higher. I think it is not possible in OOo to do it in the same way as in Access. It is maybe only possible when it is done by the makers of OOo.

I think it is also not such a good idea to implement the Ctrl-X with BASIC code by hobby programmers. I see myself also as a hobby programmer. When you want do it then you must also take care for all possible problems. A simple code as UnklDonald418 used now it is not such a problem. You can take care that you only copy the values who give no problem.

I think it better to work in a way with OOo so that you have no problem with the fact there is no Ctrl-X. In other words rebuilding Acces in OOo-base is a bad idea.

I do have some questions. Why it is so important to have Ctrl-X in your forms. Also why want you copy those fields and do it not in an other way.

Some other way
a) Copy some fields when you acces the dataform or go to an other record.
b) Make the values default when you go to a new record. This is only for new records.

About programming in OOo
I think it is real important to understand all services you use. Understanding the different services is not easy but when you understand the service then the programming is less difficult and most of the time you need less code. It is better first to learn what you can do without macros. I have to confess that I did also first start with macros and did later understand what you can do without macros.

Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2164
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved]Disable the focus on a form

Postby longi » Mon May 09, 2016 9:25 am

Hi!
You are very clever to see my problem more than once.
I like the expression 'Hobby programmer', however, in my case (I don't know how to express it properly) I'm in a really low level, so maybe I don't catch this position.
The Ctrl+X problem is not really important to me, only I'm trying to show that all kind of things other programs has, we can also have.
I'm trying it as a general tool, because the makers of OOo seem to be stuck in other things, no in Base.
I thought about a writer document explaining things I did in the code (some of them are difficult to follow), however I want to explain in a resumed way what I did:


1. I used the UnklDonald418's code to go through all forms and subforms we have in the screen
2. I selected only the focused controls (as UnklDonald418 explained me)
3. I took into consideration that a form could have as data source tables, queries or sSQL sentences
4. The form is a big ResultSet
5. I get the row in which we are
6. In my first code (without UnklDonald418's) you were advised that nothing was selected (it doesn't work now, but for checking, in the example you can try it, is the macro named 'Repeticion', in the module 6)
7. I took into consideration that we can be in the first record, so we can't copy anything (we get an advise)
8. I selected the value in a new ResultSet made with the data source, in the previous row in which we are in the form.
9. I worked a bit supposing some fields could be Principal keys, so, if you try to copy them, you'll also get an advise. ( I took metadata from the database and also from the resultset which is the source of data)
10. If you have a form made from a Query or SQL sentence, you can gather more than one primary key, and all of them can have the same name (“ID”), so the form has 'Control.Datafield' with a specific numeration. My code detect it.
11. If the field in which you want to copy the previous record is plenty, you won't copy anything (new advise)
12. If the previous record you want to copy is empty, you won't copy anything (new advise).
13. I think it works with almost all kind of fields (data, text, etc) and I copied checkboxes.

I'm conscious about the limit of my attempts and my knowledge, and all of them need to be tested in more situations (probably you'll get the same conclusion you have now, all is nonsense.)
I have to implement listboxes and comboboxes.
I have to develop something about control tables
I have a problem with date fields in forms: to start the UnklDonald418 code I have to put the 'format control' as 'no', then it seems to work properly.
I changed the key combination, because Ctrl+X is used to cut, so, sometimes we can lose some data. Now we can work with Ctrl+', in a safer way.

The last thing: to UnklDonald418: it will be welcome any example code you would brig (to ruin it with my 'big skills', of course!)

Thanks a lot!
Attachments
CONTROL X1.7z
(40.33 KiB) Downloaded 54 times
OpenOffice 4.1.5 on Windows 10
LibreOffice 5.1 on Windows 7
LibreOffice 6.0.1 on Windows10
longi
 
Posts: 107
Joined: Mon Jul 15, 2013 5:04 pm

Re: [Solved]Disable the focus on a form

Postby RPG » Mon May 09, 2016 7:16 pm

Hello

First thank for the answer you did give. It make me not clear what the answer is. There I'm not a good database designer so I will believe I'm not smart enough to understand all what you want.

What I believe I see: You have not enough knowledge of all the form controls. You have also no knowledge of SQL. A good understanding of SQL is real important when you want make a database. SQL is a real powerful programming language. So when you want work with a database then SQL is the most important thing. Designing the tables is also real important. Good designed tables will help you.

Form and their controls.
I believe the forms and their control work like a kind of a programming tool. Special the listbox can help you. The connection of a mainform with a subform can help you to display data.

Input forms and forms for modifying data.
I'm not so good in designing forms. For that reason I make most of the time a form which I use only for input some data: as example for clients. In other forms I can change some data or add data to some long lists.

BASIC code
It is sad to see that you think you use BASIC better then some good SQL code. This makes also that I have the idea that it can only work in a small database other wise it is to slow. BASIC is only for some little parts and then the rest of the work is done by code written by real masters.

I think it is better to follow the masters on this forum or read some good books. Read also some short PDF files who explain about forms and controls. Reading the help file will help you but also reading on this forum special posts of Villeroy. I did have read a small book about SQL and this helps me a lot. I did also read several time the manual of the HSQLDB special of commands for SQL.

For your information I'm a real homeuser with only one important database of 600KB.

Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2164
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved] Disable the focus on a form

Postby UnklDonald418 » Tue May 10, 2016 7:47 pm

I have uploaded an example database which hopefully will demonstrate how the two versions of the macro work.
On the surface it would seem to be a simple programming project, but it has turned out to be much more complex than anticipated.
It is still very much a work in progress, there seems to be no end to form variations that will cause problems for it.
There is a "ReadMeFirst" form which briefly explains why there are two versions.

Back up your work frequently, because when messing with macros you are likely to crash OpenOffice, which can result in data loss. Especially when using the embedded HSQL.
Attachments
CopyFieldAboveExamples.odb
(47.08 KiB) Downloaded 59 times
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.6 & LibreOffice 6.1.5.2 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 1159
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: [Solved] Disable the focus on a form

Postby RPG » Tue May 10, 2016 11:12 pm

Hello

I have read the post of UnklDonald418. It is more clear to me why you want have it but I do not see a better solution.

When you want continue with your code then the Basetools of Benitez is maybe good to look.
http://www.baseprogramming.com/resources.html
The link is for other things of Benitez. I think the Basetools is an extension.

I think it is also good to look better to the formcontroller. I think when you understand the service of the formmodel and also the service of the formcontroller then your code can maybe more easy. In the formcontroller you can find the childs(subforms) with getbyindex.

Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2164
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved] Disable the focus on a form

Postby RPG » Wed May 11, 2016 10:41 am

Hello

@UnklDonald418
When you are working with the accessible context can you find the text in that control? The next step is then: can you modify that text? It should be nice if both thing are possible. I have the idea that moving the record pointer is not a great problem even if you don't know the control.

Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2164
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved] Disable the focus on a form

Postby UnklDonald418 » Wed May 11, 2016 7:44 pm

Hello Romke, thanks for the link to Benitez, I've not seen that before.

Actually, I've found the version of the macro that generates the dialog suites my personal needs. The version based on the Event requires modification of each new Form and even then it is not always reliable. For instance on a Form with nothing but a single grid such as the example form "TestForm01(DialogOnly)" the the Event “When receiving focus” never occurs.

As to using indexes instead of names, the difference in efficiency appears to be minimal for this program. Then there is a problem when creating the dialog. Table controls begin numbering a 0, but dialogs return 0 when the Cancel button is pressed, so my dialog would look something like “Press 1 for Table 0 --->Press 2 for Table 1 ---> Press 3 for Table 2” which looks rather confusing, so I went with names. Of course if the form was created in Design View using default names then the dialog would be “ Press 1 for Form --->Press 2 for Form ---> Press 3 for Form”. In the end I guess using names seemed less confusing.
When you are working with the accessible context can you find the text in that control?

I really don't know. I kept all that code in case I might find a need for it later, but it isn't actually used in the current version of the macro. I was looking at all the Form elements (which are numerous, if I remember correctly 600+) for “com.sun.star.accessibility.AccessibleStateType.FOCUSED” as an alternative to hasFocus(). Apparently hasFocus() returns the state of that flag, because they both seem to work in unison.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.6 & LibreOffice 6.1.5.2 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 1159
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: [Solved] Disable the focus on a form

Postby RPG » Wed May 11, 2016 9:19 pm

Hello

First thank for the answer.
UnklDonald418 wrote: When you are working with the accessible context can you find the text in that control?


I really don't know.
I suppose you can not get and modify the text. I have no idea where the problems is in your code. But I also want not look to it. The code does not work with OOo in the way I want.

Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2164
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved] Disable the focus on a form

Postby longi » Mon Jun 06, 2016 5:55 pm

Hi!
I’ve been trying things, but without really good results. However I made this code that put the focus on the table control after touching it.
Maybe it is already solved for somebody, but I’ve never found similar thing, so I thought it could be useful to UnklDonald418’s code.

Code: Select all   Expand viewCollapse view
Sub CheckFocus()
    Dim oDoc as object
    Dim fname as string
    oDoc = ThisComponent.DrawPage.Forms
    fname = FocusNameListRec(oDoc)
    'print fname
end Sub


function FocusNameListRec(oDoc as object) as string

    DIM  oForm as object, oController as object, oGridControlView as object, oGCMP as object
    Dim fName as string
    Dim i as integer, j as integer, n as integer

    oController = ThisComponent.CurrentController
    FocusNameListRec = "Not Found"
    n = oDoc.Count  'get number of elements on the form
    For i=0 to n-1    'i = element counter
      If  oDoc.hasElements()  then
          oForm  = oDoc.getByIndex(i)
          if oForm.ServiceName = "stardiv.one.form.component.Form" then
              fName = FocusNameListRec(oForm)
             ' if fName <> "Not Found" then
             '    'FocusNameListRec = fName  'get return name
             ' end if
          else
             if oForm.ServiceName = "stardiv.one.form.component.Grid"  then                                         ' If the control is a grid control
             oControlView = oController.GetControl(oForm)                                                           ' The view of the grid control
             oFormView2=thiscomponent.getcurrentcontroller.getformcontroller(oForm.Parent)                          ' The view of the Form.Parent
             '----------------------------------------------------------
             
             if not  isnull (oFormView2.Currentcontrol) then                                                       
                if oForm.implementationname = "com.sun.star.comp.forms.OGridControlModel" Then                      ' The oForm.implementationname turn on when we touch a grid control
             

                ColumnaDato= oControlView.getCurrentColumnPosition()                                                ' We get the currentColumnPosition
                ColumnaDatoNombre=oForm.getbyIndex(ColumnaDato)                                                     ' We get the name of the column
                Fila= oForm.Parent.Row                                                                              ' We get the number of row
                Valor= oForm.getByIndex(Columnadato).Boundfield.GetString                                           ' We get the String in the cell
                msgbox "We are in the cell ("& Columnadato+1 &","& Fila &") from "& oForm.name & Chr(13) & " and its content is """& valor &""""
                '----------------------------------------------------------
                ' We select the row in which we are
             
                Filan=array(Fila,Nothing)                                                                           ' Array to select the row in the grid                                                             
                oControlView.Select(Filan)                                                                          ' The selection
                '-----------------------------------------------------------
                ' We select the current position, in order to anchor the focus
                       
                oControlView.setCurrentColumnPosition(Columnadato)                                                  ' We are watching to the specific cell
                oControlView.SetFocus                                                                               ' We set the focus
                '----------------------------------------------------------
   
               End if                                                                                                ' We finish the implementionname condition
            End if                                                                                                   ' We finish the not null condiction
          End if                                                                                                     ' We finish the Grid condition
          End if                                                                                                     ' We finish the Form condition
      End if                                                                                                         ' We finish the haselements condition
   next                                                                                                              ' We finish the cycle
End function


The problem I saw is that if you have several control tables in a form the behaviour is not really clear to me. It seems to be related with the tree structure of the form, subforms and control tables.
This behaviour leads us to the first question:
If the code:

Code: Select all   Expand viewCollapse view
if not  isnull (oFormView2.Currentcontrol) then                                                       
                if oForm.implementationname = "com.sun.star.comp.forms.OGridControlModel" Then                      ' The oForm.implementationname turn on when we touch a grid control


turn on (like a light) a control table, is there any other code to turn off it?
Romke said that it is not possible, but maybe it could be other different way to get same thing.

As you can see, I'm a bit stubborn!
Thanks!
OpenOffice 4.1.5 on Windows 10
LibreOffice 5.1 on Windows 7
LibreOffice 6.0.1 on Windows10
longi
 
Posts: 107
Joined: Mon Jul 15, 2013 5:04 pm

Re: [Solved] Disable the focus on a form

Postby RPG » Mon Jun 06, 2016 10:06 pm

Hello
longi wrote:As you can see, I'm a bit stubborn!
That is not bad. It is good if you use it for the good things. I think: point it how to understand what a service can do. On a moment I did decide: I want understand all services, interfaces, methodes and properties of the form model. This learning did learn me not only how to understand the form model but also how I have to learn the other services. It did also learn me how to use the API of OOo.

Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2164
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved] Disable the focus on a form

Postby oOAddict » Sun Mar 03, 2019 4:41 pm

Hi people :D ,

Normaly, you cannot unSet the focus on a form but I'have found a trick for that 8-) . When you change the sheet, the focus is lost.

Code: Select all   Expand viewCollapse view
Function unSetFocus(nameOfSheet as string) 'pass the name of the sheet
'This function change the sheet

Dim sheet, Controller As Object

sheet= ThisComponent.Sheets.getByName(nameOfSheet)
Controller = ThisComponent.CurrentController
Controller.setActiveSheet(sheet)

end function


Try to call this function when you want to unSet the focus on a button. You need to move between 2 differents sheets like that :

Code: Select all   Expand viewCollapse view
....setfocus()
msgbox "Focus is set"

unSetFocus("OtherSheet")
unSetFocus("OriginalSheet")

msgbox "Focus is lost"


If you call the unSetFocus function one after the other, you will not see the change of sheet and it will be clean :super: .

Note : you can also link a function to the Action of a checkbox by example to lost the focus. It will be the same job.

Code: Select all   Expand viewCollapse view
function disableFocus()
unSetFocus("OtherSheet")
unSetFocus("OriginalSheet")
end function
- hoOh Addict : OpenOffice 4 - Windows
oOAddict
 
Posts: 1
Joined: Sun Mar 03, 2019 4:21 pm


Return to Base

Who is online

Users browsing this forum: Kirjava and 4 guests