Set focus on a textbox in a dialog

I want to skip from a text box to another text box in a dialog box…Does Anyone know how to do it?
The macro code for focus “oDialog1.getcontrol(name of control).setFocus” doesn´t work.

Hello,

When dealing with Focus of a control you must use the View:

  oControl = oDialog1.getControl("YOUR_CONTROL_NAME_HERE")
  oView = oControl.getView()
  oView.setFocus()
1 Like

Wonderful…thanks a lot.

Here is a rather interesting way to activate a cell in a Dialog box Grid object using the Accessibility mechanism when opening the Dialog box.

REM  *****  BASIC  *****

Private oDialog As Object
Private oGrid As Object
Private oDataModel As Object
Private oGridModel As Object
Private GridCell As Object
Private Continue As Boolean
Private done As Boolean
Private AccControl As Object 

Sub DialogShow

	DialogLibraries.LoadLibrary("Standard")	
	oDialog = CreateUnoDialog(DialogLibraries.Standard.GridDialog)

	Dim oListenerTop As Object
	oListenerTop=createUnoListener("TopListen_", "com.sun.star.awt.XTopWindowListener")
	oDialog.addTopWindowlistener(oListenerTop)
	
	Dim oSize As New com.sun.star.awt.Size, factor As Double
	Dim oCC, oComponentWindow,  oToolKitWorkArea
	Dim oWindowPosSize, oTopWindowPosSize
	oCC = ThisComponent.getCurrentController()
	oComponentWindow = oCC.ComponentWindow
	
	With oComponentWindow
        	oWindowPosSize	= .getPosSize()
        	oToolKitWorkArea = .Toolkit.WorkArea
        	oTopWindowPosSize = .Toolkit.ActiveTopWindow.getPosSize()
	End With

	With oSize
        	.Width=oDialog.Model.Width
        	.Height=oDialog.Model.Height
	End With

	factor = oSize.Width /oDialog.convertSizeToPixel(oSize, com.sun.star.util.MeasureUnit.APPFONT).Width
	
	With oDialog.Model
        	.PositionX=(factor*oTopWindowPosSize.Width - .Width) / 2
        	.PositionY=(factor*oTopWindowPosSize.Height - .Height) / 2
	End With
	
	GridInit

	Continue = True
   
	DoEvents
	Do while Continue
		Wait 20
		oDialog.setVisible(true)
		If Not done And Not IsNull(GridCell) Then
			done = True
			GridCell.grabFocus()
		End If
	Loop
	
	If done Then
		done = False : AccControl = Nothing
	End If
	oDialog.dispose()
	
End Sub

Sub TopListen_WindowClosing
	Continue=false
	ThisComponent.setModified(False)
End Sub

Sub  TopListen_windowOpened
End Sub
Sub  TopListen_windowClosed
End Sub
Sub TopListen_windowMinimized
End Sub
Sub  TopListen_windowNormalized
End Sub
Sub  TopListen_windowActivated
End Sub
Sub  TopListen_windowDeactivated
End Sub
Sub  TopListen_disposing
End Sub

