VB.net编程中可能用到的边边角角(二) —— 取Mac地址、取某String的MD5值、多线程、RAR压缩、ADOX生成数据库、加载工程内的Form、一个滚动条控制两个DataGridView
1.取Mac地址
Function getMacAddress()
Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
Return nics(0).GetPhysicalAddress.ToString
End Function
2.取某String的MD5值
Function GetMD5(ByVal input As String) As String
Dim md5Hasher As MD5 = MD5.Create()
Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))
Dim sBuilder As String = String.Empty
Dim i As Integer
For i = 0 To data.Length - 1
sBuilder = sBuilder + data(i).ToString("x2") ’32位MD5
Next i
sBuilder = sBuilder.Substring(8, 16).ToUpper() ’16位MD5(就是32位MD5中间的16位。。。)
Return sBuilder
End Function
3.多线程的小示例
Public Sub Method1()
Dim i As Integer
For i = 1 To 100
MessageBox.Show("这是类AClass方法method1的内容" + i.ToString())
Next
End Sub
Public Sub Method2()
Dim i As Integer
For i = 1 To 100
MessageBox.Show("这是类AClass方法method2的内容" + i.ToString())
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As New Form13
Dim th1, th2 As Thread
th1 = New Thread(New ThreadStart(AddressOf obj.Method1))
th1.Start()
th2 = New Thread(New ThreadStart(AddressOf obj.Method2))
th2.Start()
Dim i As Integer
For i = 1 To 100
MessageBox.Show("Main方法里的内容" + i.ToString())
Next
End Sub
4.使用RAR压缩文件
Shell("D:\Program Files\WinRAR\WinRAR.exe a -r c:\456.rar c:\tips.txt") ’a之前的部分为RAR安装路径,最后的tips.txt为需要压缩的文件,中间的456.rar为压缩的路径
'Shell("D:\Program Files\WinRAR\WinRAR.exe a -p123456-r c:\456.rar c:\tips.txt") ’-p123456意为密码123456,但是我输入密码不好用,可能是日文系统的原因,具体原因待查。
5.使用ADOX生成数据库及表
Try
Dim tableName As String = "GenuineValidation"
Dim catalog As New ADOX.Catalog
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\" + tableName + ".mdb;Jet OLEDB:Engine Type=5") ’生成的数据库路径
Dim conn As New ADODB.Connection
conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\" + tableName + ".mdb", "", "", -1)
catalog.ActiveConnection = conn
Dim table As New ADOX.Table
table.Name = "GenuineValidation"
Dim column As New ADOX.Column ’可以先New一个列,编辑好了再添加
column.ParentCatalog = catalog
column.Name = "MACMD516" ’列名
'column.Type = DataTypeEnum.adChar ’Access添加这个出错,不添加默认为Text型
column.DefinedSize = 19 ’长度
table.Columns.Append(column, , 19)
table.Keys.Append("PK_GV" , KeyTypeEnum.adKeyPrimary, column, "", "") ’在刚才加的列上添加主键
table.Columns.Append("GenuineCode", , 20) ’直接添加一个列,长度20的文本型
'table.Columns.Append("GenuineCode", DataTypeEnum.adVarChar, 20) ’类型为VarChar也出错,同样不填默认Text型
table.Columns.Append("GenuineCode2", DataTypeEnum.adDate) ’日期型
catalog.Tables.Append(table)
conn.Close()
Catch ex As Exception
End Try
6.加载工程内的Form
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim f4 As Form4 = New Form4()
f4.FormBorderStyle = Windows.Forms.FormBorderStyle.None ’去掉Form4的边框,否则占地方且奇丑无比。。。
f4.Dock = DockStyle.Fill
f4.TopLevel = False
Me.Panel1.Controls.Clear()
Me.Panel1.Controls.Add(f4)
f4.Show()
End Sub
7.一个滚动条控制两个DataGridView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
HScrollBar1.Minimum = 0
HScrollBar1.Maximum = DataGridView1.Columns.Count + DataGridView1.DisplayedColumnCount(False)
HScrollBar1.SmallChange = DataGridView1.DisplayedColumnCount(False) + 1 ’点在滚动条箭头上的变化幅度
HScrollBar1.LargeChange = DataGridView1.DisplayedColumnCount(False) + 2 ’点在滚动条空白处的变化幅度
End Sub
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
DataGridView1.FirstDisplayedScrollingColumnIndex = e.NewValue
DataGridView2.FirstDisplayedScrollingColumnIndex = e.NewValue
Application.DoEvents()
End Sub
HScrollBar1是从控件里拖出来的,DataGridView1,DataGridView2的ScrollBar可以去掉。苦B的我以为是操作DataGridView1中的ScrollBar,浪费了不少时间。。。