2077 Posts in 484 Topics- by 821 Members - Latest Member: liricklagu

Pages: [1]   Go Down
  Print  
Author Topic: [Tutorial VB.NET] Membuat Graphic Vertikal Batang  (Read 2748 times)
webmaster
Administrator
phpBB Guru
*****
Offline Offline

Posts: 924


hairulazami
View Profile WWW
« on: January 27, 2009, 10:17:52 PM »

Membuat Graphic Vertikal Batang dengan VB.NET


Pertama pastikan Visual Studio lu terinstall dengan baik di komp.




Buwat project baru, lwat [CTRL+SHIFT+N] / File > New Project (Windows Aplication)





Pada Form baru, bwat design form kaya gini, gunakan TextBox, Label, dan Button









Text masing2 Label: Monday, Tuesday, Wednesday, Thursday, Friday

Name masing2 textBox: txtMonday, txtTuesday, txtWednesday, txtThursday, txtFriday

Value masing2 textBox: 12000, 11000, 8500, 16800, 17500

Name Button: btnGenerate

Value Button: Generate



hasil form nya kayak gini:





Disini gw pake Icon EXE nya dengan Icon Sendiri, bisa lu ganti di Properties Form > Icon





Sekarang Double Click pada Form Area, untuk memunculkan Script Form na, yakni script on load na

pada interface script, terdapat pilihan combo Form1, disana pilih "(Form1 Events)", dan pilih juga combo box Declaration dengan pilihan "Load"

Ktik kode ini:

Code:
Dim graphDrawingArea As Graphics
Dim bmpDrawingArea As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bmpDrawingArea = New Bitmap(Width, Height)
graphDrawingArea = Graphics.FromImage(bmpDrawingArea)
End Sub

Selanjutnya pilih kembali Combo (Form1 Events) dan Paint

kasi kode berikut ini:

Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.DrawImage(bmpDrawingArea, 0, 0)
End Sub



Beralih ke pilihan combo box "btnGenerate" dan dengan declaration "Click", kasi kdoe dibwah ini:





Code:
Private Sub btnGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
Dim monday As Integer = CInt(Me.txtMonday.Text) / 100
Dim tuesday As Integer = CInt(Me.txtTuesday.Text) / 100
Dim wednesday As Integer = CInt(Me.txtWednesday.Text) / 100
Dim thursday As Integer = CInt(Me.txtThursday.Text) / 100
Dim friday As Integer = CInt(Me.txtFriday.Text) / 100

graphDrawingArea.Clear(Me.BackColor)

graphDrawingArea.FillRectangle(New SolidBrush(Color.Red), Me.txtMonday.Left + 5, 280 - monday, 40, monday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtMonday.Left + 5, 280 - monday, 40, monday)
graphDrawingArea.FillRectangle(New SolidBrush(Color.Blue), Me.txtTuesday.Left + 5, 280 - tuesday, 40, tuesday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtTuesday.Left + 5, 280 - tuesday, 40, tuesday)
graphDrawingArea.FillRectangle(New SolidBrush(Color.Fuchsia), Me.txtWednesday.Left + 5, 280 - wednesday, 40, wednesday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtWednesday.Left + 5, 280 - wednesday, 40, wednesday)
graphDrawingArea.FillRectangle(New SolidBrush(Color.Brown), Me.txtThursday.Left + 5, 280 - thursday, 40, thursday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtThursday.Left + 5, 280 - thursday, 40, thursday)
graphDrawingArea.FillRectangle(New SolidBrush(Color.Turquoise), Me.txtFriday.Left + 5, 280 - friday, 40, friday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtFriday.Left + 5, 280 - friday, 40, friday)

graphDrawingArea.DrawRectangle(New Pen(Color.Black), 10, 280, Width - 30, 1)
Invalidate()
End Sub



jadi kalo bwat pengecekan, ni script lengkapnya:

Code:
Public Class Form1
Inherits System.Windows.Forms.Form


Dim graphDrawingArea As Graphics
Dim bmpDrawingArea As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bmpDrawingArea = New Bitmap(Width, Height)
graphDrawingArea = Graphics.FromImage(bmpDrawingArea)
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.DrawImage(bmpDrawingArea, 0, 0)
End Sub

Private Sub btnGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
Dim monday As Integer = CInt(Me.txtMonday.Text) / 100
Dim tuesday As Integer = CInt(Me.txtTuesday.Text) / 100
Dim wednesday As Integer = CInt(Me.txtWednesday.Text) / 100
Dim thursday As Integer = CInt(Me.txtThursday.Text) / 100
Dim friday As Integer = CInt(Me.txtFriday.Text) / 100

graphDrawingArea.Clear(Me.BackColor)

graphDrawingArea.FillRectangle(New SolidBrush(Color.Red), Me.txtMonday.Left + 5, 280 - monday, 40, monday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtMonday.Left + 5, 280 - monday, 40, monday)
graphDrawingArea.FillRectangle(New SolidBrush(Color.Blue), Me.txtTuesday.Left + 5, 280 - tuesday, 40, tuesday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtTuesday.Left + 5, 280 - tuesday, 40, tuesday)
graphDrawingArea.FillRectangle(New SolidBrush(Color.Fuchsia), Me.txtWednesday.Left + 5, 280 - wednesday, 40, wednesday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtWednesday.Left + 5, 280 - wednesday, 40, wednesday)
graphDrawingArea.FillRectangle(New SolidBrush(Color.Brown), Me.txtThursday.Left + 5, 280 - thursday, 40, thursday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtThursday.Left + 5, 280 - thursday, 40, thursday)
graphDrawingArea.FillRectangle(New SolidBrush(Color.Turquoise), Me.txtFriday.Left + 5, 280 - friday, 40, friday)
graphDrawingArea.DrawRectangle(New Pen(Color.Black), Me.txtFriday.Left + 5, 280 - friday, 40, friday)

graphDrawingArea.DrawRectangle(New Pen(Color.Black), 10, 280, Width - 30, 1)
Invalidate()
End Sub
End Class



Save file projet lu, dan Start Debugging (F5) untuk melihat hasil nya, begitu kita mengklik Tombol Generate, maka akan tampil kalkulasi hasil graphic batang. Selamat mencoba broder, semoga sukses !!!



« Last Edit: June 16, 2009, 11:46:33 AM by webmaster » Logged


Pages: [1]   Go Up
  Print  
 
Jump to: