Ask Experts Questions for FREE Help !
Ask
    dan28's Avatar
    dan28 Posts: 17, Reputation: 1
    New Member
     
    #1

    May 14, 2009, 01:08 AM
    changing text of a listbox item to match item above
    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's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Runescape item? [ 3 Answers ]

What is that shirt that the girl in the black circle is wearing? Is it something that you can buy? http://i138.photobucket.com/albums/q248/hyerinmoon/samsshirt.gif

Add a checkbox item to a listbox at chosen position [ 11 Answers ]

How can I add the text from a checkbox into a listbox before some text already in there and after some other text? As the picture attached shows, I need to add the bakery items after "Turn on, heat" and before the other 3 texts. Any help with the times would be good as well. Thanks

What is this item? [ 1 Answers ]

My neph and I found this in the woods amongst a bunch of old garbage/cans/shoes etc. This is bugging us so much. Info. White ceramic/porcelain. It fits perfectly in a right hand. It does not sit flat on a table. The top and bottom edges of the holes are not worn at all. There is a symbol...

Macys item [ 1 Answers ]

What if some one by a blood pressure machine in macys and it stops working but you lost the receipt what can you do? -can I bring it back to them?

I need to adjust inv from mfg item [ 1 Answers ]

I run a wood shop. I buy raw lumber and put the cost in my COGS under lumber. I then cut them down and box the finished product. I need to know how to then move my boxes into inv so I know how much I have on hand. How do I write off my sawdust as a loss?


View more questions Search