How to scroll up to the combo box by using macro?

image
.
.
I enter a number on combo box, if the number exists, I hit ENTER, The macro colours the cell background yellow. After that, the macro sets focus on the combo box to me enter a new number.
.
.
The problem is…the macro sets the focus on the combo box, but the macro doesn’t move to the combo box (as shown the image below).

image
.
.
The question is…How do I code a macro in order to move (scroll up, in this case) to the combo box?

@Jonjozz
I seem to be missing something. I provided a sample in your previous post demoing that your code worked. Just retested selecting a cell down some rows so the combo box was out of sight. When the macro was executed to set focus, the combo box was moved to.
.
Something else you are doing that may override this?

I don’t quite understand what you want. Perhaps you should select a value from the combox list by making it current.

Sub Sheet_OnChange(oTarget As Object)

  ' oRange is your work range.
  If oRange.queryIntersection(oTarget.RangeAddress).Count = 0 Then
    Exit Sub
  End If

  oForm = oSheet.DrawPage.Forms(0)
  cbo = oForm.getByName("cboItems")
  With cbo
        .Text = oTarget.String
  End With

End Sub

After the macro coloured the cell background yellow, the focus on combo box doesn’t move cursor to the combo box…if the entered number is far from combo box, I have to scroll up to the combo box, to click on it and to enter a new number. I want that macro sets focus and moves to the combo box.

oController = ThisComponent.getCurrentController()
oForm = oSheet.getDrawpage().getForms().getByIndex(0)
oControl = oForm.getByName(“cbCobranca”)
oControlView = oController.getControl(oControl)
oControlView.setFocus()
oControlView.Text=""

the commands above set focus, but they don’t move to the combo box

No scrolling. Freeze a couple of rows where your combobox is located.

It´s a big flaw…The setFocus command should move to the combo box…Why would I want to set focus, but not to move to the focused control? It doesn’t make sense.

Don’t you want to keep it simple? Use the Data Validation drop-down list. Press Alt+DownArrow to drop down the list. And instead of focusing, just select in macro the cell with the drop-down list. It also works to search for a value in a list when you enter text character by character.

1 Like

I agree with @eeigor. But what about doing both. Use the CurrentController to select the cell behind the combo box and then set focus on the combo box. Best of both worlds?

Sub Focus()
	Dim Sheet As Object
	Dim Form As Object
	Dim ComboBox As Object
	Dim Cell As Object
	Dim Controller As Object
	Dim ControlView As Object
	
	Sheet = ThisComponent.getSheets().getByName("Sheet1")
	Form = Sheet.getDrawpage().getForms().getByIndex(0)
	Controller = ThisComponent.getCurrentController()

	Cell = Sheet.getCellByPosition(2,2) 'Position of ComboBox
	Controller.select(Cell)

	CombBox = Form.getByName("ComboBox1")
	ControlView = Controller.getControl(CombBox)
	ControlView.setFocus()
	ControlView.Text=""

End Sub

Return to ComboBox.ods (20.8 KB)

@joshua4
Have you seen my comment? My sample works with just set focus.

1 Like

You´re the MAN…Fantastic…Simple and wonderful.
Your solution is brilliant in its simplicity!!!
Thanks a trillion.

@Ratslinger, I just put the OP’s ideas, your code and @eeigor’s idea together because it seems like the OP’s environment just wasn’t moving to the combo box. That’s what I meant by

It’s a hack, I guess. I apologize that I only referenced @eeigor…your code seemed to be the “line of conversation” in general, so I didn’t think to @ you.

Not necessary. What concerns me is that the sample I posted does just what the OP wanted (it was their code to begin with) but still claimed it wasn’t working. So where is the actual problem?