工作之后收集了一些小的代码段,上传上来供大家搜索,也方便自己查找.
1.正则表达式单个字符统计方法Dim str As String = "fkdsajfl甘jdsa甘kfjdsflaf" Dim reg As New Text.RegularExpressions.Regex("甘") MsgBox(reg.Matches(str).Count)
2.隐藏TabControl标签
Private Sub frmPersonSetting_Resize(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Resize Me.TabControl1.Region = New Region(New RectangleF(Me.Tabpage1.Left,Me.Tabpage1.Top,Me.Tabpage1.Width,Me.Tabpage1.Height)) End Sub
3.FTP数据上传
Try Dim client As New Net.WebClient client.Credentials = New Net.NetworkCredential(Str_Username,Str_Password) client.UploadFile(Str_Path,Str_Filename) Catch ex As Exception MsgBox(ex.ToString) End Try
4.用读文本的方式导出Excel(这种方法速度快)
Public Sub ExportToExcel(ByVal dgv As System.Windows.Forms.DataGridView) Dim str As String = "" Dim tempstr As String = "" Dim filename As String = "" Dim savefile As New SaveFileDialog savefile.Filter = "Execl files (*.xls)|*.xls" savefile.FilterIndex = 0 savefile.RestoreDirectory = True savefile.Title = "导出到Excel" savefile.ShowDialog() filename = savefile.FileName If filename = "" Then Exit Sub Using sw As StreamWriter = New StreamWriter(filename,False,System.Text.Encoding.GetEncoding(-0)) Try For i As Integer = 0 To dgv.Columns.Count - 1 If i > 0 Then str = str & vbTab End If str = str & dgv.Columns(i).HeaderText.Trim Next sw.WriteLine(str) For j As Integer = 0 To dgv.Rows.Count - 1 tempstr = "" For k As Integer = 0 To dgv.Columns.Count - 1 If k > 0 Then tempstr = tempstr & vbTab End If tempstr = tempstr & dgv.Rows(j).Cells(k).Value.ToString.Trim Next sw.WriteLine(tempstr) Next sw.Close() Catch ex As Exception MsgBox(ex.ToString.Trim) End Try End Using End Sub
5.截屏
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String,ByVal lpDeviceName As String,ByVal lpOutput As String,ByVal lpInitData As Int32) As Int32 Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Integer,ByVal x As Integer,ByVal y As Integer,ByVal nWidth As Integer,ByVal nHeight As Integer,ByVal hSrcDC As Integer,ByVal xSrc As Integer,ByVal ySrc As Integer,ByVal dwRop As Integer) As Integer Private picture As Bitmap = Nothing '以picture作为图片格式的声 Public Sub capture_window() Me.Visible = False Dim capture1 As IntPtr = CreateDC("DISPLAY",Nothing,Nothing) Dim get1 As Graphics = Graphics.FromHdc(capture1) '创建一个新的Graphics对象 picture = New Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,get1) '根据屏幕大小创建一个相同大小的Bitmap Dim get2 As Graphics = Graphics.FromImage(picture) Dim get3 As IntPtr = get1.GetHdc() '获取屏幕的句柄 Dim get4 As IntPtr = get2.GetHdc() '获取位图的句柄 BitBlt(get4,Screen.PrimaryScreen.Bounds.Width,get3,_ 0,13369376) '把当前屏幕复制到位图中 get1.ReleaseHdc(get3) '释放屏幕句柄 get2.ReleaseHdc(get4) '释放位图句柄 picture.Save("CapturePicture.jpg",ImageFormat.Jpeg) MessageBox.Show(" 已经把当前截取屏幕保存到CapturePicture.jpg,检查程序根目录") Me.Visible = True End Sub
6.获取系统字体
Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles MyBase.Load Dim InstalledFont As New System.Drawing.Text.InstalledFontCollection Dim Fonts As System.Drawing.FontFamily() = InstalledFont.Families Me.ComboBox1.DataSource = Fonts Me.ComboBox1.DisplayMember = "Name" End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object,ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged If Me.ComboBox1.SelectedItem Is Nothing Then Exit Sub Me.TextBox1.Font = New Font(CType(Me.ComboBox1.SelectedItem,System.Drawing.FontFamily),20) End Sub
7.遍历注册表 Dim key,subkey,no1,no2,no3,no4,sitekey As Microsoft.Win32.RegistryKey Dim site,valuename As String valuename = "Path" key = Microsoft.Win32.Registry.LocalMachine subkey = key.OpenSubKey("SOFTWARE") no1 = subkey.OpenSubKey("Microsoft") no2 = no1.OpenSubKey("Windows") no3 = no2.OpenSubKey("CurrentVersion") no4 = no3.OpenSubKey("App Paths") For Each site In no4.GetSubKeyNames() sitekey = no4.OpenSubKey(site) For Each valuename In sitekey.GetValueNames() If InStrRev(sitekey.GetValue(valuename).ToString,".") > 0 And sitekey.GetValue(valuename).ToString.Length > 4 Then Me.ListLj.Items.Add(sitekey.GetValue(valuename)) End If Next Next
更多代码 请访问我的网站 代码三人帮