0

I am retrieving the available wifi networks, and I want to display them in a sorted order in a TableLayoutPanel.

They should be sorted according to their "rssi": The smalller the "rssi" value, the closer they are to the user, and the more likely it's "theirs".

However, the wifi networks arrive asynchronously, and I need to re-sort them TableLayoutPanel each time a new one arrives.

For each wifi, I create a new TableLayoutPanel with various controls in it and place it in the main TableLayoutPanel like this:

Private _tlp As New List(Of TableLayoutPanel)

Private _iCount As Integer = 0

(...)

    Dim enumResult = Await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector())
    Try
        adapter = Await WiFiAdapter.FromIdAsync(enumResult.ElementAt(0).Id)
        Await adapter.ScanAsync()
        Dim connected As Windows.Networking.Connectivity.ConnectionProfile = Await adapter.NetworkAdapter.GetConnectedProfileAsync()

        Dim report = adapter.NetworkReport

        For Each network In report.AvailableNetworks

            Dim networkView As TableLayoutPanel = New TableLayoutPanel
            'Add all controls to it
            (...)

            _tlp.Add(networkView) 'add it to my private List(Of TableLayoutPanel) so that I can keep an overview

            tlpNetworksList.Controls.Add(networkView, 0, _iCount) 'Now add the small TableLayoutPanel to the big TableLayoutPanel at the next row

            _iCount += 1

        Next

And then I re-order them manually like this:

    Me.tlpNetworksList.SetRow(_tlp(0), 2)
    Me.tlpNetworksList.SetRow(_tlp(2), 0)

Here is a screenshot so that I people see that it's worth the hassles:

enter image description here

I am looking for a way to automatically sort the TableLayoutPanels.

I have read the following here:

// When adding and removing controls, the order is not kept.
var runsOrderedByStartDate = this.nodesFlowLayoutPanel.Controls.Cast<RunNodeControl>().Select(_ => new { StartDate = _.StartDateTime, RunControl = _ }).OrderBy(_ => _.StartDate).ToList();

// Sets index of controls according to their index in the ordered collection
foreach (var anonKeyValue in runsOrderedByStartDate)
{
    this.nodesFlowLayoutPanel.Controls.SetChildIndex(anonKeyValue.RunControl, runsOrderedByStartDate.IndexOf(anonKeyValue));
}

Can somebody show me how to do this with my TableLayoutPanel?

I do not understand this code, to be honest, and I don't know where I could / would insert this.

Thank you!

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • Being a TableLayoutPanel rather than a FlowLayoutPanel in your linked question think you might be better off using GetCellPosition() and SetCellPosition() methods to control display orders. – Hursey Oct 12 '22 at 21:04
  • That said, not sure you've quite got the usage of TLP right, wondering if you're mixing FlowLayoutPanels with TableLayoutPanels. Seems like you're adding TableLayoutPanels with your controls to a TableLayoutPanel, rather than adding controls to the TableLayoutPanels Cells – Hursey Oct 12 '22 at 21:08
  • @Hursey That is correct: I am adding TLPs to a TLP. – tmighty Oct 12 '22 at 21:41
  • Try: `For Each network In report.AvailableNetworks.OrderBy(Function(x) x.NetworkRssiInDecibelMilliwatts) ... Next`... – dr.null Oct 12 '22 at 22:15
  • This does not help. The wifis arrive asynchronically over time. – tmighty Oct 12 '22 at 22:36
  • OrderBy would only work if they would arrive all at once. – tmighty Oct 12 '22 at 22:37
  • Then better to separate the tasks, cache and update a `List(Of WiFiAvailableNetwork)` and whenever it changes (add new networks, remove dead ones), repopulate the parent TLP. Sort the `List(Of WiFiAvailableNetwork)` same way – dr.null Oct 12 '22 at 22:38
  • The user might just be typing the password into one of the textboxes to connect to it. Removing the control would not be nice. – tmighty Oct 13 '22 at 00:09
  • I don't think your users will stare at this thing the whole day. Update it when it gets shown/activated/deactivated..etc. Depends on your design. – dr.null Oct 13 '22 at 05:11

0 Answers0