IT KNOWLEDGE

IT KNOWLEDGE
BC

Friday, May 30, 2014

How to Invert Image in PictureBox (VB.Net)

In many developing programs allow us to develop program to edit image or customize as you want. This tutorial will show you how to develop a small application to invert image in PictureBox. I use two PictureBoxes which one is stored the base image and another one for getting result the image which is inverted.
No Control Type Name Text Event
1 Form1 FormInvertImage Inverting Image
2 PictureBox1 PictureBoxBase
3 PictureBox2 PictureBoxInvert
4 Button1 ButtonInvert Invert Click
   
  • As you see in the application screenshot, when you click on button "Invert" , you will get the image result in PictureBoxInvert. 

























How to make Font Auto Size in Textbox (VB.Net)

This tutorial will show you how to set font size of Textbox to autosize means that when you type more words in Textbox, It will set the font size to fit the Textbox which contains these text.

No Control Type Name Text Event
1 Form1 FormAutoSize AutoSize Font
2 Textbox1 TextBoxAuto Text_Changed

Code
Public Class FormAutoSizeFont
    Dim h As String
    Dim w As String
    Private Sub FormAutoSizeFont_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim orgFont As New Font(TextBoxAutosize.Font.Name, TextBoxAutosize.Font.Size, TextBoxAutosize.Font.Style)

        Dim textSize As New SizeF
        textSize = e.Graphics.MeasureString(TextBoxAutosize.Text, orgFont)
        h = textSize.Height
        w = textSize.Width
    End Sub

    Private Sub TextBoxAutosize_TextChanged(sender As Object, e As EventArgs) Handles TextBoxAutosize.TextChanged
        TextBoxAutosize.Font = New Font(TextBoxAutosize.Font.Name, 12, TextBoxAutosize.Font.Style)
        TextBoxAutosize.BorderStyle = BorderStyle.Fixed3D
        Do Until w > TextBoxAutosize.Size.Width - 5 Or TextBoxAutosize.Text = Nothing Or h > 182
            TextBoxAutosize.Font = New Font(TextBoxAutosize.Font.Name, TextBoxAutosize.Font.Size + 2, TextBoxAutosize.Font.Style)
        Loop
        TextBoxAutosize.BorderStyle = BorderStyle.None
        TextBoxAutosize.TextAlign = HorizontalAlignment.Center
        TextBoxAutosize.WordWrap = True
    End Sub
End Class










Saturday, May 24, 2014

How to set input number only in textbox (VB.Net)

When the most program developers create one control on a form which to operate number such as Quantity of Item, Number of item.., always set that control use only number to help user avoid using wrong input. In this application, I add one TextBox(Name: TextBoxOnlyNumber;  Text: Only Number) and use this textbox with KeyPress_Event(When we type text in textbox, the code will execute) then message box will show("Please enter numbers only"), if we input not number.

Code
Public Class OnlyNumber
    Private Sub TextBoxOnlyNumber_Click(sender As Object, e As EventArgs) Handles TextBoxOnlyNumber.Click
        TextBoxOnlyNumber.Text = ""
    End Sub
    Private Sub TextBoxOnlyNumber_MouseLeave(sender As Object, e As EventArgs) Handles TextBoxOnlyNumber.MouseLeave
        TextBoxOnlyNumber.Text = "Only Number!"
    End Sub
    Private Sub TextBoxOnlyNumber_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBoxOnlyNumber.KeyPress
        If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Please enter numbers only")

            e.Handled = True

        End If
    End Sub
End Class

  
this code, there are 3 block,our main code is only one block(TextBoxOnlyNumber_KeyPress), but I added another 2 blocks are:
  1. TextBoxOnlyNumber_Click : For clear textbox when we click on textbox.
  2. TextBoxOnlyNumber_MouseLeave : for showing text("Only Number") when the mouse leave from textbox.

How to find a word after specific one word (VB.Net)

Sometime programmers need a method to find the word after one specific word in one specific develop program such as VB.Net, C#, Java.... To reply this, I wrote the sample one application using VB.Net to find word after one specific word which input in textbox as below:

Application Design

No Control Type Name Text
1 Form1 FormWordAfterWord Get Word After Word
2 Textbox1 TextboxWord
3 Textbox2 TextboxFind
4 Button1 ButtonFind Find

  • As you see sample above, we input text in TextBoxWord "Visual Basic is my favourite" and we want to find one word which located after word "my" in TextBoxFind and the message box show that "The word after word : my is favourite" which is right to our goal.

 Code
Imports System.Text.RegularExpressions
Public Class FormWordAfterWord

    Private Sub ButtonFind_Click(sender As Object, e As EventArgs) Handles ButtonFind.Click

        Dim text As String = TextBoxWord.Text



        Dim word As String = "\b" & TextBoxFind.Text & "\b\s+(\w+)"



        For Each a As Match In Regex.Matches(text, word, RegexOptions.IgnoreCase)



            MessageBox.Show("The word after word: " & TextBoxFind.Text & "  is  " & a.Groups(1).Value)

        Next
    End Sub
End Class













Friday, May 23, 2014

How to Validate Email Address in Textbox (VB.Net)

The small application below show you how to validate email address in textbox using VB.Net. To test process, I have designed as below:

No Control Type Name Text
1 Form1 ValidateEmail Validate Email
2 Textbox1 TextBoxEmail
3 Label1 LabelStatus
4 Button1 ButtonValidate Validate Email

This process, I used ButtonValidate_Click to check email address in TextBoxEmail. I use to condition which is True/False condition as you see in code below and the result will show in LabelStatus. This code work propertly!!!

Code

Imports System.Text.RegularExpressions
Public Class ValidateEmail
    Private Sub ButtonValidate_Click(sender As Object, e As EventArgs) Handles ButtonValidate.Click
        Dim pattern As String = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"


        Dim match As System.Text.RegularExpressions.Match = Regex.Match(TextBoxEmail.Text.Trim(), pattern, RegexOptions.IgnoreCase)
        If (match.Success) Then
            LabelStatus.Text = "The email address :  " & TextBoxEmail.Text & "  is corrected"
        Else
            LabelStatus.Text = "The email address :  " & TextBoxEmail.Text & "  is incorrected !!!"
            TextBoxEmail.Clear()
        End If
    End Sub
End Class