none
After Sort the List RRS feed

  • Question

  • Hi All,

    I have to lists F_ as integer and C_ as string.Put List F_ in class "A" and List C_ in class "B".Then sorted by item "B".

    MY question is How to reuse back item.A and item.B in Form2.

    Option Strict On
    Option Explicit On
    
    Imports System.IO
    Imports System.Data
    Imports System.Text
    
    Public Class Form1
        Private F_ As New List(Of Integer)
        Private C_ As New List(Of String)
    
    
        Dim TM_ As New List(Of String)
        Dim Items_ As New List(Of I_Sort)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            F_.Add(99)
            F_.Add(99)
            F_.Add(78)
            F_.Add(55)
            F_.Add(92)
            F_.Add(92)
            F_.Add(76)
            F_.Add(17)
            F_.Add(53)
            F_.Add(31)
    
            C_.Add("101")
            C_.Add("203")
            C_.Add("665")
            C_.Add("205")
            C_.Add("321")
            C_.Add("442")
            C_.Add("505")
            C_.Add("665")
            C_.Add("663")
            C_.Add("321")
    
            For I As Integer = 0 To C_.Count - 1
                TextBox1.AppendText(C_(I) & "   " & F_(I) & vbCrLf)
                Items_.Add(New I_Sort With {.A = C_(I), .B = F_(I)})
    
            Next
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim SortedItems As IEnumerable(Of I_Sort) = Items_.OrderBy(Function(item) item.B)
            SortedItems.ToList.ForEach(Sub(item) TextBox2.AppendText(item.A & "  =" & item.B & vbCrLf))
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Form2.Show()
        End Sub
    End Class
    
    Public Class I_Sort
        Public A As String
        Public B As Integer
    
        Public Overrides Function ToString() As String
            Return A.ToString & " : " & B.ToString
        End Function
    
    End Class
    
    
    
    Public Class Form2
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For i As Integer = 0 To Form1.TextBox2.Text.Count - 1
                Me.TextBox1.AppendText(Form1.TextBox2.Text(i).ToString)
            Next
    
    
    
        End Sub
    
    End Class

    thank.

    Sunday, April 21, 2019 6:38 AM

Answers

  • Thank you leshay for your feedback.

    I want find a maxima number at Form2.It may be look like this,

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim Items As New List(Of Item_Max)
            Items.Add(New Item_Max With {.HA_ = Item.A, .IA_ = Item.B)})
    
    
            Dim Max As Integer = 0
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ > Max Then Max = Items(J).HA_
            Next
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ = Max Then
                    TextBox1.AppendText(Items(J).ToString & vbCrLf)
                End If
            Next
    
    
    
        End Sub
    End Class
    
    Public Class Item_Max
        Public HA_ As String
        Public IA_ As Integer
    
        Public Overrides Function ToString() As String
            Return HA_.ToString & " : " & IA_.ToString
        End Function
    
    End Class

    Hi,

    try to fix code:

    Public Class Form1
        Private F_ As New List(Of Integer)
        Private C_ As New List(Of String)
    
        Dim TM_ As New List(Of String)
        Public Items_ As New List(Of I_Sort)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            F_.Add(99)
            F_.Add(99)
            F_.Add(78)
            F_.Add(55)
            F_.Add(92)
            F_.Add(92)
            F_.Add(76)
            F_.Add(17)
            F_.Add(53)
            F_.Add(31)
    
            C_.Add("101")
            C_.Add("203")
            C_.Add("665")
            C_.Add("205")
            C_.Add("321")
            C_.Add("442")
            C_.Add("505")
            C_.Add("665")
            C_.Add("663")
            C_.Add("321")
    
            For I As Integer = 0 To C_.Count - 1
                TextBox1.AppendText(C_(I) & "   " & F_(I) & vbCrLf)
                Items_.Add(New I_Sort With {.A = C_(I), .B = F_(I)})
    
            Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim SortedItems As IEnumerable(Of I_Sort) = Items_.OrderBy(Function(item) item.B)
            SortedItems.ToList.ForEach(Sub(item) TextBox2.AppendText(item.A & "  =" & item.B & vbCrLf))
        End Sub
    
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Form2.Show()
    
        End Sub
    
        Public Class I_Sort
            Public A As String
            Public B As Integer
    
            Public Overrides Function ToString() As String
                Return A.ToString & " : " & B.ToString
            End Function
    
        End Class
    
    
    
    End Class
    

    Form2:

    Public Class Form2
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'For i As Integer = 0 To Form1.TextBox2.Text.Count - 1
            '    Me.TextBox1.AppendText(Form1.TextBox2.Text(i).ToString)
            'Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim Items As New List(Of Item_Max)
            For Each t In Form1.Items_
                Items.Add(New Item_Max With {.HA_ = t.A, .IA_ = t.B})
            Next
    
    
            Dim Max As Integer = 0
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ > Max Then Max = Items(J).HA_
            Next
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ = Max Then
                    TextBox1.AppendText(Items(J).ToString & vbCrLf)
                End If
            Next
    
        End Sub
        Public Class Item_Max
            Public HA_ As String
            Public IA_ As Integer
    
            Public Overrides Function ToString() As String
                Return HA_.ToString & " : " & IA_.ToString
            End Function
    
        End Class
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mipakteh Wednesday, April 24, 2019 2:20 PM
    Wednesday, April 24, 2019 5:06 AM

