vb.net2005资料收集(系统篇)

来源:互联网 发布:移动网络接入点名称 编辑:程序博客网 时间:2024/04/19 17:15

一、浏览文件夹,并选择文件

Imports System.IO
Public Class Form1
    
'浏览文件夹
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
Me.ListBox1.Items.Clear()
        
Dim MyDlg As New FolderBrowserDialog()
        
If (MyDlg.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
            
Me.TextBox1.Text = MyDlg.SelectedPath
            
Dim i As Integer = 0
            
Dim MyFilter As String = ""
            MyFilter 
= "*.jpg"
            
For i = 0 To System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter).Length - 1 Step 1
                
Me.ListBox1.Items.Add(System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter)(i))
            
Next
            MyFilter 
= "*.gif"
            
For i = 0 To System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter).Length - 1 Step 1
                
Me.ListBox1.Items.Add(System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter)(i))
            
Next
            MyFilter 
= "*.bmp"
            
For i = 0 To System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter).Length - 1 Step 1
                
Me.ListBox1.Items.Add(System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter)(i))
            
Next
            MyFilter 
= "*.tif"
            
For i = 0 To System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter).Length - 1 Step 1
                
Me.ListBox1.Items.Add(System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter)(i))
            
Next
            MyFilter 
= "*.png"
            
For i = 0 To System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter).Length - 1 Step 1
                
Me.ListBox1.Items.Add(System.IO.Directory.GetFiles(MyDlg.SelectedPath, MyFilter)(i))
            
Next
        
End If
    
End Sub

    
'显示图像
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        
Dim MyFileName As String = Me.ListBox1.SelectedItem.ToString()
        
Me.PictureBox1.Image = System.Drawing.Bitmap.FromFile(MyFileName)
        
Dim Tlg As New FolderBrowserDialog
        
If Tlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            
Me.TextBox1.Text = Tlg.SelectedPath
            
Dim s As Integer = 0
            
Dim tFilter As String = ""
            tFilter 
= "*.jpg"
            
For s = 0 To Directory.GetFiles(Tlg.SelectedPath, tFilter).Length - 1 Step 1
                
Me.ListBox1.Items.Add(Directory.GetFiles(Tlg.SelectedPath, tFilter)(s))
            
Next

        
End If
    
End Sub

End Class

 

    '浏览文件夹,并把文件夹下的所有的文件填充到ListBox中
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
Dim MyDlg As New FolderBrowserDialog()
        
If MyDlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            
Me.ListBox1.Items.Clear()
            
Me.TextBox1.Text = MyDlg.SelectedPath
            
Dim MyFiles() As String = System.IO.Directory.GetFiles(Me.TextBox1.Text)
            
For Each MyFile As String In MyFiles
                
Me.ListBox1.Items.Add(MyFile)
            
Next
        
End If
    
End Sub

 

二、设置字体

Public Class Form1
    
'浏览文本文件
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        
Dim MyDlg As New OpenFileDialog()
        MyDlg.CheckFileExists 
= True
        MyDlg.Filter 
= "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
        
If MyDlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            
Me.RichTextBox1.LoadFile(MyDlg.FileName, RichTextBoxStreamType.PlainText)
        
End If
    
End Sub

    
'宋体1号
    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        
Me.RichTextBox1.Font = New System.Drawing.Font("宋体"26)
    
End Sub

    
'宋体5号
    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        
Me.RichTextBox1.Font = New System.Drawing.Font("宋体"10.5)
    
End Sub

    
'楷体1号
    Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click
        
Me.RichTextBox1.Font = New System.Drawing.Font("楷体_GB2312"26)
    
End Sub

    
'楷体5号
    Private Sub ToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem4.Click
        
Me.RichTextBox1.Font = New System.Drawing.Font("楷体_GB2312"10.5)
    
End Sub

End Class

 

三、多线程:如何从子线程更新主线程数据

Imports System.Threading
Public Class Form1
    
Public MyThread As Thread
    
'从子线程更新进度条数据
    Public Sub UpdateProgress()
        
For i As Integer = 1 To 100 Step 1
            Thread.Sleep(
50)
            
Dim MyValue As Integer = i
            
Me.ProgressBar1.Value = MyValue
            
Me.Label1.Text = "当前模拟执行进度完成百分比:" + MyValue.ToString() + "%"
        
Next
    
End Sub

    
'启动显示执行进度
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
Dim MyDelegate As New ThreadStart(AddressOf UpdateProgress)
        
Dim MyThread As New Thread(MyDelegate)
        MyThread.Start()
    
End Sub

End Class

 

四、如何使用Stopwatch实现高精度计时

dim MyWatch as new system.diagnostics.stopwatch
mywatch.start
…………
mywatch.stop
Textbox2.text
=mywatch.elapsedmilliseconds.tostring+"毫秒"

 

五、在程序界面执行DOS命令

Imports System.Diagnostics
Public Class Form1
    
'执行DOS命令
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
Dim MyProcess As New Process()
        
'设定程序名
        MyProcess.StartInfo.FileName = "cmd.exe"
        
'关闭Shell的使用
        MyProcess.StartInfo.UseShellExecute = False
        
'重定向标准输入
        MyProcess.StartInfo.RedirectStandardInput = True
        
'重定向标准输出
        MyProcess.StartInfo.RedirectStandardOutput = True
        
'重定向错误输出
        MyProcess.StartInfo.RedirectStandardError = True
        
'设置不显示窗口
        MyProcess.StartInfo.CreateNoWindow = True
        
'执行DOS命令
        MyProcess.Start()
        MyProcess.StandardInput.WriteLine(
Me.TextBox1.Text)
        MyProcess.StandardInput.WriteLine(
"exit")
        
'从输出流获取命令执行结果,
        Dim MyInfo As String = MyProcess.StandardOutput.ReadToEnd()
        
Me.RichTextBox1.Text = MyInfo
    
End Sub

End Class
原创粉丝点击