Selasa, 13 September 2011

Pembuatan Conrol Array Visual Basic 2008


Microsoft Visual Basic atau dikenal dengan VB, merupakan salah satu perangkat lunak (software) yang digunakan untuk pengembangan software (software development), dimana dengannya kita dapat membuat aplikasi-aplikasi sederhana bahkan profesional. Versi yang telah ada dari Microsoft Visual Basic adalah Visual Basic 9 atau disebut juga Visual Basic 2008. Pada versi ini kita dihadapkan pada tampilan grafis dan lingkungan IDE (Integrated Development Environment) yang sangat berbeda dari versi sebelumnya.

Sebagaimana judul tulisan ini, sebelum kita melakukan uji coba softwarenya alangkah baik jika kita mengetahui terlebih dahulu hal-hal apa saja yang fitur-fitur yang ada  bersama dengan hadirnya Visual Basic 2008 ini, dan satu hal lagi dalam perbandingannya nanti saya lebih membandingkannya dengan versi 6. Karena selain telah familier juga karena kepopuleran dari versi tersebut, melihat hal-hal apa yang ada (feature) dari versi 9.0 ini, adalah sebagai berikut :
- Support for Data
- Language Feature
- Project Designer Sopport for Windows Presentation Foundation (WPF)      Applications
- Windows Communication Foundation (WCF)
- Improvements to IDE
- ClickOnce Deployment

Menuju pada judul tulisan saya, Control array dari visual Basic 6 berbeda dengan Visual Basic 2008, jika anda menggunakan Visual Basic 6 anda tinggal copy-paste maka control tersebut anda bisa jadikan dengan control array, tetapi tidak sama dengan Visual Basic 2008. Jika anda menggunaka Visual Basic 2008 control array harus dideklarasikan pada Class.

Langsung saja kita masuk ke Visual Basic 2008 pada Start Page pilih -> Create Project pada Tamplate pilih -> Windows Forms Application lalu diberi nama “CreateCntrlArray”. Lalu tambahkan dua Class yaitu denga cara pada window Solution Explorer pada Project File “CreateCntrlArray” anda Klik kanan pilih -> Add -> pilih Class.
Disini saya menggunaka Class dengan nama : ClsTxtBox.vb dan ClsTxtBox2.vb, dan satu User Control yaitu : CtrlDetil.vb.
Untuk menambahkan User Control yaitu denga cara pada window Solution Explorer pada Project File “CreateCntrlArray” anda Klik kanan pilih -> Add -> pilih User Control kemudian anda beri nama “CtrlDetil”.

juga satu Form yaitu : Form1.vb.

Berikut adalah Script dari View Code ClsTxtBox.vb