All replies

  • Hi

    OK, I tried your code and found the sorting works fine, the setting of TextBox contents works fine, the Buttons work fine.

    *

    How to reuse back item.A and item.B in Form2 means nothing to me at all. What are you trying to do? Maybe show examples before/after of item.A and item.B - is it something you want to do on another Button click?


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, April 21, 2019 1:19 PM
    Sunday, April 21, 2019 1:19 PM
  • Thank you leshay for your feedback.

    I want find a maxima number at Form2.It may be look like this,

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim Items As New List(Of Item_Max)
            Items.Add(New Item_Max With {.HA_ = Item.A, .IA_ = Item.B)})
    
    
            Dim Max As Integer = 0
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ > Max Then Max = Items(J).HA_
            Next
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ = Max Then
                    TextBox1.AppendText(Items(J).ToString & vbCrLf)
                End If
            Next
    
    
    
        End Sub
    End Class
    
    Public Class Item_Max
        Public HA_ As String
        Public IA_ As Integer
    
        Public Overrides Function ToString() As String
            Return HA_.ToString & " : " & IA_.ToString
        End Function
    
    End Class

    Sunday, April 21, 2019 2:47 PM
  • Thank you leshay for your feedback.

    I want find a maxima number at Form2.It may be look like this,

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim Items As New List(Of Item_Max)
            Items.Add(New Item_Max With {.HA_ = Item.A, .IA_ = Item.B)})
    
    
            Dim Max As Integer = 0
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ > Max Then Max = Items(J).HA_
            Next
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ = Max Then
                    TextBox1.AppendText(Items(J).ToString & vbCrLf)
                End If
            Next
    
    
    
        End Sub
    End Class
    
    Public Class Item_Max
        Public HA_ As String
        Public IA_ As Integer
    
        Public Overrides Function ToString() As String
            Return HA_.ToString & " : " & IA_.ToString
        End Function
    
    End Class

    Hi,

    try to fix code:

    Public Class Form1
        Private F_ As New List(Of Integer)
        Private C_ As New List(Of String)
    
        Dim TM_ As New List(Of String)
        Public Items_ As New List(Of I_Sort)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            F_.Add(99)
            F_.Add(99)
            F_.Add(78)
            F_.Add(55)
            F_.Add(92)
            F_.Add(92)
            F_.Add(76)
            F_.Add(17)
            F_.Add(53)
            F_.Add(31)
    
            C_.Add("101")
            C_.Add("203")
            C_.Add("665")
            C_.Add("205")
            C_.Add("321")
            C_.Add("442")
            C_.Add("505")
            C_.Add("665")
            C_.Add("663")
            C_.Add("321")
    
            For I As Integer = 0 To C_.Count - 1
                TextBox1.AppendText(C_(I) & "   " & F_(I) & vbCrLf)
                Items_.Add(New I_Sort With {.A = C_(I), .B = F_(I)})
    
            Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim SortedItems As IEnumerable(Of I_Sort) = Items_.OrderBy(Function(item) item.B)
            SortedItems.ToList.ForEach(Sub(item) TextBox2.AppendText(item.A & "  =" & item.B & vbCrLf))
        End Sub
    
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Form2.Show()
    
        End Sub
    
        Public Class I_Sort
            Public A As String
            Public B As Integer
    
            Public Overrides Function ToString() As String
                Return A.ToString & " : " & B.ToString
            End Function
    
        End Class
    
    
    
    End Class
    

    Form2:

    Public Class Form2
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'For i As Integer = 0 To Form1.TextBox2.Text.Count - 1
            '    Me.TextBox1.AppendText(Form1.TextBox2.Text(i).ToString)
            'Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim Items As New List(Of Item_Max)
            For Each t In Form1.Items_
                Items.Add(New Item_Max With {.HA_ = t.A, .IA_ = t.B})
            Next
    
    
            Dim Max As Integer = 0
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ > Max Then Max = Items(J).HA_
            Next
            For J As Integer = 0 To Items.Count - 1
                If Items(J).HA_ = Max Then
                    TextBox1.AppendText(Items(J).ToString & vbCrLf)
                End If
            Next
    
        End Sub
        Public Class Item_Max
            Public HA_ As String
            Public IA_ As Integer
    
            Public Overrides Function ToString() As String
                Return HA_.ToString & " : " & IA_.ToString
            End Function
    
        End Class
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mipakteh Wednesday, April 24, 2019 2:20 PM
    Wednesday, April 24, 2019 5:06 AM
  • good,very very good Alex,
    Wednesday, April 24, 2019 2:20 PM