A whole program, practise in Swedish words - Download
With the Common Dialog-object, you can simply use the standard Open and Save As popup windows.
In this example, create these objects on your form:
Object
Type
Name of the object
1 Common Dialog Box CommonDialog1
1
DriveListBox
Drive1
2 DirListBox Dir1,
Dir2
1 FileListBox
File1
3 Listbox Listbox1, Listbox2, Listbox3
2 Command-buttons Command1, Command2.
The code allows you to open a textfile by clicking Command1-button, put the file name in Listbox1 and the contents of the file in Listbox2 & 3. It also allows you to save the contents of ListBox2 & 3 by selecting an object in ListBox1 and press the Command2-button.
Private Sub Form_Load()
Dir1.Visible = False
' Hide
Drive1.Visible = False
' these
Dir2.Visible = False
' items
File1.Visible = False
'
Form1.Visible = True
Command1.Caption =
"Load"
' Set names on
Command2.Caption =
"Save"
' these buttons
End Sub
Private Sub Command1_Click()
CommonDialog1.ShowOpen
' Open CommonDialog1 in mode Open
CommonDialog1.Filter = "*.txt|*.txt"
' Filter the files abled to be opened
Listbox1.AddItem CommonDialog1.FileTitle
' Add the name of the file to Listbox1
Open Commondialog1.Filename For Input As #1
' Open the file selected in the Commondialogbox for input
Dim s, e
Do Until EOF(1)
' Do until End Of File
Input #1, s, e
ListBox2.AddItem s
' Add the text to ListBox2
ListBox3.AddItem e
' and Listbox3
Loop
Close #1
End Sub
Private Sub Command2_Click()
CommonDialog1.Filter = "*.txt|*.txt"
CommonDialog1.ShowSave
' Open CommonDialog1 in mode Save As
Dim i, l
For i = 0 To ListBox1.ListCount - 1
' Loop the number of entrys in ListBox1
If ListBox1.Selected(i) = True Then
' Is this the selected value?
Open CommonDialog1.FileName For Output As #1
'Open the filename selected in ListBox1 or CommonDialog1 for output
For l = 0 To
ListBox2.ListCount - 1
' Loop the number of entrys in ListBox2
Write #1, ListBox2.List(l), ListBox3.List(l) ' Write
entrys from ListBox2 & 3 to the file
Next
Close #1
End If
Next
End Sub
It's very simple to add menus to your program; just use the Menu Editor from the Tools-menu to add items to it.
This example simple creates a File-menu:
In the Menu Editor-window, type the Captionname (as it'll be shown in the menu, & before a letter creates shortcut letters) and the objectname of the text (for example mnuFile).
&File
'Shown as File
....&New 'Shown clicking on
File as New
....&Open 'Shown clicking on File
as Open
....&Close 'Shown clicking on File
as Close
....E&xit 'Shown clicking on
File as Exit
For every submenu you want to create, press the right-arrow sign to move it to the right (indicated by four dots in front of the caption name).
Creating actions for the Menu Items
In the Form, press the Open-item and a new Private Sub will be created:
Private Sub(mnuFileOpen_Click()
End Sub
Inside this procedure, you could put, for example the same code as in Command1_Click() procedure.