Need the text from a single ListBox selection assigned to a string variable

I’m attempting to assign the text from a Base form ListBox single selection to a string variable. Using the following code gives me a ‘Property or Method not found’ error.

Sub GetLBtext

Dim oForm As Object
Dim oField As Object
Dim var As String

oForm= ThisComponent.Drawpage.Forms.getByName("Form Name With ListBox_1")
oField = oForm.getByName("ListBox_1").getSelectedItem()
var = oField

End Sub

Upon some research, I came accross this discussion [Solved] Pass ListBox selected item to Textfield (View topic) • Apache OpenOffice Community Forum at the Open Office Forum that I’m pretty sure contains the answer I’m looking for, but its content is way over my head. Any chance I could get someone out there to explain to me how to properly go about this? As you may have guessed I’m a total LO Basic noob, so please be gentle :slight_smile:

You’re close. Try:

oField = oForm.getByName("ListBox_1")
var = oField.SelectedValue

Right as usual! Thanks, Ratslinger

FYI - try better naming conventions. ‘var’ is not good. You will have no idea what that is six months from now. ‘oField’ is an object ( thus the ‘o’). ‘sLB1Selection’ is a string (thus the ‘s’). When you use that later in the code you have a better idea what it is. Be descriptive. Imagine variables names String1, String2, String3, etc. No idea what they are.

True enough, and thanks for the tip. I just used variable names that described what they represented in the code snippet I posted. My real code has better names and is properly commented. Nice to know you’ve got my back…