Wednesday 1 April 2015

How to Perform String Operation in Windows Application?


Hi friends today we learn about the various string operations.


In many application we need to perform strings such as we need only upper case letters, but user enter lower case letters, in this case we have to convert them into upper case. Like this there are many string operations which we discuss here. 


1.    Making Upper case letter

Step 1: Take New Form. You can take new form by following steps.
    
 Click on “Project” Menu.
 Click on “Add Windows Form ”, you can see in below image




Step2 :  Take two “TextBox” , one for display unformatted text and another for formatted text.

Step3 : Take one “RadioButton”. You can take simple button also from Toolbox

Step4 : Double click on that RadioButton and write below code.

            TextBox2.Text = UCase(TextBox1.Text)

In TextBox2, the formatted text i.e. Upper Case Letters will be displayed and text will be input by user in TextBox1.    It will look like below picture.

Upper Case

Now similarly you can perform different operation using radio button. You have to take another RadioButtons and write below code. On each radio button by double click on it.

1.    Making Lower Case Letter

Source Code :  
TextBox2.Text = LCase(TextBox1.Text)
Lower Case


1.    Extract Character
     TextBox2.Text = Mid(TextBox1.Text, Val(TextBox5.Text), Val(TextBox6.Text))        If RadioButton3.Checked = False Then            TextBox5.Clear()            TextBox6.Clear()            RadioButton3.Enabled = False    End If

Explanation:

Mid(integer 1, integer 2) is a function used to extract the character from string. First argument defines the starting position of string, and second argument defines the number of characters. 
Clear() function is used to clear the textbox or any input done by user.
Checked is the property of radio button and CheckBox (we will discuss latter it). It is a    boolean property and either True or False.
 Enabled . It is a property which decide that  the tool will be work or not.

Extract Character


4.    Count Character without space
 Dim i As Integer        Dim c As Integer        For i = 1 To Len(TextBox1.Text)            If Mid(TextBox1.Text, i, 1) <> "" Then                c = c + 1            End If        Next        TextBox2.Text = c

Explanation :  i and c are the variables. In VB.Net <> means not equal to.

5.    Count word
 Dim s As String        Dim s1, s2 As String        s = TextBox1.Text        s1 = UCase(Mid(s, 1, 1))        s2 = ""        Dim i As Integer        For i = 2 To Len(s)
            s2 = s2 & LCase(Mid(s, i, 1))        Next        TextBox2.Text = s1 & s2




6.    Count capital Letters

Dim i, c As Integer        For i = 1 To Len(TextBox1.Text)
    If Asc(Mid(TextBox1.Text, i, 1)) >= 65 And Asc(Mid(TextBox1.Text, i, 1)) <= 90 Then                c = c + 1            EndIf        Next        TextBox2.Text = c

7.    Count Small Letters

 Dim i, c As Integer     For i = 1 To Len(TextBox1.Text)
     If Asc(Mid(TextBox1.Text, i, 1)) >= 97 And Asc(Mid(TextBox1.Text, i, 1)) <= 122 Then                c = c + 1            EndIf        Next        TextBox2.Text = c

Here is a complete design


 Here is a complete code .

    Public flag, z As Integer    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RadioButton3.Enabled = False        Button2.Hide()
    End Sub 
    Private SubRadioButton1_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton1.CheckedChanged
        TextBox2.Text = UCase(TextBox1.Text)
    End Sub 
    Private SubRadioButton2_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton2.CheckedChanged
        TextBox2.Text = LCase(TextBox1.Text)
    End Sub 
    Private SubRadioButton3_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton3.CheckedChanged
        TextBox2.Text = Mid(TextBox1.Text, Val(TextBox5.Text), Val(TextBox6.Text))
        IfRadioButton3.Checked = False Then            TextBox5.Clear()
            TextBox6.Clear()
            RadioButton3.Enabled = False        End If    End Sub 
    Private SubRadioButton4_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton4.CheckedChanged
        Dim i As Integer        Dim c As Integer        For i = 1 To Len(TextBox1.Text)
            IfMid(TextBox1.Text, i, 1) <> " " Then                c = c + 1
            EndIf        Next        TextBox2.Text = c
    End Sub 
    Private SubRadioButton5_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton5.CheckedChanged
        Dim i As Integer        Dim c As Integer        Dim s1 As String        s1 = Trim(TextBox1.Text)
        For i = 1 To Len(s1)
            IfMid(s1, i, 1) = " "Then                If Mid(s1, i - 1, 1) <> " " Then                    c = c + 1
                End If            EndIf        Next        TextBox2.Text = c + 1
    End Sub 
    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged, TextBox6.TextChanged
        If Not ((TextBox5.Text = "") Or (TextBox6.Text = "")) Then            RadioButton3.Enabled = True 
        End If    End Sub 
    Private SubRadioButton7_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton7.CheckedChanged
        Dim i, c As Integer        For i = 1 To Len(TextBox1.Text)
            IfAsc(Mid(TextBox1.Text, i, 1)) >= 65 AndAsc(Mid(TextBox1.Text, i, 1)) <= 90 Then                c = c + 1
            EndIf        Next        TextBox2.Text = c
    End Sub 
    Private SubRadioButton9_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton9.CheckedChanged
        Dim i, c As Integer        For i = 1 To Len(TextBox1.Text)
            IfAsc(Mid(TextBox1.Text, i, 1)) >= 97 AndAsc(Mid(TextBox1.Text, i, 1)) <= 122 Then                c = c + 1
            EndIf        Next        TextBox2.Text = c
    End Sub 
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Timer1.Enabled = False        Button1.Show()
        Button2.Hide()
        TextBox2.Clear()
        flag = 1
    End SubEnd Class
Public Class Form1




No comments:

Post a Comment

About Me

Popular Posts

Designed ByBlogger Templates