I have what I thought was a simple comparison test of text in a Listbox cell with the Listbox’s listindex, but I keep getting the error “Too many arguments: Expected 1 but got 0.”
The list is compared externally to the control, from another control. I’ve tried several variations, but nothing works. Ideas?
[code] Dim iC As Integer = Listbox.ListCount-1
Dim i As Integer
Dim iL As Integer = Listbox.ListIndex
For i = 0 to iC
If Listbox(i).Text= Listbox.ListIndex.Text Then
MsgBox “Your item is already in the List. Please Delete It.”
Exit For
ElseIf i = Listbox.ListIndex Then
End If
If i = iC And Listbox(i).Text <> Listbox(iL).Text Then inList = True
You are also trying to access the superclass Listbox, you need to use the instance names.
Try this
Function IsDuplicateItem() as Boolean
dim iListboxMaximum as Integer = lbListboxInstance.ListCount - 1
for i as Integer = 0 to iListboxMaximum
if lbListboxInstance(i).Text = lbListboxInstance.Cell(lbListboxInstance.ListIndex, 0) then
MsgBox("Duplicate Item" + EndOfLine + EndOfLine + _
"This item is already in the lbListboxInstance. Please delete it to continue.")
return true
end
next
End Function
Sorry about using listbox and not listbox1. I was trying to make it generic.
Sorry I’m on my phone so please understand my quoting.
You have 2 pieces in the line that I don’t understand.
.cell … It is on one side and not the other. Why?
(lbListboxInstance.ListIndex, 0). Again. Why this side and not the other? It is granted it only has 1 column, but still…
Function IsDuplicateItem(lb as listbox) as Boolean
dim iListboxMaximum as Integer = lb.ListCount - 1
for i as Integer = 0 to iListboxMaximum
if lb(i).Text = lb.Cell(lbListboxInstance.ListIndex, 0) and i<>lb.listindex then
MsgBox("Duplicate Item" + EndOfLine + EndOfLine + _
"This item is already in the Listbox. Please delete it to continue.")
return true
end
next
End Function
lb is ANY listbox in you app, passed to the function
lb(i).text is the text value of that row … see LangRef
added check of i<>lb.listindex so it doesn’t think itself is a duplicate
Ah, that’s okay, I just thought it might have been part of the problem.
When seeking help it’s best to post your actual code, you might inadvertently change something when you clean it up!
So Cell gives you the text for the specific cell, and now that you’ve asked that question I see that code wouldn’t work either. Text basically gives you the value for Cell(ListIndex, 0)
Let me try to rewrite that to something that will work.
Public Function HasValue(lbHaystack as Listbox, sNeedle as String) as Boolean
dim iLBMax as Integer = lbHaystack.ListCount - 1
for i as Integer = 0 to iLBMax
if lbHaystack.Cell(i, 0) = sNeedle then
return true
end
next
End Function
This you would use before adding the item, so:
if HasValue(lbMyListboxInstance, "Search Value") = true then
MsgBox("Duplicate!")
end
This code is also untested and unchecked, but I am much more confident in it as I wrote it from scratch instead of trying to change what you’d already posted
Thanks. For some reason I hadn’t tried that particular combination. Thanks Tim. It works.
Mine was missing the Cell(Listbox1.ListIndex, 0).
I thought the Cell referred to the value, but I didn’t have the other part - the other dimension.
[code]Dim iC As Integer = Listbox1.ListCount-1
Dim i As Integer
For i = 0 to iC
If Listbox1.Cell(i, 0) = Listbox1.Cell(Listbox1.ListIndex, 0) And i <> Listbox1.ListIndex Then
inList = True
MsgBox “Your text is already in the List. Please Delete It.”
Exit For
End If
If i = iC And Listbox1.Cell(i, 0) = Listbox1.Cell(Listbox1.ListIndex, 0) Then inList = True