VB List Index

To say that I find VB challenging is something of an understatement. It is a constant batte between what I am trying to achieve, my dwindling sanity and syntax errors.

The latest object of my frustration is the Listindex. Sure, I can populate a list box and make a selection to return the index value of the item I have selected. The problem that is defeating me completely is how to turn that number into the actual text in the listbox that it represents.

If "RED" is the second item in a listbox, how do I convert a listindex of 1 to "RED".

I have been hacking through the help files in Opus and the VB sites on the web for hours now. Could some nice VB guru put me out of my misery and restire some equilibrium to my dwindling sanity?

Control.GetItemAt.name

Thanks Jon but I am still struggling ...

I want to load the the chosen item into a variable called dopple. But:

dopple = Control("listbox1").GetItemAt.name

gives me an error. What anI doing wrong?

Slow down and think about it. You know you want the name of item 1. Where is the item number specified in dopple = Control("listbox1").GetItemAt.name ? It's not. Clearly, the item number needs to be provided somewhere. The sensible thing to do would be to look at the docs and work out where it should go.

I have hacked through the help files until I am blue in the face, before I asked for help. As my old granny used to say: God helps those who help themselves, and, believe me, I always try self-help; help files, the web etc before I bother anyone.

As I have said before I am no programmer. I can see the logic of why I should have to supply the item number, but it is the syntax that always defeats me and where I require help.

Faced with a really nice project failing on my ignorance and syntax errors is extremely frustrating. Hence the plea for help that evn a numpty like me can understand

dlg.Control("listbox1") returns a Control object (assuming that dlg is the name of your Dialog object).

Assuming that part works, you then want to ask that Control to give you the item at position 1.

GetItemAt can do that, but you have to tell it which position you want. It cannot magically know you want position 1. You have to tell it you want position 1.

Let's look at the documentation for the Control object where we see this description of the GetItemAt method:

The GetItemAt method requires an argument, the position of the item you want to get:

This makes sense, since GetItemAt needs to know which item you want it to get, and you need some way to tell it.

Let's look at your line of code:

dopple = dlg.Control("listbox1").GetItemAt.name

How many arguments is it passing to GetItemAt?

Zero. It isn't passing any arguments to GetItemAt.

But GetItemAt needs one argument. So we need to call it with one argument. So now we have this puzzle:

dopple = dlg.Control("listbox1").GetItemAt( ??? ).name

What could go into the ??? to solve this puzzle?

The one argument that GetItemAt takes is called "position" and is an integer (i.e. a number).

Which item do you want to get? The Item at position 1.

So you need to pass 1 as the argument to GetItemAt to tell it you want the item at position 1.

dopple = dlg.Control("listbox1").GetItemAt(1).name

Leo, many thanks for your detailed explanation. I had actually gor that far after I received John's message. The reason I wrote again was that it does not work and I could not understand why,

The line of code
dopple = dlg.Control("listbox1").GetItemAt(1).name

Error at line 62, position 8
A method was called unexpectedly (0x8000ffff)

This is the full development code I am using:

Option Explicit
'Function OnClick(ByRef clickData)
Dim List1
	Dim folderItem
	Dim folderEnum
	Dim tag
	Dim tagString
	dim dopple
	Dim dlg
	Dim msg
	dim clipstring
	dim selecteditem
	dim exto
	dim pathbo
	dim folderdir
	dim totchar
	dim whichform
	dim sider
	dim bottomer
	dim piccydata
Function OnClick(ByRef clickData)
   If clickData.func.sourcetab.selected_files.count = 0 Then
      Msgbox "Nothing is selected",16, "NO FILE SELECTED"
	  'Unload.Me
	  End If
ClipString = ""
   For Each SelectedItem In ClickData.Func.sourcetab.Selected
         ClipString = SelectedItem.name_stem
		 exto = SelectedItem.ext
		 pathbo = selectedItem.path & "\" & ClipString & exto
   Next
   'Now discover if the picture is landscape or portrait to call the right user form
   For Each selecteditem in clickData.func.sourcetab.selected
      Set piccyData = selectedItem.Metadata.image
sider = piccydata.picheight
bottomer=  piccydata.picwidth
if sider > bottomer Then
      whichform = "dirbo"
	  Else whichform = "dirbohor"
	  End If
Next
	Set dlg = clickData.func.dlg
	dlg.template = whichform
	dlg.detach = true
	dlg.Show

	Set folderEnum = DOpus.FSUtil.ReadDir("E:\Imaging\Jpeg Images Store", False)
	Do While (Not folderEnum.Complete)
		Set folderItem = folderEnum.Next
		totchar = Len(folderitem)
	folderdir = Right(folderitem,(totchar-29))
		'DOpus.Output folderItem
		dlg.Control("listbox1").AddItem(folderdir)
		dlg.control("piccy2").label = pathbo

	Loop

	Do       
		Set Msg = Dlg.GetMsg()    
	Loop While Msg
		'dopple = dlg.control("listbox1").value
       dopple = dlg.Control("listbox1").GetItemAt(1).name
'
		msgbox dopple

			
End Function

It has me confused because the line above the code you gave me,which successfully gives me the list index to plug into GetItemAt, works successfully. Obviously I am doing something else wrong. For the sake of mysanity I would love to know what.

Once the message loop exits the dialog has been destroyed and the list no longer exists.

Got it! Many thanks to youboth for your patience and help.