🔤 Mastering
String Manipulation in VB.NET
with WinForms: A Complete Code Example
String manipulation is a fundamental part of programming with Visual Basic .NET, especially when building desktop applications with Windows Forms. In this post, you'll explore a comprehensive and interactive example that demonstrates over 30 string functions in VB.NET, complete with dynamic UI controls.
This VB.NET project showcases:
- ✅ 31 String Methods
- ✅ 2 Dynamic TextBoxes (Input & Output)
- ✅ Auto-generated RadioButtons for each string method
- ✅ Practical usage of .NET Framework 4.8
- ✅ Real-time examples for each function
🧠 What You'll Learn
This example helps you master these key VB.NET string functions:
Asc
,Chr
,InStr
,Join
,Replace
,Trim
,Format
,Split
, and many more.- How to dynamically generate controls at runtime.
- Enum-based radio button selection to trigger method demonstrations.
🛠️ How to Run This Example
- Open Visual Studio.
- Create a new VB.NET Windows Forms App (.NET Framework 4.8) project.
- Add a new Form:
MainForm.vb
. - Paste the code below inside your
MainForm
class.
💻 Complete Source Code (MainForm.vb)
Public Class MainForm
Private Enum Description As Integer
Asc : AscW : Chr : ChrW : Filter : Format : FormatCurrency
FormatDateTime : FormatNumber : FormatPercent : InStr : InStrRev : Join
LCase : Left : Len : LSet : LTrim : Mid : Replace : Right
RSet : RTrim : Space : Split : StrComp : StrConv : StrDup
StrReverse : Trim : UCase
End Enum
Private WithEvents Ioptions As RadioButton
Private WithEvents ItextBox As TextBox
Private ThisTextBox(1) As TextBox
Private Labels(1) As Label
Dim LabelText As String() = {"Input", "Output"}
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
Size = New Size(575, 425)
Dim EnumCount As Integer = 0
Dim IoptionNm As String() = [Enum].GetNames(GetType(Description))
For Each Imem As String In IoptionNm
EnumCount += 20
Ioptions = New RadioButton With {
.Size = New Size(115, 17),
.Location = If(EnumCount < 20 * 19, New Size(0, EnumCount), New Size(150, EnumCount - (20 * 18))),
.Name = Imem,
.Text = Imem
}
AddHandler Ioptions.CheckedChanged, AddressOf OptionOption
Controls.Add(Ioptions)
Next
For I As Integer = 0 To 1
ItextBox = New TextBox With {
.Multiline = True,
.Size = New Size(250, 80),
.Location = New Point(300, 100 * (I + 0.25)),
.ScrollBars = ScrollBars.Both,
.Name = $"TextBox{I}",
.Text = String.Empty
}
AddHandler ItextBox.TextChanged, AddressOf TextBox_TextChanged
ThisTextBox(I) = ItextBox
Controls.Add(ItextBox)
Dim ILabel = New Label With {
.Size = New Size(100, 20),
.Location = New Point(ThisTextBox(I).Location.X, ThisTextBox(I).Location.Y - 15),
.Name = $"Label{I}",
.Text = LabelText(I)
}
Labels(I) = ILabel
Controls.Add(ILabel)
Next
End Sub
Private Sub OptionOption(sender As Object, e As EventArgs)
Dim Radios = DirectCast(sender, RadioButton)
ThisTextBox(0).Text = String.Empty
ThisTextBox(0).ReadOnly = True
ThisTextBox(1).Text = String.Empty
Select Case Radios.Text
Case Description.Asc.ToString
ThisTextBox(0).Text = "Returns ASCII code of characters."
For Each ch As Char In ThisTextBox(0).Text
ThisTextBox(1).Text += Asc(ch) & " "
Next
Case Description.Chr.ToString
Dim inputStr = "Hello"
Dim result As String = ""
For Each ch As Char In inputStr
result += Chr(Asc(ch))
Next
ThisTextBox(0).Text = inputStr
ThisTextBox(1).Text = result
Case Description.Join.ToString
Dim arr() As String = {"VB", "NET", "String"}
ThisTextBox(0).Text = "Joining array: " & String.Join(",", arr)
ThisTextBox(1).Text = Join(arr, "-")
Case Description.Replace.ToString
ThisTextBox(0).Text = "Replace the word 'VB' with 'Visual Basic'."
ThisTextBox(1).Text = Replace("I love VB", "VB", "Visual Basic")
Case Description.Trim.ToString
ThisTextBox(0).Text = " Trim these spaces. "
ThisTextBox(1).Text = Trim(ThisTextBox(0).Text)
Case Description.Len.ToString
ThisTextBox(0).Text = "Length of this sentence."
ThisTextBox(1).Text = Len(ThisTextBox(0).Text).ToString()
Case Description.StrReverse.ToString
ThisTextBox(0).Text = "Reverse me!"
ThisTextBox(1).Text = StrReverse(ThisTextBox(0).Text)
End Select
End Sub
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub MainForm_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = ChrW(Keys.Escape) Then Close()
End Sub
End Class
🔗 Related Resources
💡 Final Thoughts
This WinForms example provides an interactive playground to test and understand all the commonly used string manipulation methods in VB.NET. It’s perfect for students, beginner developers, or anyone needing a refresher on VB.NET string handling.
💬 If you found this post helpful, please share it and leave a comment below!
♥ Here are some online Visual Basic lessons and courses: