Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

Visual Basic .NET

Compare Two Strings and return differences and matches

vb2010 compare strings and return differences
VB2010 Compare Strings in Collections

Collections as an Alternative to Arrays

👺Visual Studio 2010

Although collections are most often used for working with the Object Data Type, you can use a collection to work with any Data type

In some circumstances, it can be more efficient to store items in a collection than in an array.

If you need to change the size of an array, you must use the ReDim Statement (Visual Basic). When you do this, Visual Basic creates a new array and releases the previous array for disposal. This takes execution time. Therefore, if the number of items you are working with changes frequently, or you cannot predict the maximum number of items you need, you might obtain better performance using a collection.

A collection, which does not have to create a new object or copy existing elements, can handle resizing in less execution time than an array, which has to use ReDim. But if the size does not change, or changes only rarely, an array is likely to be more efficient. As always, performance is highly dependent on the individual application. It is often worth your time to try both an array and a collection.

👩🏿‍🎤This Example

How to compare two Treeviews in VB2010
How to return the differences between two Treeviews

🥈Solution

- Create Visual Basic 2010 Windows Application Project
- On Form1, place two tree views and two textboxes and one button

Visual Basic Online Course
Visual Basic 2010 Compare Strings from Treeview

How does it work ?

- Store the Treeview1 [TRV1] nodes into Textbox, and the same with Treeview2 [TRV2]
- How many strings [Lines] in each Textbox

We want to find where both Textboxes are different and where both Textboxes are matched to one another. 

For example, differences are :

  • TRV1 has an extra string [Southern American]
  • TRV2 node string [Africa] is different from TRV2 node string [African]

🎁Steps to accomplish this task

'Visual Basic Online Course - Arrays
'Compare Two Arrays and Return differences - Dec./2013 
'By/ Acc. Ahmed Ebied (evry1falls) [Google me]

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, _
    e As System.EventArgs) Handles MyBase.Load
        'Nothing to be done here
    End Sub
    Private Sub BtnDiff_Click(sender As System.Object, _
    e As System.EventArgs) Handles BtnDiff.Click
        Dim Nodes1Cnt As Integer = TRV1.Nodes.Count - 1
        Dim Nodes2Cnt As Integer = TRV2.Nodes.Count - 1
        'TextBoxes are used 
        Dim Txt1, Txt2 As New TextBox

        For I As Integer = 0 To Nodes1Cnt
            Txt1.AppendText(TRV1.Nodes(I).Text & (vbCrLf))
        Next
        For I As Integer = 0 To Nodes2Cnt
            Txt2.AppendText(TRV2.Nodes(I).Text & (vbCrLf))
        Next
        Dim Sfound As String = ""
        Dim XI As Integer = 0

        Dim Lines1 = Txt1.Lines
        Dim Lines2 = Txt2.Lines

        For Each line In Lines1
            Dim curLine As String = line
            If Lines2.Any(Function(s) s.Equals(curLine, _
            StringComparison.CurrentCultureIgnoreCase)) Then
                Dim Matched As String = (curLine & vbCrLf)
                'You can retrieve matches here,
                ' I.E [TextBox2.Text = Matched]
            Else
                'Return Array comparison result here
                Sfound += (curLine & vbCrLf)
            End If
        Next
        TxtDiff.Text = Sfound
    End Sub
End Class
VB.NET Github Gist

👂Extended work

  • You can easily extract any Database tables, columns Schema into a TREEVIEW control, and you can also using this example compare two tables or two columns in a Database using this example as well.
  • You can use ListViews or ComboBoxes or ListBoxes instead of the Treeview control, I used the Treeview control because it's always tricky to work with. 
  • Using other controls that store strings in Collection format would be so easy, even much easier than using TreeViews.

🔬Another Treeview Examples

Expand and Collapse two TreeViews at the same time according to one another even if they both don't have the same nodes names. 
Expand TreeView1 [TRV1] node when TreeView2 [TRV2] node is expanded or collapsed.
'Visual Basic Online Course [adonetaccess2003.blogspot.com]
'Expand and Collapse Treeviews
'Find a node in one treeview that matches another node
'in another treeview.

 Private Sub TV1_AfterCollapse(sender As Object, _
 e As System.Windows.Forms.TreeViewEventArgs) _
 Handles TV1.AfterCollapse
 
        TV1.SelectedNode = e.Node
        Dim I As Integer = TV2.Nodes.Count - 1
        For sN As Integer = 0 To I
            If TV2.Nodes(sN).Text = TV1.SelectedNode.Text Then
                TV2.Nodes(sN).Collapse()
                Exit For
                Exit Sub
            End If
        Next
    End Sub
    
    Private Sub TV1_AfterExpand(sender As Object, _
    e As System.Windows.Forms.TreeViewEventArgs) _
    Handles TV1.AfterExpand
    
        TV1.SelectedNode = e.Node
        Dim I As Integer = TV2.Nodes.Count - 1
        For sN As Integer = 0 To I
            If TV2.Nodes(sN).Text = TV1.SelectedNode.Text Then
                TV2.Nodes(sN).Expand()
                Exit For
                Exit Sub
            End If
        Next
    End Sub
    
    Private Sub TV2_AfterCollapse(sender As Object, _
    e As System.Windows.Forms.TreeViewEventArgs) _
    Handles TV2.AfterCollapse
    
        TV2.SelectedNode = e.Node
        Dim I As Integer = TV1.Nodes.Count - 1
        For sN As Integer = 0 To I
            If TV1.Nodes(sN).Text = TV2.SelectedNode.Text Then
                TV1.Nodes(sN).Collapse()
                Exit For
                Exit Sub
            End If
        Next
    End Sub
    
    Private Sub TV2_AfterExpand(sender As Object, _
    e As System.Windows.Forms.TreeViewEventArgs) _
    Handles TV2.AfterExpand
    
        TV2.SelectedNode = e.Node
        Dim I As Integer = TV1.Nodes.Count - 1
        For sN As Integer = 0 To I
            If TV1.Nodes(sN).Text = TV2.SelectedNode.Text Then
                TV1.Nodes(sN).Expand()
                Exit For
                Exit Sub
            End If
        Next
    End Sub

TreeView Example : Change the node font

'Visual Basic Online Course
'VB 2010 Change the node Font
Dim ColRoot As treeNode
    ColRoot.NodeFont = New System.Drawing.Font("Arial", 9, FontStyle.Underline)
    ColRoot.ForeColor = Color.Brown

TreeView Example : Add a photo (icon) to the TreeView

1) Add ImageList from the ToolBox
2) Add 2 photos to the ImageList1 [From properties, Collection], for example :
- Photo1.Png (Index by default is Zero 0), this will be the Parent icon
- Photo2.Png (Index by default is One 1), this will be the first child icon
3) Code to display image/Icon in the Tree View :
'Visual Basic Online Course
'VB 2010 Add photo to Treeview
Public Class Form1
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    TRV1.ImageList = ImageList1
    'Parent Node
    TRV1.Nodes.Add(key:="Info", text:="First Name", imageIndex:=0)
    'First Child Node
    TRV1.Nodes(0).Nodes.Add(text:="Evry1falls", imageIndex:=1)
  End Sub
End Class

VB.NET Github Gist

 Here are some online Visual Basic lessons and courses:

Bottom Ad [Post Page]