My Coding!: VB.Net
Showing posts with label VB.Net. Show all posts
Showing posts with label VB.Net. Show all posts

Wednesday 1 April 2015

// // Leave a Comment

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            End If        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            End If        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 Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        TextBox2.Text = UCase(TextBox1.Text)
    End Sub 
    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
        TextBox2.Text = LCase(TextBox1.Text)
    End Sub 
    Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
        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    End Sub 
    Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
        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
    End Sub 
    Private Sub RadioButton5_CheckedChanged(sender As Object, 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)
            If Mid(s1, i, 1) = " " Then                If Mid(s1, i - 1, 1) <> " " Then                    c = c + 1
                End If            End If        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 Sub RadioButton7_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton7.CheckedChanged
        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
            End If        Next        TextBox2.Text = c
    End Sub 
    Private Sub RadioButton9_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton9.CheckedChanged
        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
            End If        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


Read More

Monday 16 March 2015

// // Leave a Comment

How Insert Date and Time in windows Form?


Hi friends this is our first program so we start with date and time.

The very first thing is that you have to take the new form, I was explained in last tutorial that how to take new form. It will look like below image.




After that follow the below steps
Step 1: Take the 2 Labels from Toolbox (Label is used to display anything in form).
Step 2: Rename the first label as  ”lbDate” which we use to display Date
             To rename Label go to Property Box right side of the window search for Design part.
In “Design” search Name type name of label.
You can change other properties like fonts, font color, font style etc. from property
Step 3:  Double click on form.

After double click code window will be open.  It will look like below image.


Write there below code. (Friend I want to suggest you that don’t copy and paste the code, please write it so that you canremember it for future).

lbDate.Text = Format(Now, "dd-M-yyyy")'To display date lbTime.Text = Format(Now, "hh:mm:sstt")‘To display time 
When we write the above code, date and time will be shown. But time will be static here it will show only time when the application will execute. To show continues clock we need to execute the code “lbTime.Text = Format(Now, "hh:mm:sstt")” every second. For this we need Timer tool.

For timer go to the design tab, then search for Component part, in component part there will be Timer, double click on it.


In above image you can see it. Just like the above image see right side of the window in property box, you can see the Enable property make it “True” and in Interval property make it “1000” .
You can also change the name of Timer in Name property.
After that double click on timer and copy paste the code 

lbTime.Text = Format(Now, "hh:mm:sstt")”.

After completing the above all steps, Run the program by clicking on Start button.


There are different formats for different date and time given below.
lbDate.Text = Format(Now, "dddd")         ‘To display the full name of day like “Monday”
lbDate.Text = Format(Now, "ddd")           ‘To display short day name like “Mon”
lbDate.Text = Format(Now, "MMMM")    ‘To display full name of month like “March”
lbDate.Text = Format(Now, "MMM")       ‘To display short name of month like “Mar”
lbDate.Text = Format(Now, "MM")            ‘To display month in no. format like “03”
lbTime.Text = Format(Now, "hh:mm:sstt")‘To display time in 12 hr format
lbTime.Text = Format(Now, "HH:mm:sstt")‘To display time in 24 hr format

Note: We can also display the Date and time on Textbox, masktext box or on any other text display component.
Read More