Hi,
I have a listbox with data,
Code Name Car
AB01 Arief Honda
AB02 Doni daihatsu
XD01 Raisa Suzuki
XS01 Romeo mercedes
BP01 Bernard hyundai
BP02 Harly skoda
AB03 Aslam nisan
I want to show every row which is has 2 correct code from left into a label using a timer,
here is the code on the timer.action,
[code]Timer3.Enabled=true
dim o as integer
for o=data_wine.Listcount downto 0
dim d as string
d=data_wine.Cell(o,0)
d=d.Left(2)
if d =“AB” or d=“BP” then
end if
if data_wine.Cell(o,0) = d then
Label5.Text=data_wine.cell(data_wine.ListIndex,1) +" - @ "+ data_wine.cell(data_wine.ListIndex,2)
end if
data_wine.ListIndex=data_wine.ListIndex+1
next[/code]
But I got outbound exception, where are the mistakes ?
thanks
Arief
In a list box & other places the count is the number of rows, but the first row is row 0, so you have 1 row starting at 0. Change the line
for o=data_wine.Listcount downto 0
to
for o=data_wine.Listcount - 1 downto 0
and you’ll be fine.
ListCount is the number of rows in the list, but the rows start with number zero. So ListCount is always 1 higher than the maximum row index, leading to your out of bounds exception on the very first FOR loop. Try:
for o=data_wine.Listcount-1 downto 0
Just little help and a bit to give you wind in coding world to consider but please make a look in technical reference and examples which comes with Xojo mate.
You have bellow two ways of your code - Sample 1 and Sample 2 which represents 2 ways of doing your coding dilemma.
You don’t need to use ListIndex since you don’t need to move selection when your making a list of items which match to matching criteria.
Dim i as integer
Dim CurrentLine As String ' or Text vartype depnds on for which OS your doing code
Dim SubStrPart As String ' or Text vartype depnds on for which OS your doing code
Dim HaveThisCode() As String = Array("AB","BP") ' Code list for checking - foo array elements
'--------------------------------------------------------------------------------------------------------------
' Sample 1
'--------------------------------------------------------------------------------------------------------------
for i = self.Listbox1.ListCount-1 to 0 step -1
CurrentLine = Self.Listbox1.List(i).ToText
SubStrPart = CurrentLine.Left(2).Uppercase ' You can skip above line and put code from above line and do left function if you don't need complete line of current item in listbox control for later use
' If current item in listbox have code from list then
' You can also use FOR... NEXT for HaveThisCode and go over list using it rather then to use OR OR OR OR OR
If SubStrPart = HaveThisCode(0).Uppercase OR SubStrPart = HaveThisCode(1).Uppercase Then
' Your code goes here
' Current item match to ThisCode array list
End If
next i
'--------------------------------------------------------------------------------------------------------------
' Sample 2
'--------------------------------------------------------------------------------------------------------------
' Dim part from Sample 1 plus
dim n As Integer ' extra var for FOR NEXT part for HaveThisCode array
' If HaveThisCode list is set then
if HaveThisCode.Ubound > -1 then
' Go over listbox list and
for i = self.Listbox1.ListCount-1 to 0 step -1
' Get first 2 characters
SubStrPart = Self.Listbox1.List(i).Left(2).Uppercase
for n=0 to HaveThisCode.Ubound-1
' If current item in listbox have code from list then
if HaveThisCode(n).Uppercase = SubStrPart Then
' Your code goes here
' Current item match to ThisCode array list
' Exit from For N loop and move to next item in list in listbox
' in case that your checking that at least one code from HaveThisCode is in need otherwise you can put some buffer to handle that like is Array or Dictionary
Exit For
End if
Next n
Next i
End If
Thanks mr. Bogdan.,
I will give a try,
the first code that i wrote here, it running, but doesnt giving any result. Its just make slower while loading.
Thanks
Arief
For future work it’s best in your interests to define first what you want to make and focus on logic behind the graphic user interface (GUI). If you have a good logic in code which isn’t so tight to GUI then you can later implement that part to any GUI or even write that your logic in to any programming and/or scripting language or tool, tomorrow.
So, please for your self, start with looking first all examples which come with Xojo in New Project dialog and view a bit technical reference which come with Xojo or which you can look also online.
On above 2 samples code which I gave you already, so far, which are based on your post, all data are going from behind but it’s a question why you need that, to go in that direction since it’s in common to use from beginning not from the end
For i = 0 to n
vs.
For i =n to 0
Anyway making a good plan and concept of complete idea is 50% of completed project/idea.
Use simple things for start and then after them go to more complex and complex stuff
- step by step
and all the best from the new kids on the block