VB.NET常用技巧代码_第1页
VB.NET常用技巧代码_第2页
VB.NET常用技巧代码_第3页
VB.NET常用技巧代码_第4页
VB.NET常用技巧代码_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. 单元格相互计算关键字: .Focused Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged If TextBox1.Focused Then TextBox2.Text = Val(TextBox1.Text) * 3 ElseIf TextBox2.Focused Then TextBox1.Text = Val(TextBox2.Text) / 3 End If End Sub2. 对比两个数据的字符大小关键字: StrComp(string1,string2) Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Label4.Text = StrComp(TextBox5.Text, TextBox6.Text)string1 string2,返回1 End Sub3. 对应匹配关键字: While.End While Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim city() As String = 北京, 上海, 天津, 广州, 哈尔滨, 长春, 沈阳, 西安, 兰州, 银川, 太原 Dim no() As String = 010, 021, 022, 020, 0451, 0431, 024, 029, 0931, 0951, 0351 Dim i% = 0 While (i UBound(no) And StrComp(no(i), TextBox7.Text) 0) i = i + 1 End While If StrComp(no(i), TextBox7.Text) = 0 Then Label5.Text = 对应城市为: & city(i) Else Label5.Text = 没有所对应城市 End If End Sub4. 随机数生成、在原基础扩增数组、逐行扩增文本显示关键字: Int(Rnd() * 80) + 20、ReDim Preserve score(n + 1)、mystr = mystr + String(i) & Chr(13) & Chr(10) Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim n%, score!(), i%, sum! Dim mystr As String = n = Val(TextBox8.Text) ReDim score(n - 1) For i = 0 To n - 1 score(i) = Int(Rnd() * 80) + 20 sum = sum + score(i) Next ReDim Preserve score(n + 1) score(n) = sum / n score(n)平均数计算,score(n+1)计算超过平均数的个数 For i = 0 To n - 1 If score(i) score(n) Then score(n + 1) = score(n + 1) + 1 End If Next For i = 0 To n - 1 mystr = mystr + Str(score(i) & Chr(13) & Chr(10) Next TextBox9.Text = mystr TextBox10.Text = score(n) TextBox11.Text = score(n + 1) End Sub5. 骗过文本框TextChanged事件关键字: Dim a# a = Val(TextBox1.Text) TextBox1.Text = TextBox1.Text = a6. LinkLabel单击后文字切换关键字: 定义全局变量、NotDim judge As Boolean Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked judge = Not judge If judge = False Then LinkLabel1.Text = 温度/ Else LinkLabel1.Text = 温度/ End If End Sub7. 限制文本框数字输入关键字: KeyPress事件、CType(转换类型)、InStr(字符查找)、TextBox1.SelectionStart=0(光标在起始位置)Private Sub T11_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles T11.KeyPress, T12.KeyPress, T13.KeyPress Dim bool0 As Boolean, bool1 As Boolean, bool2 As Boolean, bool3 As Boolean 对输入的小数点进行判断 If InStr(CType(sender, TextBox).Text, .) 0 Then bool0 = True 如果已经输入了一个小数点,则第二个小数点不能再输入 ElseIf CType(sender, TextBox).Text Like *e = True Or CType(sender, TextBox).Text Like *e* = True Then If CType(sender, TextBox).SelectionStart = InStr(CType(sender, TextBox).Text, e) Then bool0 = True 如果没有输入小数点,当出现如“1e”字符时,则光标从“e”往后不能输入小数点;如果出现如“1e32”,则“e”后面的不能输入小数点 ElseIf Mid(CType(sender, TextBox).Text, 1, 1) = - Then If CType(sender, TextBox).SelectionStart = 0 Then bool0 = True 如果有负号,则负号前不能输入小数点 End If End If ElseIf Mid(CType(sender, TextBox).Text, 1, 1) = - Then If CType(sender, TextBox).SelectionStart = 0 Then bool0 = True 如果有负号,则负号前不能输入小数点 End If Else bool0 = False End If 对输入e进行判断 If InStr(CType(sender, TextBox).Text, e) 0 Then bool1 = True 如果已经输入一个e,则第二个e不能再输入 ElseIf CType(sender, TextBox).Text = Then bool1 = True 如果文本框为空值,则不能以e打头 ElseIf CType(sender, TextBox).Text = . Or CType(sender, TextBox).Text = -. Or CType(sender, TextBox).Text = - Then bool1 = True 如果文本框第一个字符为小数点、负号、“-.”,则不能紧接着输入e ElseIf CType(sender, TextBox).Text Like * Then If CType(sender, TextBox).SelectionStart Len(CType(sender, TextBox).Text) Then bool1 = True 如果文本框已经有字符,则只有当光标在字符串最右侧时可以输入e End If Else bool1 = False End If 对负号输入判断 If CType(sender, TextBox).Text = Then bool2 = True 如果文本框为空值,则可以输入负号 ElseIf CType(sender, TextBox).Text Like *e = True Then If CType(sender, TextBox).SelectionStart = InStr(CType(sender, TextBox).Text, e) Then bool2 = True 如果文本框当前字符如“1e”,紧接着可以输入负号 ElseIf CType(sender, TextBox).SelectionStart = 0 And Mid(CType(sender, TextBox).Text, 1, 1) - Then bool2 = True 如果文本框当前字符如“1e”,光标在起始位置并且第一个字符不是负号时,可以在起始位置输入负号 End If ElseIf CType(sender, TextBox).Text Like *e#* = True Then If CType(sender, TextBox).SelectionStart = InStr(CType(sender, TextBox).Text, e) Then bool2 = True 如果文本框当前字符如“1e3”,则当光标移到e后面才能输入负号 ElseIf CType(sender, TextBox).SelectionStart = 0 And Mid(CType(sender, TextBox).Text, 1, 1) - Then bool2 = True 如果文本框当前字符如“1e3”,光标在起始位置并且第一个字符不是负号时,可以在起始位置输入负号 End If ElseIf Mid(CType(sender, TextBox).Text, 1, 1) - Then If CType(sender, TextBox).SelectionStart = 0 Then bool2 = True 如果文本框当前第一个字符不是负号,则当光标移动到起始位置时可以输入负号 End If Else bool2 = False End If 对已经输入内容的部分再次输入数字进行判断 If InStr(CType(sender, TextBox).Text, -) 0 Then If CType(sender, TextBox).SelectionStart = 0 Or CType(sender, TextBox).SelectionStart = InStr(2, CType(sender, TextBox).Text, -) - 1 Then bool3 = True 两种情况,一种是负号在前面部分,则负号前不能输入数字;另一种是负号在e后面,负号前不能输入数字 End If Else bool3 = False End If KeyPress事件的核心部分,对文本框仅限制输入数字、小数点、负号、退格键、e,并通过以上的判断来具体体现限制字符输入 If Char.IsDigit(e.KeyChar) Or e.KeyChar = . Or e.KeyChar = Chr(8) Or e.KeyChar = e Then If e.KeyChar = . And bool0 Then e.Handled = True ElseIf e.KeyChar = e And bool1 Then e.Handled = True ElseIf Char.IsDigit(e.KeyChar) And bool3 Then e.Handled = True Else e.Handled = False End If ElseIf e.KeyChar = - And bool2 Then e.Handled = False Else e.Handled = True End If End Sub8. 小数位处理关键字: Format()、递归调用 该函数为格式化数字显示格式函数。示例中浮点数显示最多占9个字符,如-0.000001, 科学计数显示最多占12个字符,如-1.0000e+004。如需修改格式显示,只需修改如 “10000”、“0.0001”、“0.000000”等标识符,注意Select Case的个数。 Function formatdigit$(ByVal a#) Dim b$ = , n% If a 0 Then If a = 10000 Then For n = 1 To 999 If Int(a / 10 n + 0.1) 0 And Int(a / 10 n + 0.1) = 0.0001 And a 10000 Then Select Case a Case Is 10 b = Format(a, 0.000000) Case Is 100 b = Format(a, 0.00000) Case Is 1000 b = Format(a, 0.0000) Case Is 0 And a 0 And Int(a * 10 n + 0.1) 10 Then Exit For End If Next b = Format(a * 10 n, 0.0000) & e- & Format(n, 000) 小于0.0001时以科学计数显示,并保留四位小数 End If ElseIf a = 0 Then b = Format(a, 0.000000) 当数字为零时显示 ElseIf a 0 Then b = - & formatdigit(-a) 递归调用,求小于零的情况 End Ifformatdigit = b End Function9. 对限制字符输入的文本框使用快捷键关键字: e.Control、e.KeyCode Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown If e.Control And e.KeyCode = Keys.A Then CType(sender, TextBox).SelectAll() End If If e.Control And e.KeyCode = Keys.C Then CType(sender, TextBox).Copy() End If If e.Control And e.KeyCode = Keys.V Then CType(sender, TextBox).Paste() End If If e.Control And e.KeyCode = Keys.X Then CType(sender, TextBox).Cut() End If End Sub10. 批量为某一容器中的全部文本框赋值关键字: Control、TypeOf Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim c As Control For Each c In TabControl1.TabPages(0).Controls 这里以TabControl1中的第一个页面为容器 If TypeOf c Is TextBox Then c.Text = End If Next End Sub其实这样操作效率不高 For i = 1 To 3 For Each c1 In Me.Controls For Each c2 In Me.Controls If TypeOf c1 Is TextBox Then If TypeOf c2 Is TextBox Then If c1.Name = T1 & i Then If c2.Name = T2 & i Then c1.Text = i c2.Text = Val(c1.Text) * (i + 1) End If End If End If End If Next Next Next11. 复制光标所在文本框并着色显示关键字: Clipboard.SetText()、GotFocus事件 Dim T As New TextBox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If T.Text Then Clipboard.SetText(T.Text, TextDataFormat.Text) Dim c As Control For Each c In Me.Controls If TypeOf c Is TextBox Then If c.Name = T.Name Then c.BackColor = T.BackColor Exit Sub End If End If Next End If End Sub 对单击鼠标后光标所在的文本框复制内容 Private Sub T11_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles T11.GotFocus, T12.GotFocus, T13.GotFocus, T21.GotFocus, T22.GotFocus, T23.GotFocus T.Text = CType(sender, TextBox).Text T.Name = CType(sender, TextBox).Name T.BackColor = Color.DeepPink Dim c As Control For Each c In Me.Controls If TypeOf c Is TextBox Then c.BackColor = Color.Empty 让文本框背景色恢复到原来 End If Next End Sub 对单击鼠标光标所在的文本框又进行了文字编辑的文本框内容复制 Private Sub T11_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles T11.TextChanged, T12.TextChanged, T13.TextChanged, T21.TextChanged, T22.TextChanged, T23.TextChanged Dim c As Control For Each c In Me.Controls If TypeOf c Is TextBox Then If c.Focused Then T.Text = c.Text End If End If NextEnd Sub12. DataGridView控件显示标题行号关键字: DataGridView,RowPostPaint事件 Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint 通过RowPostPaint事件画行号 Dim rect As New Rectangle(e.RowBounds.Left, e.RowBounds.Top, _ DataGridView1.RowHeadersWidth - 3, e.RowBounds.Height) 定义一个矩形, 矩形边界贴左和上, 矩形的宽度为行标题的高度减去一个数值(这里是) TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString, _ e.InheritedRowStyle.Font, rect, e.InheritedRowStyle.ForeColor, _ TextFormatFlags.Right Or TextFormatFlags.VerticalCenter) 画文本,文本从开始,字体,前景色都是继承行标题的样式,字体大小(像素) 就是上面定义的矩形大小,字体是右对齐、垂直居中的End Sub13. DataGridView动态添加行并为单元格赋值关键字: .Rows.Add、.Rows(index).Cells(0).Value Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 为Datagridview添加行,并将单元格赋值 Dim index As Integer For index = 0 To 19 index = DataGridView1.Rows.Add 添加行的关键句 DataGridView1.Rows(index).Cells(0).Value = (index + 1).ToString 为单元格赋值,第一列单元格从Cells(0)开始 Next End Sub14. DataGridView设置技巧关键字:是否允许用户添加行:AllowUserToAddRows属性更改背景色(默认是灰黑色):BackgroundColor属性设置行标题字体样式:ColumnHeaderDefaultCellStyle属性单元格样式设置:DefaultCellStyle属性禁止按列进行排序:进入Columns集合编辑列,更改SortMode属性为NotSortabel设置行标题宽度和列标题高度,并固定:ColumnHeadersHeight + ColumnHeadersHeightSizeMode属性改为DisableResizing;RowHeadersWidth + RowHeadersWidthSizeMode属性改为DisableResizing设置行高并固定:AutoSizeRowsMode设为None,AllowUserToResizeRows属性为False;RowTemplateHeight属性设置高度值,如18列的右侧不显示空白:AutoSizeColumnsMode属性设为Fill选择模式:SelectionMode属性,可设置按列标题选择(ColumnHeadersSelect)不显示编辑图标:ShowEditionIcon属性设置为False让最后一行不显示黑边技巧:DataGridView高度=列标题高度+行高*任意整数+2去除行标题上的小三角形:(代码)DataGridView1.RowHeadersDefaultCellStyle.Padding = New Padding(DataGridView1.RowHeadersWidth)15. DataGridView选中的单元格内容删除关键字: IsDBNull() Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown 用删除键或退格键删除内容 If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then For Each cell As DataGridViewCell In DataGridView1.SelectedCells If IsDBNull(cell.Value) = False Then cell.Value = DBNull.Value End If Next End IfEnd Sub16. DataGridView添加右键复制菜单关键字: Clipboard.SetDataObject()、GetClipboardContent Private Sub 复制CtrlToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 复制CtrlToolStripMenuItem.Click If DataGridView1.Focused Then Clipboard.SetDataObject(DataGridView1.GetClipboardContent) ElseIf DataGridView2.Focused Then Clipboard.SetDataObject(DataGridView2.GetClipboardContent) End IfEnd Sub17. 综合运用将Excel表格中的内容导入DataGridView关键字: DataGridView1.Columns.Add()、.Font=New Font(“Arial”, .Font.Size)OpenFileDialog1.Filter = Excle文件(*.xls)|*.xls、SELECT * FROM Sheet1$A1:B65536、Catch End Try Dim wd As Integer = 30 行标题宽度 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim index As Integer, i As Integer Me.DataGridView1.Columns.Add(column1, X) Me.DataGridView1.Columns.Add(column1, Y) With DataGridView1 .RowHeadersDefaultCellStyle.Padding = New Padding(DataGridView1.RowHeadersWidth) 去除行标题上的小三角形 .AllowUserToAddRows = False .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing .ColumnHeadersHeight = 20 .RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing .RowHeadersWidth = wd .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None .AllowUserToResizeRows = False .AllowUserToResizeColumns = False .RowTemplate.Height = 18 .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill .ShowEditingIcon = False .ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText .SelectionMode = DataGridViewSelectionMode.RowHeaderSelect .RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single With .ColumnHeadersDefaultCellStyle .Font = New Font(Arial, .Font.Size) .Alignment = DataGridViewContentAlignment.MiddleCenter End With With .DefaultCellStyle .Font = New Font(Arial, .Font.Size) .Alignment = DataGridViewContentAlignment.MiddleRight End With End With For i = 0 To DataGridView1.Columns.Count - 1 Me.DataGridView1.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable Next For index = 0 To 15 index = DataGridView1.Rows.Add Next End Sub Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint 通过RowPostPaint事件画行号 Dim rect As New Rectangle(e.RowBounds.Left, e.RowBounds.Top, _ DataGridView1.RowHeadersWidth - 1, e.RowBounds.Height) 定义一个矩形, 矩形边界贴左和上, 矩形的宽度为行标题的高度- 一个数值(这里是) TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString, _ Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, rect, e.InheritedRowStyle.ForeColor, _ TextFormatFlags.Right Or

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论