Skip to main content

Find and Replace word in VB.NET.

DESCRIPTION:
Accept a text from user and If user clicks on find button, show index of the first occurrence of the word given in find textbox. If user clicks replace button, found word should be replaced with the word given for replace.
PROPERTIES:
Label1
Text(Empty)
Label2
TextFind Text
Label3
TextReplace Text
Button1
TextFIND
Button2
TextREPLACE
CODEING:
Public Class Form1
    Dim i As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.Text = InputBox("Enter the Text...")
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        i = Label1.Text.IndexOf(TextBox1.Text)
        If i = -1 Or TextBox1.Text = "" Then
            MsgBox(TextBox1.Text & " Not Found.")
        Else
            MsgBox("Index of first occurrence of " & TextBox1.Text & " is: " & (i + 1))
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        i = Label1.Text.IndexOf(TextBox1.Text)
        If i = -1 Or TextBox1.Text = "" Or TextBox2.Text = "" Then
            MsgBox("word Exist Or Input Both Box")
        Else
            MsgBox(Label1.Text & " is update To " & Label1.Text.Replace(TextBox1.Text, TextBox2.Text))
            Label1.Text = Label1.Text.Replace(TextBox1.Text, TextBox2.Text)
        End If
    End Sub
End Class
DESIGNING:









Comments