BenEskew.com Just another web developer's personal weblog.

11Sep/090

Visual Basic Code Snippets #10

In this next update I'll re-post my Visual Basic code snippets (which are older than five years by the way) that pertain to Window Actions and File Functions.

Minimize All Active Windows

Compatibility: Win. 98-XP

Add the following code into a module within your project...

Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Const VK_LWIN = &H5B
Public Const KEYEVENTF_KEYUP = &H2

Add the following code into a command button or wherever you want the code to execute...

Call keybd_event(VK_LWIN, 0, 0, 0)
Call keybd_event(&H4D, 0, 0, 0)
Call keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)

Load a Text Box with Items from a Text File

Compatibility: Win. 98-XP

Add the following code into a module within your project...

Public Sub LoadText(Lst As TextBox, file As String)
'Call LoadText (Text1,"C:\Windows\System\Saved.txt")
On Error GoTo error
Dim mystr As String
Open file For Input As #1
Do While Not EOF(1)
Line Input #1, a$
texto$ = texto$ + a$ + Chr$(13) + Chr$(10)
Loop
Lst = texto$
Close #1
Exit Sub
error:
X = MsgBox("File Not Found", vbOKOnly, "Error")
End Sub

Add a TextBox named Text1 and make sure there is a text file named test.txt with "test" inside it inside the directory where you are working on your project and add the following code wherever you want the code to execute...

Call LoadText(Text1, "test.txt")

Save Data to a Text File

Compatibility: Win. 98-XP

Add the following code into a module within your project...

Public Sub SaveText(Lst As TextBox, file As String)
'Call SaveText (Text1,"C:\Windows\System\Saved.txt")
On Error GoTo error
Dim mystr As String
Open file For Output As #1
Print #1, Lst
Close 1
Exit Sub
error:
X = MsgBox("There has been an error!", vbOKOnly, "Error")
End Sub

Add a TextBox named Text1 and add the following code wherever you want the code to execute...

Call SaveText(Text1, "C:\Program Files\Program Name\test.txt")

Visual Basic Code Snippets #11