VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > 批处理教程 >
  • vb.net教程之程序对话框

程序对话框(相关vb.net教程)
有关对话框的一组控件:
打开文件对话框openfiledialog:
保存文件对话框savefiledialog:
颜色对话框colordialog:
字体对话框fontdialog:
常用属性:
Name:控件名。
Filename:对话框中所选择的文件名
Filenames:允许选择多个文件时,所选择的文件名数组。当multiselect为真时才可用。
Multiselect:弹出的选择文件对话框中,允许不允许选择多个。
Filter:显示的文件类型,它由“说明|*.类型”构成,一般放在窗体的load事件中。
DefaultExt:保存文件时默认扩展名,字符串,一般放在窗体的load事件中。
Color:对话框选定的颜色。
Font: 对话框选定的字体。
常用方法:
Showdialog:用以弹出对话框。
应用实例:制作一个图片浏览器
见,图片浏览器1
程序为:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ss As String
        Op.ShowDialog()
        ss = Op.FileName
        PictureBox1.Image = Image.FromFile(ss)
 
    End Sub
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Op.Filter = "*.jpg|*.jpg|*.bmp|*.bmp"
    End Sub
应用实例:制作一个可同时打开多个文件的图片浏览器,并能进行上下翻阅浏览。
见,可上下翻阅的图片浏览器,程序为:
Public Class Form1
    Dim i As Integer
    Dim j As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OpenF.Filter = "jpg file|*.jpg|bmp file|*.bmp|gif file|*.gif"
        'OpenF.DefaultExt = "bmp"
        OpenF.Multiselect = True
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenF.ShowDialog()
        PictureBox1.Image = Image.FromFile(OpenF.FileName)
        i = UBound(OpenF.FileNames)
        MsgBox("选择的文件数为:" & Str(i + 1), 32, "提示")
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        j = j + 1
        If j >= i Then
            j = i
            Button2.Enabled = False
            Button3.Enabled = True
        End If
        PictureBox1.Image = Image.FromFile(OpenF.FileNames(j))
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        j = j - 1
        If j <= 0 Then
            j = 0
            Button3.Enabled = False
            Button2.Enabled = True
        End If
        PictureBox1.Image = Image.FromFile(OpenF.FileNames(j))
    End Sub
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        End
    End Sub
End Class
应用实例:通过颜色,字体对话框设置文本框中的字体,字号,字的颜色。
见,通用对话框综合实例[实验报告33 ]
程序为:
Public Class Form1
    Dim ss As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        o.ShowDialog()
        ss = o.FileName
        P.Image = Image.FromFile(ss)
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        C.ShowDialog()
        L.ForeColor = C.Color
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        F.ShowDialog()
        L.Font = F.Font
    End Sub
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        End
    End Sub
 
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        S.ShowDialog()
        ss = S.FileName
        ss = MsgBox("如果要真正的保存一个文件," & Chr(13) & Chr(13) & "还需要文件操作控件", 16, "注意!")
    End Sub
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        S.Filter = ("文本文件(*.txt)|*.txt")
        S.DefaultExt = "txt"
        o.Filter = ("图形文件*.jpg|*.jpg|*.bmp|*.bmp")
        o.DefaultExt = "jpg"
    End Sub
End Class
练习:试开发一个记事本软件。
见,小小记事本,程序为:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ColorDialog1.ShowDialog()
        TextBox1.ForeColor = ColorDialog1.Color
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        FontDialog1.ShowDialog()
        TextBox1.Font = FontDialog1.Font
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim n As Integer
        Dim f As String
        SaveFileDialog1.ShowDialog()
        f = SaveFileDialog1.FileName
        n = FreeFile()
        FileOpen(n, f, OpenMode.Output)
        Print(n, TextBox1.Text)
        FileClose(n)
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OpenFileDialog1.Filter = "文本文件|*.txt"
        SaveFileDialog1.Filter = "文本文件|*.txt"
        OpenFileDialog1.DefaultExt = "txt"
        SaveFileDialog1.DefaultExt = "txt"
    End Sub
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim fn As Integer
        Dim s As String = ""
        OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            fn = FreeFile()
            FileOpen(fn, OpenFileDialog1.FileName, OpenMode.Input)
            Do While Not EOF(fn)
                s = s + LineInput(fn) + vbCrLf
            Loop
            FileClose(fn)
            TextBox1.Text = s
        End If
    End Sub
 
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        End
End Sub

相关教程