杰斯的全金属狂潮:vb制作保存对话框要哪些代码

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/28 04:28:17
还有对话框这个部件的名字叫啥来着?如果我要保存TEXT中的文字,保存格式为*.txt,代码怎样写?

用Common dialog 控件(即COMDLG32.OCX),在VB界面里按Ctrl+T调出控件选择,把"Microsoft Common Dialog Control 6.0"选上。
一个代码的例子(把以下代码保存为frmMain.frm,然后用VB打开。):

VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form frmMain
Caption = "Save Text"
ClientHeight = 3150
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3150
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton cmdOpen
Caption = "打开"
Height = 495
Left = 3240
TabIndex = 3
Top = 110
Width = 1215
End
Begin VB.CommandButton cmdExit
Caption = "退出"
Height = 495
Left = 3240
TabIndex = 2
Top = 1200
Width = 1215
End
Begin VB.CommandButton cmdSave
Caption = "保存"
Height = 495
Left = 3240
TabIndex = 1
Top = 600
Width = 1215
End
Begin VB.TextBox txtContent
Height = 3135
Left = 0
MultiLine = -1 'True
TabIndex = 0
Top = 0
Width = 3015
End
Begin MSComDlg.CommonDialog CMD
Left = 4080
Top = 1200
_ExtentX = 847
_ExtentY = 847
_Version = 393216
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdExit_Click()
Unload Me

End Sub

Private Sub cmdOpen_Click()
Dim strText As String
CMD.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*"
CMD.DialogTitle = "打开文件"
CMD.ShowOpen
If Len(CMD.FileName) = 0 Then Exit Sub
Open CMD.FileName For Input As #1
txtContent.Text = ""
While Not EOF(1)
Input #1, strText
txtContent.Text = txtContent.Text + strText + vbCrLf
Wend
Close #1
End Sub

Private Sub cmdSave_Click()
CMD.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*"
CMD.DialogTitle = "保存文件"
CMD.ShowSave
If Len(CMD.FileName) = 0 Then Exit Sub
Open CMD.FileName For Output As #1
Write #1, txtContent.Text
Close #1
End Sub

MICROSOFT COMMON Dialog Control 6.0,这个是对话框控件.

右击该控件,就可以设置.