SETHA SITE INFORMATION

  • Increase font size
  • Default font size
  • Decrease font size
Visual Basic 2008

How to Draw Line in Visual Basic .Net Using Mouse Pointer?

E-mail Print PDF

This article shows you how to create a small Visual Basic .Net project using Mouse Pointer to draw line in a PictureBox control. Mouse Pointer events (MouseDown, MouseMove, and MouseUp) are used in this article. However, if you draw a new line pass through some parts of the existing line in the box then it will erase those parts.

Note: picRegion in code below is a name of PictureBox control
  • Global Variable
    Private startX As Integer = 0   'start position x of line
    Private startY As Integer = 0   'start position y of line
    Private oldEndX As Integer = 0  'the previous ending position x
    Private oldEndY As Integer = 0  'the previous ending position y
    Private gBitmap As Bitmap       'bitmap to place in picturebox
    Private isMouseDown As Boolean = False  'recognize mouse down or up
    
  • Mouse Down Event: is to get position X and Y for the beginning point of line.
    Private Sub picRegion_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles picRegion.MouseDown
            isMouseDown = True
            startX = e.X
            startY = e.Y
    End Sub
    
  • Mouse Move Event: process only while mouse pointer being pressed.
    1. Delete previous line by draw another line with background color
    2. Draw another line with black color with the current ending point
    Private Sub picRegion_MouseMove(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.MouseEventArgs) _
            Handles picRegion.MouseMove
            If isMouseDown = True Then
                'draw line during move
                Dim g As Graphics = Graphics.FromImage(gBitmap)
                g.DrawLine(New Pen(Me.picRegion.BackColor), startX, startY, oldEndX, oldEndY)
                picRegion.Image = gBitmap
    
                g.DrawLine(Pens.Black, startX, startY, e.X, e.Y)
                picRegion.Image = gBitmap
                '
                oldEndX = e.X
                oldEndY = e.Y
            End If
    End Sub
    
  • Mouse Up Event: just note that mouse pointer does not pressed anymore.
    Private Sub picRegion_MouseUp(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles picRegion.MouseUp
            isMouseDown = False
    End Sub
    
  • Form Load: create new bitmap object to be a region for drawing
    Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load
            gBitmap = New Bitmap(picRegion.Width, picRegion.Height)
    End Sub
    

Download Source Code Here
 

Getting Start with DataGridView Control in Visual Basic 2008

E-mail Print PDF

This article enclosed with video clip shows you to get start using the DataGridView Control in Visual Basic .Net.

  • Add Columns to DataGridView
  • Define Column Width
  • Add Data to DataGridView

'add column
With Me.DataGridView1
   .Columns.Add("id", "ID")
   .Columns.Add("name", "Name")
   .Columns.Add("sex", "Sex")
End With

'define columns width
With Me.DataGridView1
   .Columns("id").Width = 200
   .Columns("name").Width = 500
   .Columns("sex").Width = 100
End With

'add data (10 rows)
For i As Integer = 1 To 10
    Me.DataGridView1.Rows.Add()
    With Me.DataGridView1.Rows(Me.DataGridView1.Rows.Count - 2)
        .Cells("id").Value = i
        .Cells("name").Value = "Name " & i
        .Cells("sex").Value = "M"
    End With
Next
 

How to Draw Shape (Line, Rectangle, Pie) in Visual Basic .Net?

E-mail Print PDF

This article uses Visual Basic .Net code to draw Line, Rectangle and Pie within a Form object.

Draw Two Lines
Dim g As Graphics = Me.CreateGraphics()
g.DrawLine(Pens.Black, 50, 70, 100, 250)
g.DrawLine(Pens.Black, 120, 70, 120, 250)
Draw a Rectangle (Width=100, Height=200)
Dim g As Graphics = Me.CreateGraphics
g.DrawRectangle(Pens.Blue, 220, 70, 100, 200)
Draw a Pie (Height=200, Width=100, Angle=270)
Dim g As Graphics = Me.CreateGraphics
g.DrawPie(Pens.Red, 400, 70, 100, 200, 0, 270)

Download Code Here
 

How to create text file, write text file, and read text file in VB .Net?

E-mail Print PDF

This article shows you how to write Visual Basic .Net code to create Text File, write text into Text File and read content from Text File.

When we request to write text into Text File it will check first whether Text File does exist or does not. If it does not exist it will create new Text File automatically. Otherwise it will append text into the existing text file.

Write text into Text File
Dim sw As New System.IO.StreamWriter("C:\txttest.txt", True)
sw.WriteLine(txtWrite.Text)
sw.Close()

MsgBox("Save completed!")
Me.txtWrite.Text = ""
Read content from Text File
Dim sr As New System.IO.StreamReader("C:\txttest.txt")
Dim str As String
Do
   str = sr.ReadLine()
   Me.txtRead.Text += str + vbCrLf
Loop Until sr.EndOfStream = True
sr.Close()

Download Source Code
 

How to get file name from a specified directory in Visual Basic.Net?

E-mail Print PDF
The below VB.Net code uses to get all document files (*.docx) in directory C:\Test\. For example, if we have 3 document files (.docx) in C:\Test\ directory then the below code will result those three files in full path on 3 message boxes sequentially.
 
Visual Basic .Net Code
Dim strFiles() As String
strFiles = System.IO.Directory.GetFiles("C:\Test\","*.docx")
For Each myStr As String In strFiles
    MsgBox(myStr)
Next
 


Page 2 of 6