Okay, langsung saja saya akan menampilkan secara keseluruhan kode tersebut:
Untuk Formnya:
Option Explicit
Private Sub Form_Load()
MsgBox "Kunjungi saya di www.tutorial-vb.com", vbOKOnly, " ^,..,^"
Dim intStateFileNbr As Integer
Dim strBS As String
Dim strStateAbbrev As String
Dim strStateName As String
strBS = IIf(Right$(App.Path, 1) = "\", "", "\")
intStateFileNbr = FreeFile
Open (App.Path & strBS & "STATES.DAT") For Input As #intStateFileNbr
Do Until EOF(intStateFileNbr)
Input #intStateFileNbr, strStateAbbrev, strStateName
'Dibawah ini contohnya adalah AK-ALASKA
'memberikan kesan ada pembatas pada variabel file tersebut berupa garis minus (-)
Combo1.AddItem strStateAbbrev & " - " & strStateName
Loop
Close #intStateFileNbr
Combo1.ListIndex = 0
SetDropDownWidth Combo1
End Sub
Untuk Modulenya:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const CB_SETDROPPEDWIDTH As Long = &H160
Public Sub SetDropDownWidth(mCombo As ComboBox)
Dim RetVal As Long
Dim PixelWidth As Long
Dim MaxWidth As Long
Dim LoopCounter As Long
Dim lWidth As Long
For LoopCounter = 0 To mCombo.ListCount - 1
lWidth = mCombo.Parent.TextWidth(mCombo.List(LoopCounter))
If lWidth > MaxWidth Then
MaxWidth = lWidth
End If
Next LoopCounter
MaxWidth = MaxWidth + (23 * Screen.TwipsPerPixelX)
If MaxWidth > (mCombo.Width * 2) Then
MaxWidth = (mCombo.Width * 2)
ElseIf MaxWidth < mCombo.Width Then
MaxWidth = mCombo.Width
End If
PixelWidth = (MaxWidth \ Screen.TwipsPerPixelX)
RetVal = SendMessage(mCombo.hwnd, CB_SETDROPPEDWIDTH, PixelWidth, 0)
End Sub
Kamu bisa mendownload program tersebut dengan langsung klik Sample Membuat State dengan VB
Baiklah sekian dari saya. Kalau kalian suka dengan artikel ini, like dengan FB. Jika kalian masih kurang mengerti, silahkan bertanya dicomment postingan ini. Program ini dibuat dengan Visual Basic di www.tutorial-vb.com