Kamis, 19 Desember 2013

Modifikasi Aplikasi “Hello, World!” dengan menambah kontrol secara dinamis pada saat di jalankan di Visual Basic 6.0

Hello_World_Modif_vb6

Setelah pada posting sebelumnya saya membuat aplikasi sederhana “Hello, World!” maka pada postingan ini saya mencoba beberapa modifikasi yang saya harap dapat memberikan gambaran lebih lanjut kepada teman-teman mengenai metode menambah kontrol secara dinamis pada saat aplikasi.di jalankan.

 

1. Jalankan terlebih dahulu aplikasi Visual Basic 6.0

2. Ketika jendela “New Project” muncul pilih “Standard EXE” kemudian tekan tombol “Open”

New Project VB6

3. Pada “Menu Bar” pilih “View” kemudian pilih “Code” atau pada jendela “Project” pilih “View Code

Project View Code

4. Setelah jendela “Project1 - Form1(Code)” terbuka copy semua listing code di bawah ini kemudian paste-kan di sana

 

Option Explicit

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Const GWL_STYLE As Long = (-16&)
Const GWL_EXSTYLE As Long = (-20&)
Const WS_THICKFRAME As Long = &H40000
Const WS_MINIMIZEBOX As Long = &H20000
Const WS_MAXIMIZEBOX As Long = &H10000
Const WS_EX_TOOLWINDOW As Long = &H80&

Dim WithEvents cmdClickHere As VB.CommandButton
Dim WithEvents lblName As VB.Label
Dim WithEvents txtName As VB.TextBox
Dim WithEvents cmdExit As VB.CommandButton

Private Sub cmdClickHere_Click()
If cmdClickHere.Caption = "Click Here" Then
MsgBox "Hello, " + txtName.Text, vbInformation, "Information"
Me.Caption = "Hello, " + txtName.Text
cmdClickHere.Caption = "New"
ElseIf cmdClickHere.Caption = "New" Then
txtName.Text = ""
txtName.SetFocus
cmdClickHere.Caption = "Click Here"
Me.Caption = "Hello World"
End If
End Sub

Private Sub cmdExit_Click()
Dim Response As Integer

Response = MsgBox("Close the program", vbQuestion + vbYesNo, "Confirmation")

If Response = vbYes Then
End
End If
End Sub

Private Sub Form_Load()
With Me
.Caption = "Hello World"
.Top = (Screen.Height - .Height) / 2
.Left = (Screen.Width - .Width) / 2
End With

Call SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) Xor (WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX))

Set cmdClickHere = Controls.Add("VB.CommandButton", "cmdClickHere", Form1)
With cmdClickHere
.Move Me.ScaleLeft + 1100, Me.ScaleTop + 1400, .Width + 1300, .Height
.Visible = True
.Caption = "Click Here"
.TabIndex = 2
.Enabled = False
End With

Set lblName = Controls.Add("VB.Label", "lblName", Form1)
With lblName
.Move Me.ScaleLeft + 1100, Me.ScaleTop + 600
.Visible = True
.Caption = "Type Your Name:"
.AutoSize = True
.TabIndex = 0
End With

Set txtName = Controls.Add("VB.TextBox", "txtName", Form1)
With txtName
.Move Me.ScaleLeft + 1100, Me.ScaleTop + 860, .Width + 1300, .Height - 50
.Visible = True
.Text = ""
.FontSize = 12
.TabIndex = 1
End With

Set cmdExit = Controls.Add("VB.CommandButton", "cmdExit", Form1)
With cmdExit
.Move Me.ScaleLeft + 1100, Me.ScaleTop + 2000, .Width + 1300, .Height
.Visible = True
.Caption = "E&xit"
.TabIndex = 3
End With
End Sub

Private Sub txtName_Change()
cmdClickHere.Enabled = True

If txtName.Text = "" Then
cmdClickHere.Enabled = False
End If
End Sub

Private Sub txtName_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then cmdClickHere_Click
End Sub


5. Pilih tombol “Start” pada “Command Bar” atau pada “Menu Bar” pilih “Run” kemudian pilih “Start” atau teman-teman bisa langsung menekan tombol “F5” pada keyboard


Start Run


Label:

0 Komentar:

Posting Komentar

Berlangganan Posting Komentar [Atom]

<< Beranda