0

I'm still a student without much experience using vb.net and I am having some trouble splitting a string within an array into 2 values. For example in my textbox I have several lines of measurements that are Length x Width: 20x14, 10x8, 16x13. Each measurement is on its own line. I'm trying to split all Width values that are greater than 12 into 2 separate measurements, so with that last example, I would have 5 measurements (LxW): 20x12, 20x2, 10x8, 16x12, 16x1, then I would like to add these measurements to a new textbox with each measurement on its own line.

Here is the code I have so far. Again, I am very new to programming and this is the first serious project for me since "Hello World", so what I have might be way off. Thanks in advance.

    Dim room As String = RoomsTextBox.Text
    If room.EndsWith(vbCrLf) Then room = room.Substring(0, room.Length - vbCrLf.Length)
    Dim roomarray() As String = room.Split(vbCrLf)
    Dim Cuts(roomarray.Length - 1, 0) As String
    RoomsTextBox.Select(0, 0)

    Dim CutLength As Integer
    Dim CutWidth As Integer
    Dim i As Integer
    Dim j As Integer
    CutsTextBox.Select()

    Cuts(i, j) = (Val(roomarray(i).Split("x")(0))) & Val(roomarray(j).Split("x")(1))
    For i = 0 To Cuts.GetUpperBound(0)
        For j = 0 To Cuts.GetUpperBound(1)
            Cuts(i, j) = 0
        Next
        If Val(roomarray(i)) > 12 Then
            CutWidth = Val(roomarray(i)) - 12
            CutLength = Val(roomarray(j))

        Else
            CutWidth = Val(roomarray(i))
            CutLength = Val(roomarray(j))
        End If
        Dim inserttext = CutsTextBox.Text
        Dim insertposition As Integer = CutsTextBox.SelectionStart
        CutsTextBox.Text = CutsTextBox.Text.Insert(0, CutLength.ToString & "x" & _  
        CutWidth.ToString)
        CutsTextBox.SelectionStart = insertposition + inserttext.Length
    Next i

I even tried it with inserting the measurements into a ListBox. Here is the code for that:

    Dim room As String = RoomsTextBox.Text
    Dim roomarray() As String = room.Split(vbCrLf)
    Dim Cuts(roomarray.Length - 1, 0) As String

    Dim CutLength As Integer
    Dim CutWidth As Integer
    Dim i As Integer
    Dim j As Integer
    CutsTextBox.Select()

    Cuts(i, j) = (Val(roomarray(i).Split("x")(0))) & Val(roomarray(j).Split("x")(1))
    For i = 0 To Cuts.GetUpperBound(0)
        For j = 0 To Cuts.GetUpperBound(1)
            Cuts(i, j) = 0
        Next
        If Val(roomarray(i)) > 12 Then
            CutWidth = Val(roomarray(i)) - 12
            CutLength = Val(roomarray(j))

        Else
            CutWidth = Val(roomarray(i))
            CutLength = Val(roomarray(j))
        End If
        ListBox1.Items.Add(CutLength.ToString & "x" & CutWidth.ToString)
    Next i
1
  • So do you have your 5 new measurements, and you are just stuck on the adding them to a textbox on their own line, or are you stuck getting the measurements? Commented Apr 27, 2011 at 22:46

1 Answer 1

1

Try this out.

Dim dimensions As String() = txtInput.Text.Split(vbCrLf)
    Dim final As New List(Of String)

    For Each item In dimensions
        Dim lw As String() = item.Split("x")

        Dim length As String = lw(0)
        Dim width As Integer = CInt(lw(1))

        If width > 12 Then
            Dim new1 As String
            Dim new2 As String

            new1 = length & "x" & (width - 12).ToString
            new2 = length & "x12"

            final.Add(new1)
            final.Add(new2)

        Else
            final.Add(item)
        End If

    Next

    For Each item In final
        txtOutPut.Text += item & vbCrLf
    Next
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jack, that did exactly what I wanted. I really appreciate the help.
Sorry just now marked it as the accepted answer, I'm new to this site and wasn't sure what that meant. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.