Visual Basic .NET
Compare Two Strings and return differences and matches
![]() |
VB2010 Compare Strings in Collections |
Collections as an Alternative to Arrays
👺Visual Studio 2010
👩🏿🎤This Example
🥈Solution
Visual Basic 2010 Compare Strings from Treeview |
How does it work ?
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
👂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.'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 ToolBox2) 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
♥ Here are some online Visual Basic lessons and courses: