VBNET常用技巧代码.docx
- 文档编号:7317519
- 上传时间:2023-01-22
- 格式:DOCX
- 页数:18
- 大小:23.89KB
VBNET常用技巧代码.docx
《VBNET常用技巧代码.docx》由会员分享,可在线阅读,更多相关《VBNET常用技巧代码.docx(18页珍藏版)》请在冰豆网上搜索。
VBNET常用技巧代码
1.单元格相互计算
关键字:
.Focused
PrivateSubTextBox1_TextChanged(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesTextBox1.TextChanged,TextBox2.TextChanged
IfTextBox1.FocusedThen
TextBox2.Text=Val(TextBox1.Text)*3
ElseIfTextBox2.FocusedThen
TextBox1.Text=Val(TextBox2.Text)/3
EndIf
EndSub
2.对比两个数据的字符大小
关键字:
StrComp(string1,string2)
PrivateSubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.Click
Label4.Text=StrComp(TextBox5.Text,TextBox6.Text)
'string1 'string1=string2,返回0 'string1>string2,返回1 EndSub 3.对应匹配 关键字: While….EndWhile PrivateSubButton4_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton4.Click Dimcity()AsString={"北京","上海","天津","广州","哈尔滨","长春","沈阳","西安","兰州","银川","太原"} Dimno()AsString={"010","021","022","020","0451","0431","024","029","0931","0951","0351"} Dimi%=0 While(i i=i+1 EndWhile IfStrComp(no(i),TextBox7.Text)=0Then Label5.Text="对应城市为: "&city(i) Else Label5.Text="没有所对应城市" EndIf EndSub 4.随机数生成、在原基础扩增数组、逐行扩增文本显示 关键字: Int(Rnd()*80)+20、ReDimPreservescore(n+1)、mystr=mystr+String(i)&Chr(13)&Chr(10) PrivateSubButton5_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton5.Click Dimn%,score! (),i%,sum! DimmystrAsString="" n=Val(TextBox8.Text) ReDimscore(n-1) Fori=0Ton-1 score(i)=Int(Rnd()*80)+20 sum=sum+score(i) Next ReDimPreservescore(n+1) score(n)=sum/n'score(n)平均数计算,score(n+1)计算超过平均数的个数 Fori=0Ton-1 Ifscore(i)>score(n)Then score(n+1)=score(n+1)+1 EndIf Next Fori=0Ton-1 mystr=mystr+Str(score(i))&Chr(13)&Chr(10) Next TextBox9.Text=mystr TextBox10.Text=score(n) TextBox11.Text=score(n+1) EndSub 5.骗过文本框TextChanged事件 关键字: Dima# a=Val(TextBox1.Text) TextBox1.Text="" TextBox1.Text=a 6.LinkLabel单击后文字切换 关键字: 定义全局变量、Not DimjudgeAsBoolean PrivateSubLinkLabel1_LinkClicked(ByValsenderAsSystem.Object,ByValeAsSystem.Windows.Forms.LinkLabelLinkClickedEventArgs)HandlesLinkLabel1.LinkClicked judge=Notjudge Ifjudge=FalseThen LinkLabel1.Text="温度/℃" Else LinkLabel1.Text="温度/℉" EndIf EndSub 7.限制文本框数字输入 关键字: KeyPress事件、CType(转换类型)、InStr(字符查找)、TextBox1.SelectionStart=0(光标在起始位置) PrivateSubT11_KeyPress(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.KeyPressEventArgs)HandlesT11.KeyPress,T12.KeyPress,T13.KeyPress Dimbool0AsBoolean,bool1AsBoolean,bool2AsBoolean,bool3AsBoolean '对输入的小数点进行判断 IfInStr(CType(sender,TextBox).Text,".")>0Then bool0=True'如果已经输入了一个小数点,则第二个小数点不能再输入 ElseIfCType(sender,TextBox).TextLike"*e"=TrueOrCType(sender,TextBox).TextLike"*e*"=TrueThen IfCType(sender,TextBox).SelectionStart>=InStr(CType(sender,TextBox).Text,"e")Then bool0=True'如果没有输入小数点,当出现如“1e”字符时,则光标从“e”往后不能输入小数点;如果出现如“1e32”,则“e”后面的不能输入小数点 ElseIfMid(CType(sender,TextBox).Text,1,1)="-"Then IfCType(sender,TextBox).SelectionStart=0Then bool0=True'如果有负号,则负号前不能输入小数点 EndIf EndIf ElseIfMid(CType(sender,TextBox).Text,1,1)="-"Then IfCType(sender,TextBox).SelectionStart=0Then bool0=True'如果有负号,则负号前不能输入小数点 EndIf Else bool0=False EndIf '对输入e进行判断 IfInStr(CType(sender,TextBox).Text,"e")>0Then bool1=True'如果已经输入一个e,则第二个e不能再输入 ElseIfCType(sender,TextBox).Text=""Then bool1=True'如果文本框为空值,则不能以e打头 ElseIfCType(sender,TextBox).Text="."OrCType(sender,TextBox).Text="-."OrCType(sender,TextBox).Text="-"Then bool1=True'如果文本框第一个字符为小数点、负号、“-.”,则不能紧接着输入e ElseIfCType(sender,TextBox).TextLike"*"Then IfCType(sender,TextBox).SelectionStart<>Len(CType(sender,TextBox).Text)Then bool1=True'如果文本框已经有字符,则只有当光标在字符串最右侧时可以输入e EndIf Else bool1=False EndIf '对负号输入判断 IfCType(sender,TextBox).Text=""Then bool2=True'如果文本框为空值,则可以输入负号 ElseIfCType(sender,TextBox).TextLike"*e"=TrueThen IfCType(sender,TextBox).SelectionStart=InStr(CType(sender,TextBox).Text,"e")Then bool2=True'如果文本框当前字符如“1e”,紧接着可以输入负号 ElseIfCType(sender,TextBox).SelectionStart=0AndMid(CType(sender,TextBox).Text,1,1)<>"-"Then bool2=True'如果文本框当前字符如“1e”,光标在起始位置并且第一个字符不是负号时,可以在起始位置输入负号 EndIf ElseIfCType(sender,TextBox).TextLike"*e#*"=TrueThen IfCType(sender,TextBox).SelectionStart=InStr(CType(sender,TextBox).Text,"e")Then bool2=True'如果文本框当前字符如“1e3”,则当光标移到e后面才能输入负号 ElseIfCType(sender,TextBox).SelectionStart=0AndMid(CType(sender,TextBox).Text,1,1)<>"-"Then bool2=True'如果文本框当前字符如“1e3”,光标在起始位置并且第一个字符不是负号时,可以在起始位置输入负号 EndIf ElseIfMid(CType(sender,TextBox).Text,1,1)<>"-"Then IfCType(sender,TextBox).SelectionStart=0Then bool2=True'如果文本框当前第一个字符不是负号,则当光标移动到起始位置时可以输入负号 EndIf Else bool2=False EndIf '对已经输入内容的部分再次输入数字进行判断 IfInStr(CType(sender,TextBox).Text,"-")>0Then IfCType(sender,TextBox).SelectionStart=0OrCType(sender,TextBox).SelectionStart=InStr(2,CType(sender,TextBox).Text,"-")-1Then bool3=True'两种情况,一种是负号在前面部分,则负号前不能输入数字;另一种是负号在e后面,负号前不能输入数字 EndIf Else bool3=False EndIf 'KeyPress事件的核心部分,对文本框仅限制输入数字、小数点、负号、退格键、e,并通过以上的判断来具体体现限制字符输入 IfChar.IsDigit(e.KeyChar)Ore.KeyChar="."Ore.KeyChar=Chr(8)Ore.KeyChar="e"Then Ife.KeyChar="."Andbool0Then e.Handled=True ElseIfe.KeyChar="e"Andbool1Then e.Handled=True ElseIfChar.IsDigit(e.KeyChar)Andbool3Then e.Handled=True Else e.Handled=False EndIf ElseIfe.KeyChar="-"Andbool2Then e.Handled=False Else e.Handled=True EndIf EndSub 8.小数位处理 关键字: Format()、递归调用 '该函数为格式化数字显示格式函数。 示例中浮点数显示最多占9个字符,如-0.000001, '科学计数显示最多占12个字符,如-1.0000e+004。 如需修改格式显示,只需修改如 '“10000”、“0.0001”、“"0.000000"”等标识符,注意SelectCase的个数。 Functionformatdigit$(ByVala#) Dimb$="",n% Ifa>0Then Ifa>=10000Then Forn=1To999 IfInt(a/10^n+0.1)>0AndInt(a/10^n+0.1)<10Then'加0.1是为了消除浮点运算误差 ExitFor EndIf Next b=Format(a/(10^n),"0.0000")&"e+"&Format(n,"000")'10000以上以科学计数显示,并保留四位小数 ElseIfa>=0.0001Anda<10000Then SelectCasea CaseIs<10 b=Format(a,"0.000000") CaseIs<100 b=Format(a,"0.00000") CaseIs<1000 b=Format(a,"0.0000") CaseIs<10000 b=Format(a,"0.000")'随着数字位数的变化,保留小数也相应变化 EndSelect ElseIfa>0Anda<0.0001Then Forn=1To999 IfInt(a*10^n+0.1)>0AndInt(a*10^n+0.1)<10Then ExitFor EndIf Next b=Format(a*10^n,"0.0000")&"e-"&Format(n,"000")'小于0.0001时以科学计数显示,并保留四位小数 EndIf ElseIfa=0Then b=Format(a,"0.000000")'当数字为零时显示 ElseIfa<0Then b="-"&formatdigit(-a)'递归调用,求小于零的情况 EndIf formatdigit=b EndFunction 9.对限制字符输入的文本框使用快捷键 关键字: e.Control、e.KeyCode PrivateSubTextBox1_KeyDown(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.KeyEventArgs)HandlesTextBox1.KeyDown,TextBox2.KeyDown,TextBox3.KeyDown Ife.ControlAnde.KeyCode=Keys.AThen CType(sender,TextBox).SelectAll() EndIf Ife.ControlAnde.KeyCode=Keys.CThen CType(sender,TextBox).Copy() EndIf Ife.ControlAnde.KeyCode=Keys.VThen CType(sender,TextBox).Paste() EndIf Ife.ControlAnde.KeyCode=Keys.XThen CType(sender,TextBox).Cut() EndIf EndSub 10.批量为某一容器中的全部文本框赋值 关键字: Control、TypeOf PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click DimcAsControl ForEachcInTabControl1.TabPages(0).Controls'这里以TabControl1中的第一个页面为容器 IfTypeOfcIsTextBoxThen c.Text="" EndIf Next EndSub 其实这样操作效率不高 Fori=1To3 ForEachc1InMe.Controls ForEachc2InMe.Controls IfTypeOfc1IsTextBoxThen IfTypeOfc2IsTextBoxThen Ifc1.Name="T1"&iThen Ifc2.Name="T2"&iThen c1.Text=i c2.Text=Val(c1.Text)*(i+1) EndIf EndIf EndIf EndIf Next Next Next 11.复制光标所在文本框并着色显示 关键字: Clipboard.SetText()、GotFocus事件 DimTAsNewTextBox PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click IfT.Text<>""Then Clipboard.SetText(T.Text,TextDataFormat.Text) DimcAsControl ForEachcInMe.Controls IfTypeOfcIsTextBoxThen Ifc.Name=T.NameThen c.BackColor=T.BackColor ExitSub EndIf EndIf Next EndIf EndSub '对单击鼠标后光标所在的文本框复制内容 PrivateSubT11_GotFocus(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesT11.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 DimcAsControl ForEachcInMe.Controls IfTypeOfcIsTextBoxThen c.BackColor=Color.Empty'让文本框背景色恢复到原来 EndIf Next EndSub '对单击鼠标光标所在的文本框又进行了文字编辑的文本框内容复制 PrivateSubT11_TextChanged(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesT11.TextChanged,T12.TextChanged,T13.TextChanged,T21.TextChanged,T22.TextChanged,T23.TextChanged DimcAsControl ForEachcInMe.Controls IfTypeOfcIsTextBoxThen Ifc.FocusedThen T.Text=c.Text EndIf EndIf Next EndSub 12.DataGridView控件显示标题行号 关键字: DataGridView,RowPostPaint事件 PrivateSubDataGridView1_RowPostPaint(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.DataGridViewRowPostPaintEventArgs)HandlesDataGridView1.RowPostPaint '通过RowPostPaint事件画行号 DimrectAsNewRectangle(e.RowBounds.Left,e.RowBounds.Top,_ DataGridView1.RowHeadersWidth-3,e.RowBounds.Height) '定义一个矩形,矩形边界贴左和上,矩形的宽度为行标题的高度减去一个数值(这里是) TextRenderer.DrawText(e.Graphics,(e.RowIndex+1).ToStrin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VBNET 常用 技巧 代码