Sub GridInit

	oGrid = oDialog.getControl("GridControl1")
    oDataModel = CreateUnoService("com.sun.star.awt.grid.DefaultGridDataModel")
    oGridModel = oDialog.Model.getByName("GridControl1")
    
	Dim oDoc As Object
	Dim oSheet As Object
	
	Dim oCell As Object
 	oDoc = ThisComponent
    oSheet = ThisComponent.CurrentController.getActiveSheet()
    
    Dim colcnt As Integer, rowcnt As Integer
    Dim oCursor As Object
    oCursor = oSheet.createCursor()
    oCursor.gotoEndOfUsedArea(False)
    colcnt = oCursor.RangeAddress.EndColumn
	rowcnt = oCursor.RangeAddress.EndRow

	Dim columnModel As Object
	columnModel = oGridModel.ColumnModel
	Dim columns(0 To colcnt) As Object
	
	For i = 0 to colcnt
		columns(i) =  columnModel.createColumn()
		oCell = oSheet.getCellByPosition(i, 0)
		columns(i).Title = oCell.getString()
		columnModel.addColumn(columns(i))
	Next
	
	Dim colWidth As Integer
	colWidth = oGrid.Model.Width / (colcnt + 1)
    For i = 0 To colcnt
    	columns(i).ColumnWidth = colWidth
    Next
    
    Dim rows(rowcnt) As Variant, values As String
        		
    For i = 1 To rowcnt 
    	values = ""
    	For j = 0 To colcnt
			oCell = oSheet.getCellByPosition(j, i)
    		values = values &  oCell.getString() & "¤"
    	Next j
    	rows(i) = Split(values, "¤")
		oDataModel.addRow(oDataModel.RowCount, rows(i))
    Next i
    
   oGridModel.GridDataModel = oDataModel
   
	Dim maxLen As Integer
    Dim charWidthFactor As Integer
    charWidthFactor = oSheet.getCellByPosition(0, 0).CharHeight * 0.35

	For i = 0 To colcnt
		maxLen = GetMaxTextLength(i, rowcnt)
		columns(i).ColumnWidth = maxLen * charWidthFactor
	Next i

	Dim AccGrid As Object
	AccControl = oDialog.getAccessibleContext().getAccessibleChild("Grid control")
	
	For i = 0 To AccControl.getAccessibleContext().getAccessibleChildCount() - 1
      If AccControl.getAccessibleContext().getAccessibleChild(i).getAccessibleName() = "Grid control" Then
         AccGrid = AccControl.getAccessibleContext().getAccessibleChild(i) :  Exit For
      End If
   Next i
	
	If Not IsNull(AccGrid) Then
         GridCell = AccGrid.getAccessibleContext().getAccessibleChild(0)
         'If Not IsNull(GridCell) Then
         	'MsgBox GridCell.getAccessibleRole() ' returns 59 (TABLE_CELL)
         'End If
	End If
	
End Sub

Sub GridMouseDown(oEvent)

	On Error Resume Next
	Dim row As Long : row = oGrid.CurrentRow
    Dim col As Long : col = oGrid.CurrentColumn
    oDialog.Title = oDataModel.getRowData(row)(col)
     If Err <> 0 Then Reset
     
End Sub

Sub GridKeyDown(oEvent)
	On Error Resume Next
	Dim row As Long : row = oGrid.CurrentRow
    Dim col As Long : col = oGrid.CurrentColumn
    oDialog.Title = oDataModel.getRowData(row)(col)
    If Err <> 0 Then Reset
End Sub

Function GetMaxTextLength(ByVal colIndex As Integer, ByVal maxRowIndex As Integer) As Integer

    Dim maxLength As Integer, currentLength As Integer, i As Integer
    Dim oCell As Object
    Dim oSheet As Object
    oSheet = ThisComponent.CurrentController.getActiveSheet()
    
    For i = 0 To maxRowIndex
        oCell = oSheet.getCellByPosition(colIndex, i)
        currentLength = Len(oCell.getString())
        If currentLength > maxLength Then maxLength = currentLength
    Next i
    
    GetMaxTextLength = maxLength
    
End Function

DynamicGrid.ods (20.1 KB)

Hello!
The LO developers have provided the XGridSelectionListener interface for these purposes.
In addition, the UnoControlGrid service has methods: getCurrentColumn, getCurrentRow, getColumnAtPoint, getRowAtPoint, goToCell, …

Unfortunately, XGridSelectionListener does not trigger an event if you just move from one column to another and the row remains the same, which is actually a well known bug. If you don’t believe me, be my guest and try it yourself:
DynamicGrid.ods (20.6 KB)

:slight_smile:

I think these bugs aren’t widely known (yet) :wink:

A simple demo of creating an interactive dialogbox grid.
EDIT:
Added opening the edit box also with mouse double-click
InteractiveDynamicGrid.ods (22.9 KB)


Usage:
Click GetData on the toolbar, then click ShowDialog. A dialog box containing a grid control appears.
Select any cell and then press Alt+X. A text box will appear over the selected cell with the same text as the cell text. You can edit the text, then pressing Enter will update both the data in the corresponding cell in the Calc spreadsheet and the data in the Grid object.

I haven’t commented on the code in any way because I think it ‘forces’ you to examine and think about what’s going on in the code.

It should be noted that the Grid object must be adjusted so that dynamically created columns and rows can fit inside the object. If you want to use scroll bars, the position of the edit text field must be relative to the scroll bar value.


I used Google Translate to create this text.