Question

The list Private msklistclass1 As New List(Of MaskedTextBox) contains following MaskedTextBox controls after executing the following code

 For Each ctrl As Control In Me.pnlclass11.Controls
    If TypeOf ctrl Is MaskedTextBox Then
      msklistclass1.Add(ctrl)
    End If
 Next

seat112
seat212
seat312
seat412
seat512
seat612
seat122
seat222
seat322
seat422
seat522
seat622

but they aren't in the order I have shown above I suppose. When I try to assign values to these controls in a sequential manner they don't get assigned in an order.

I tried the following code

For i = 0 To 11 Step 1
     msklistclass1(i).Text = rno312(i)
Next

The assignment I expect is

seat112  1138M0321
seat212  1138M0322
seat312  1138M0323
seat412  1138M0324
seat512  1138M0325
seat612  1138M0326

But they aren't getting assigned in this order Is there a possibility to sort the list msklistclass1


This line gives me the following output msklistclass1.Sort(Function(x, y) x.Name.CompareTo(y.Name))

seat111  1138M0321  seat121  1138M0321
seat211  1138M0323  seat221  1138M0324
seat311  1138M0325  seat321  1138M0326
seat411  1138M0326  seat421  1138M0327
seat511  1138M0328  seat521  1138M0329
seat611  1138M0330  seat621  1138M0331

but I want

seat111  1138M0321  seat121  1138M0327
seat211  1138M0322  seat221  1138M0328
seat311  1138M0323  seat321  1138M0329
seat411  1138M0324  seat421  1138M0330
seat511  1138M0325  seat521  1138M0331
seat611  1138M0326  seat621  1138M0332
Was it helpful?

Solution

Using LINQ, you can do this:

Dim listOrdered = From m In msklistclass1 Order By m.Text

Or if you cannot use LINQ or do not want to, then do this:

msklistclass1.Sort(Function(x, y) x.Name.CompareTo(y.Text))

Note: If you want to sort by a different property of the MaskedTextBox, then just change Text to whatever the property name is, like Name for instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top