SmashedPotato
SmashedPotato

Reputation: 75

how to compare (sort) values of dynamically created textboxes?

i'm currently doing a program that aims to sort values of dynamically created textboxes. how to do this in vb.net? This is my code for creating the textboxes:

For cnt = 0 To Val(TextBox1.Text) - 1
       arrivalbox.Add(New TextBox)
             With arrivalbox(cnt)
                .Parent = Me
                .Left = 0
                .Height = 13
                .Width = 65
                .Top = .Height * cnt + 50
                .Visible = True
                .Tag = cnt
                .Text = ""
                .Name = "arrival" & cnt
                .Location = New Point(380, 120 + (cnt * 25))
                .TextAlign = HorizontalAlignment.Center
                .Enabled = False
                Me.Controls.Add(New TextBox)
End With

Upvotes: 0

Views: 253

Answers (1)

TylerDurden
TylerDurden

Reputation: 1670

Something like this will loop through all the textboxes and add them to a sortedlist which will sort all the textboxes according to the values.

I'm not sure what you are trying to achieve here. But im guessing once you get these sorted then you can change the position based on their position in the sorted list

Dim listedboxes As SortedList(Of Double, TextBox)
For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                  listedboxes.add(ctrl.value,ctrl)
            End if
Next

EDIT maybe something like this.

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim listedboxes As New SortedList(Of Double, TextBox)
    For i As Integer = 0 To TextBox1.Text.Length - 1
        Dim tb As New TextBox
        With tb
            .Parent = Me
            .Left = 0
            .Height = 13
            .Width = 65
            .Top = .Height * i + 50
            .Visible = True
            .Tag = i
            .Text = TextBox1.Text.Substring(i, 1)
            .Name = "arrival" & i
            .TextAlign = HorizontalAlignment.Center
            .Enabled = False
            Me.Controls.Add(New TextBox)
        End With
        listedboxes.Add(TextBox1.Text.Substring(i, 1), tb)
    Next

    Dim j = 0
    For Each kvp As KeyValuePair(Of Double, TextBox) In listedboxes
        kvp.Value.Location = New Point(380, 120 + (j * 25))
        j += 1
    Next
End Sub

enter image description here

Upvotes: 1

Related Questions