PDA

View Full Version : Changing text of a listbox item to match item above


dan28
May 14, 2009, 01:08 AM
I need this code to be able to take the time out of an item in a list box and replace the time of the item under. The code then needs to take the time in brackets at the end of the item and add it to the end of that time also.

e.g.
this program is meant to be used for a bakery to tell them what need to be baked and when
so the list box looks like

09:00 turn on oven (15)
09:15 cheese rolls (18)
09:33 knot rolls (15)
Etc
Etc


Feel free to write a new code if u want
Cause that one is confusing





Dim strCurrentString As String
Dim strLineTime As String
Dim intDuration As Integer

Dim intLastItem As Integer = lbxOven1.Items.Count - 1
Dim strHours As String
Dim strNewTimeMinutes As String
Dim strLastMinutes As String
Dim strLastHours As String
Dim strEndBit As String
Dim strNewString, strMinutes As String


'loop counts through all items in listbox
For I = 0 To lbxOven1.Items.Count - 1
If Not intLastItem = I Then

If I = 0 Then

'sets current string = to the curent item being worked on in loop
strCurrentString = lbxOven1.Items(I).ToString

'takes the time of the current item (I)
strLineTime = GetStartTime(strCurrentString)

'takes the text of current item (I)
strEndBit = strCurrentString.Substring(strCurrentString.IndexO f(" "))

'makes sure loop doesn't try to process last item


'takes the value of baking time between brackets
intDuration = GetDurationMinutes(strCurrentString)

'takes the minutes part of current time
strMinutes = GetMinutes(strLineTime)

' MessageBox.Show("strCurrenString=" & strCurrentString & " strMinutes=" & strMinutes & " strLineTime=" & strLineTime)


'takes hours part of current time
strHours = GetHours(strLineTime)

'stores value of time in minutes of current to be called on for next loop
strLastMinutes = strMinutes

' stores time in hrs of current item to be called on in next loop
strLastHours = strHours

'adds duration of baking onto minutes of current time


'sets current string = to the curent item being worked on in loop
Else : strCurrentString = lbxOven1.Items(I).ToString

'takes the time of the current item (I)
strLineTime = GetStartTime(strCurrentString)

'takes the text of current item (I)
strEndBit = strCurrentString.Substring(strCurrentString.IndexO f(" "))

'makes sure loop doesn't try to process last item
If Not intLastItem = I Then

'takes the value of baking time between brackets
intDuration = GetDurationMinutes(strCurrentString)

'takes the minutes part of current time
strMinutes = GetMinutes(strLineTime)

'MessageBox.Show("strCurrenString=" & strCurrentString & " strMinutes=" & strMinutes & " strLineTime=" & strLineTime)


'takes hours part of current time
strHours = GetHours(strLineTime)

'stores value of time in minutes of current to be called on for next loop
strLastMinutes = strMinutes

' stores time in hrs of current item to be called on in next loop
strLastHours = strHours

'adds duration of baking onto minutes of current time

strNewTimeMinutes = NewTimeMinutes(CStr(intDuration), strLastMinutes)

' MessageBox.Show(strNewTimeMinutes)


'adds previous hours with : and newMinutes and text
strNewString = strLastHours & ":" & strNewTimeMinutes & strEndBit

'removes old time
lbxOven1.Items.RemoveAt(I)
'inserts new time
lbxOven1.Items.Insert(I, strNewString)


End If

End If
End If



Private Function GetStartTime(ByVal strLine As String) As String

Dim strResult As String = ""

Dim intSpace As Integer = strLine.IndexOf("

Dim intSpace As Integer = strLine.IndexOf(")
strResult = strLine.Substring(0, intSpace)

Return strResult

End Function

Private Function GetDurationMinutes(ByVal strLine As String) As Integer

Dim strResult As String = "")
strResult = strLine.Substring(0, intSpace)

Return strResult

End Function

Private Function GetDurationMinutes(ByVal strLine As String) As Integer

Dim strResult As String = "("

Dim intFirstBracket As Integer = strLine.IndexOf(")")
Dim intLastBracket As Integer = strLine.IndexOf(""

Dim intColons As Integer = strLine.IndexOf(")
strResult = strLine.Substring(intFirstBracket + 1, intLastBracket - intFirstBracket - 1)
Return CInt(strResult)

End Function
Private Function GetMinutes(ByVal strLine As String) As String

Dim strResult As String = ")
strResult = strLine.Substring(intColons + 1, 2)
Return (strResult)

End Function
Private Function GetHours(ByVal strLine As String) As String

Dim strResult As String = ""

Dim intColons As Integer = strLine.IndexOf("

Dim intColons As Integer = strLine.IndexOf(")
strResult = strLine.Substring(0, intColons)
Return (strResult)

End Function
Private Function NewTimeMinutes(ByVal strDuration As String, ByVal strMinutes As String) As String

Dim strResult As String = "")
strResult = strLine.Substring(intColons + 1, 2)
Return (strResult)

End Function
Private Function GetHours(ByVal strLine As String) As String

Dim strResult As String = ""

strResult = CStr(CInt(strHours))
Return (strResult)

End Function

Perito
May 14, 2009, 01:18 PM
I need this code to be able to take the time out of an item in a list box and replace the time of the item under. The code then needs to take the time in brackets at the end of the item and add it to the end of that time also.


I don't really understand that.

If you have a line, say in this format: "hh:mm Sometext", you can parse it. You usually parse it depending on what's remains constant. Here's an example:

Dim TimeStr As String
Dim TextStr As String
Dim FullStr As String = "hh:mm Sometext"

' let's say we use the first space as the separator. This will work, but only
' if the first space is always the separator between the date
' and the rest of the string.

Dim P As Integer = FullStr.Indexof(" ") ' P is the position of the first space.
TimeStr = FullStr.SubString(0, P) ' P is used as the "count" here. This will copy
' up to, but not including, the first space.
Dim L = FullStr.Length - P
TextStr = FullStr.Substring(P, L)


TimeStr now contains "hh:mm" and TextStr contains "Sometext".

If you wish to replace the time in the listbox, you can rebuild the string

TimeStr = NewTime.ToString
FullStr = TimeStr & " " & TextStr

Then put it back in the listbox at the index you wish it to be.

Listbox1.Items.Item(index) = FullStr ' now modified.