Public Class ClsTxtBox
    Inherits System.Collections.CollectionBase
    Private mHost As UserControl
    Private mHostName As String
    ' Visual Basic
    Private ReadOnly HostForm As System.Windows.Forms.UserControl
    ' Visual Basic
    Public Sub New(ByVal host As System.Windows.Forms.UserControl)
        HostForm = host
        Me.AddTextbox()
    End Sub
    ' Visual Basic
    ' Declare Property Item
    Default Public ReadOnly Property Item(ByVal Index As Integer) As  _
       System.Windows.Forms.TextBox
        Get
        Return CType(Me.List.Item(Index), System.Windows.Forms.TextBox)
        End Get
    End Property

   
    ' Visual Basic
    ' Declare Property UBound
    Public ReadOnly Property UBound() As Integer
        Get
            'Collection Index is Zero-Based
            'UpperBound = Count of ListItems - 1
            Return (list.Count - 1)
        End Get
    End Property
    ' Visual Basic
    ' Function to add Control TextBox
    Public Function AddTextbox() As System.Windows.Forms.TextBox
        ' Create a new instance of the TextBox class.
        Dim aTextbox As New System.Windows.Forms.TextBox()
        ' Add the TextBox to the collection's internal list.
        Me.List.Add(aTextbox)
        ' Add the TextBox to the controls collection of the form
        ' referenced by the HostForm field.
        HostForm.Controls.Add(aTextbox)
        ' Set intial properties for the TextBox object.
        aTextbox.Top = (Count - 1) * 20
        aTextbox.Left = 0
        aTextbox.Width = 80
        aTextbox.TabStop = False
        aTextbox.Tag = Me.Count
        aTextbox.Text = "Textbox " & Me.Count.ToString
        ' Visual Basic
        ' Declare Event TextBox
        AddHandler aTextbox.Click, AddressOf ClickHandler
        AddHandler aTextbox.KeyDown, AddressOf KeyDownHandler
        Return aTextbox
    End Function
    ' Visual Basic
    ' Function to Remove Control TextBox
    Public Sub Remove()
        ' Check to be sure there is a Label to remove.
        If Me.Count > 0 Then
        ' Remove the last TextBox added to the array from the host form
        ' controls collection. Note the use of the default property in
        ' accessing the array.
            HostForm.Controls.Remove(Me(Me.Count - 1))
            Me.List.RemoveAt(Me.Count - 1)
        End If
    End Sub
    ' Visual Basic Event Click
    ' Address of Event Click
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As  _
       System.EventArgs)
        MessageBox.Show("you have clicked Textbox " & _
CType(CType(sender,  _
           System.Windows.Forms.TextBox).Tag, String))
    End Sub
    ' Visual Basic Event KeyDown
    ' Address of Event KeyDown
    Private Sub KeyDownHandler(ByVal sender As Object, ByVal e As _  System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            CtrlDetil.MyTB2(CType(CType(sender,  _
               System.Windows.Forms.TextBox).Tag, String) - 1).Focus()
        End If
    End Sub
End Class

Lalu kemudian adalah Script dari View Code ClsTxtBox2.vb

Public Class ClsTxtBox2
    Inherits System.Collections.CollectionBase
    Private mHost As UserControl
    Private mHostName As String
    ' Visual Basic
    Private ReadOnly HostForm As System.Windows.Forms.UserControl
    ' Visual Basic
    Public Sub New(ByVal host As System.Windows.Forms.UserControl)
        HostForm = host
        Me.AddTextbox()
    End Sub
    ' Visual Basic
    Default Public ReadOnly Property Item(ByVal Index As Integer) As  _
       System.Windows.Forms.TextBox
        Get
Return CType(Me.List.Item(Index), _ System.Windows.Forms.TextBox)
        End Get
    End Property

    Public ReadOnly Property UBound() As Integer
        Get
            'Collection Index is Zero-Based
            'UpperBound = Count of ListItems - 1
            Return (list.Count - 1)
        End Get
    End Property

    ' Visual Basic
    ' Function to add Control TextBox2
    Public Function AddTextbox() As System.Windows.Forms.TextBox
        ' Create a new instance of the TextBox class.
        Dim aTextbox As New System.Windows.Forms.TextBox()
        ' Add the TextBox to the collection's internal list.
        Me.List.Add(aTextbox)
        ' Add the TextBox to the controls collection of the form
        ' referenced by the HostForm field.
        HostForm.Controls.Add(aTextbox)
        ' Set intial properties for the TextBox object.
        aTextbox.Top = (Count - 1) * 20
        aTextbox.Left = 80
        aTextbox.Width = 100
        aTextbox.TabStop = False
        aTextbox.Tag = Me.Count
        aTextbox.Text = "Textbox2 " & Me.Count.ToString
        ' Visual Basic
        ' Declare Event TextBox
        AddHandler aTextbox.Click, AddressOf ClickHandler
        AddHandler aTextbox.KeyDown, AddressOf KeyDownHandler
        Return aTextbox
    End Function
    ' Visual Basic
    ' Function to Remove Control TextBox2
    Public Sub Remove()
        ' Check to be sure there is a Label to remove.
        If Me.Count > 0 Then
        ' Remove the last TextBox added to the array from the host form
        ' controls collection. Note the use of the default property in
        ' accessing the array.
            HostForm.Controls.Remove(Me(Me.Count - 1))
            Me.List.RemoveAt(Me.Count - 1)
        End If
    End Sub
    ' Visual Basic Event Click
    ' Address of Event Click
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As  _
       System.EventArgs)
        MessageBox.Show("you have clicked Textbox2 " & CType(CType(sender,  _
           System.Windows.Forms.TextBox).Tag, String))
    End Sub
    ' Visual Basic Event KeyDown
    ' Address of Event KeyDown
    Private Sub KeyDownHandler(ByVal sender As Object, ByVal e As _ System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            CtrlDetil.MyTB(CType(CType(sender,  _
               System.Windows.Forms.TextBox).Tag, String) - 1).Focus()
        End If
    End Sub
End Class


Gambar dari User Control View Designer CtrlDetil.vb :
CtrlDetil Designer.





















Berikutnya Script dari View Code User Control : CtrlDetil.vb

Public Class CtrlDetil

    Public Shared MyTB As ClsTxtBox
    Public Shared MyTB2 As ClsTxtBox2
    Dim mOwner As Form1

#Region " Windows Form Designer generated code "
    Public Sub New(ByVal owner As Form1, _
                   ByVal myX As Integer, ByVal myY As Integer)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        mOwner = owner
        Me.Top = myY
        Me.Left = myX
        MyTB = New ClsTxtBox(Me)
        MyTB2 = New ClsTxtBox2(Me)
    End Sub
#End Region
End Class


Gambar dibawah adalah Form1 masih dalam format Designer.


Form1 Designer.






















Control-control yang perlu ditambahkan pada Form1 adalah :

-Button:
      * Button1 ->  Name      : btnLoad
                    Text       : Load
                    Enabled    : True
      * Button2 ->  Name      : btnUnLoad
                    Text       : UnLoad
                    Enabled    : False
-Label:
      * Label1 ->   Name : Label1
                    Text  : -
      * Label2 ->   Name : Label2
                    Text  : UBound :
      * Label3 ->   Name : Label3
                    Text  : Count :
      * Label4 ->   Name : Label4
                    Text  : -



Script View Code Form1.vb :

Imports CreateCntrlArray.CtrlDetil
Imports Microsoft.VisualBasic
Public Class Form1
    Dim Ctl As New CtrlDetil(Me, 0, 0)
    Dim mUbound As Long = 0

    Private Sub ChangeCtrl()
        Label1.Text = Trim(Str(MyTB.UBound))
        Label4.Text = Trim(Str(MyTB2.UBound + 1))
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MyBase.Load
        Me.Controls.Add(Ctl)
  Ctl.AutoScroll = True
        Ctl.Width = 208
        Ctl.Height = 279
        Ctl.BackColor = Color.LightSteelBlue
        Me.StartPosition = FormStartPosition.CenterScreen
        ChangeCtrl()
    End Sub

  Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnLoad.Click
        MyTB.AddTextbox()
        MyTB2.AddTextbox()
        btnUnLoad.Enabled = True
        ChangeCtrl()
  End Sub

  Private Sub btnUnLoad_Click(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles btnUnLoad.Click
        MyTB.Remove()
        MyTB2.Remove()
        If MyTB.UBound = 0 Then btnUnLoad.Enabled = False
        ChangeCtrl()
  End Sub
End Class

Dan jika Project dijalankan dengan tekan F5. maka hasilnya adalah seperti berikut:
Form1 Run (tekan F5).






















Dan jika anda menekan Button Load makan Control TextBox dan TextBox2 akan bertambah kebawah seperti ini :

Form1 setelah tekan Button Load.























Untuk menghapus Control makan tekan saja Button UnLoad. OK ternyata mudahkan… Semoga berhasil… Terimakasih.

Tidak ada komentar:

Posting Komentar