20.3.11

Measure Elapsed Time with VB.NET

Hi Everybody,
I would like to discuss one of the most common FAQs regarding how we can accurately measure elapsed time at current instance with VB.NET 2010 Windows application? If any question, please feel free and contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

We are using stopwatch System.Diagnostics.Stopwatch for using elapsed time. Lets have a look useful example for it.


1-)We are creating a new Windows Application Project (stopwatch)



2-)We are designing this windows form in a similar way to the picture below




3-) We are writing this code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'create a stopwatch
Dim my_swatch As New Stopwatch()

'start stopwatch for measuring time
my_swatch.Start()

'define a double
Dim i As Double

'loop until i = 100000
Do Until i = 100000

' i add 1
i = i + 1

' add listbox
ListBox1.Items.Add(i)

Loop

'show elapsed time after process completed
MsgBox(my_swatch.ElapsedMilliseconds)

End Sub

4-)Lets run application




Have a great day

Yener Turkeli
yenerturkeli@v-basic.net

16.3.11

Serial Port Communication


Hi Everybody,
I would like to explain most common question within this essay , how we can communicate serial port with VB.NET 2010 windows application? If any question, please feel free and contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

1-) We are creating a new Windows Application Project (serial_port)



2-) We are designing this windows form in a similar way to the picture below




3-)We are writing this code

Private Sub get_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles get_btn.Click

'if port is open
If SerialPort1.IsOpen Then
SerialPort1.RtsEnable = False
SerialPort1.DtrEnable = False
SerialPort1.Close()

Application.DoEvents()



End If

'set port setting
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = 0
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.WriteTimeout = 2000

Try

'open port
SerialPort1.Open()

Catch ex As Exception

'give alert any problem
MsgBox(ex.Message)

End Try

If SerialPort1.IsOpen Then
SerialPort1.RtsEnable = True

End If


End Sub


Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

'define a string
Dim data_received As String

'get data from serial port
data_received = SerialPort1.ReadLine


'give an alert when data received
MsgBox(data_received)


End Sub



4-) Lets run application





Have a great day

Yener Turkeli
yenerturkeli@v-basic.net


10.3.11

Working with Clipboard

Hi Everybody,
I would like to discuss one of the most common FAQs regarding how we can work screenshot withVB.NET 2010 Windows application? If any question, please feel free and contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

1-)We are creating a new Windows Application Project (working_clipboard)




2-)We are designing this windows form in a similar way to the picture below




3-) We are writing this code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'send printSRC key to windows
SendKeys.Send("+({PRTSC})")

'wait in order to catch the clipboard
System.Threading.Thread.Sleep(100)

'screen_shot as dataobject
Dim screen_shot As IDataObject

'get data from clipboard
screen_shot = Clipboard.GetDataObject()

'define an image
Dim bmp_image As Bitmap

'if clipboard data present
If screen_shot.GetDataPresent(GetType(System.Drawing.Bitmap)) Then

'convert to image
bmp_image = CType(screen_shot.GetData(GetType(System.Drawing.Bitmap)), Bitmap)

'show image in picturebox
PictureBox1.Image = bmp_image
End If



End Sub

4-) Lets run the application



Programme can easily get screenshot . If you like you can run background process , windows service .


Yener Turkeli
yenerturkeli@v-basic.net


1.3.11

Folder Watcher with VB.NET 2010

Hi Everybody,
I would like to explain most common question within this essay , how we can watch a folder with VB.NET 2010 windows application? If any question, please feel free and contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

1-) We are creating a new Windows Application Project (file_system_watcher)




2-) We are designing this windows form in a similar way to the picture below



3-)We are writing this code

Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
ListBox1.Items.Add("Modified: " & e.FullPath)
End Sub

Private Sub show_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles folder_btn.Click




Try
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then

'select folder
folder_txt.Text = FolderBrowserDialog1.SelectedPath
FileSystemWatcher1.Path = folder_txt.Text

End If
Catch ex As Exception
MsgBox(ex.Message, vbCritical, "File System Watcher")
End Try


End Sub

Private Sub FileSystemWatcher1_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created

'if any file created add fullpath to the listbox
ListBox1.Items.Add("Created: " & e.FullPath)
End Sub

Private Sub FileSystemWatcher1_Deleted(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted

'if any file deleted add fullpath to the listbox
ListBox1.Items.Add("Deleted: " & e.FullPath)
End Sub

Private Sub FileSystemWatcher1_Renamed(ByVal sender As System.Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed

'if any file renamed add path to the listbox
ListBox1.Items.Add("Renamed: " & e.FullPath)
End Sub

4-) Lets run application and select a folder




Yener Turkeli
yenerturkeli@v-basic.net