CodeItBetter Programming Another VB Programming Blog

How to Erase an Array — removes all of its elements

Posted on August 25, 2011
1
2
'Arrays, Collections, Lists - How to Erase an Array -- removes all of its elements
Erase MyArray()

How to Download a file from the Internet — without getting a dialog box (in two ways)

Posted on August 24, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'Internet - How to Download a file from the Internet -- without getting a dialog box (in two ways)
'Method 1:

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
    (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
    ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
 
Private Sub Form_Load()
    Dim llRetVal As Long
    llRetVal = URLDownloadToFile(0, "http://www.yahoo.com", "c:\Temp.htm", 0, 0)
End Sub
 
'Method 2:

Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long
 
Public Sub DownloadFile(FileURL As String)
    Dim sDownload As String
 
    sDownload = StrConv(FileURL, vbUnicode)
    Call DoFileDownload(sDownload)
End Sub
Filed under: Internet No Comments

How to Display keyboard key pressed — including system keys

Posted on August 24, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'Controls - How to Display keyboard key pressed -- including system keys
Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, _
    ByVal wMapType As Long) As Long
Public Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, _
    ByVal lbBuffer As String, ByVal nSize As Long) As Long
 
Private Sub txtBody_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim dummy&
    Dim scancode&
    Dim keyname As String * 256
    'Get the scancode
    scancode& = MapVirtualKey(KeyCode, 0)
    dummy = GetKeyNameText(scancode& * &H10000, keyname, 255)
    MsgBox keyname
    KeyCode = 0
End Sub
Filed under: Controls No Comments

How to show Run Dialog

Posted on August 23, 2011
1
2
3
4
5
6
7
8
9
10
11
12
'System & API - How to show Run Dialog
Private Declare Function SHRunDialog Lib "shell32" Alias "#61" (ByVal hwnd As Long, _
    ByVal I_dont_know_1 As Long, ByVal I_dont_know_2 As Long, ByVal dTitle As String, _
    ByVal dPrompt As String, ByVal uFlags As Long) As Long
 
Private Sub Form_Load()
    'No Browse Button. Need to enter the program path manually
    'SHRunDialog hwnd, 0, 0, "This is Run dialog", "Please enter the program you wish to run ", 1
    'Normal Run Dialog
    SHRunDialog hwnd, 0, 0, "This is Run dialog", "Please enter the program you wish to run ", 2
    Unload Me
End Sub
Filed under: System & API No Comments

How to Enable/Disable Close Button in a form

Posted on August 23, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'Forms - How to Enable/Disable Close Button in a form
Option Explicit
 
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, _
    ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, _
    ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
 
Private Const MF_BYCOMMAND = &H0&
Private Const SC_CLOSE = &HF060&
 
Public Sub SetXState(frm As Form, blnState As Boolean)
    Dim hMenu As Long
    hMenu = GetSystemMenu(frm.hwnd, blnState)
    Call RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND)
    Call DrawMenuBar(frm.hwnd)
End Sub
 
Private Sub Command1_Click()
    'To Disable X button in a form
    Call SetXState(Me, False)
 
    'To Enable X button in a form
    Call SetXState(Me, True)
End Sub
Filed under: Forms 1 Comment