I want to temporarily modify the width of a column and then reset it using the ‘when receiving focus’ and “when losing focus” events. I want to use the same macro for all columns (and possibly other controls). How can my macro know what object called it?
Hello,
You tagged this question as base
which is the database module. What columns are you talking about?
.
Use oEvent:
Sub get_something(oEvent)
control_name = oEvent.Source.Model.Name
The columns in a table control, and also list box controls.
Install the MRI extension.
Link your event to MRILib.Module1.Mri
Find out all these things by yourself.
@DonThompson
Have shown you method to get name of control where macro was called from. But based upon what you seem to want you may have many other issues.
.
For example, you have same sub for focus received/lost. This can put you in a loop. Focus received fired to show message creates a focus lost. Close the message and you may be back to message received. There are many other pitfalls here.
.
To check for focus you need the View of the control. Here for say a ListBox:
Rem oLBctrl = GET_YOUR_LISTBOX_CONTROL_HERE
Rem Get VIEW for listbox
CtlView = ThisComponent.CurrentController.GetControl(oLBctrl)
.
Other controls similar.
Then you can check CtlView for hasFocus()
- boolean
.
I would not go this route. At best, separate Focus received/lost. I see many issues here.
Sorry, I was unclear. I do not want to use the same sub for focus received/lost just one sub for got focus and another for lost focus with each applying changes to the control which called them at the time.
Agree with Ratslinger that attaching a macro to an event can lead to endless loops. Fiddled around a bit and ended up with
sub length(oEvent)
dim oDoc,oForm,oDocCtl, oControlView,oGrid as object
dim iColumn as integer
oDoc = thiscomponent
oForm = oDoc.getDrawPage().getForms().getByName("MainForm")
oDocCtl = oDoc.getCurrentController()
oGrid = oForm.getByName("SubForm").getByName("SubForm_Grid")
oControlView = oDocCtl.getControl(oGrid)
iColumn = oControlview.getCurrentColumnPosition
if iColumn = 3 then REM column count starts with 0
oControlView.getModel().getByIndex(3).setPropertyValue("Width",300)
else
oControlView.getModel().getByIndex(3).setPropertyValue("Width",100)
endif`
Attached this macro to the event table control - mouse pressed
That works in case of clicking with the mouse in the column, other trigger selections (approaches) are welcomed in case of navigating in the table control with f.i. the TAB key
This is for a mainform including a coupled subform, inspect mri(oDoc.getDrawPage().getForms()) for your case