Demo1:使用窗体控件实现一个简易版计算器
Public Class Form1Private Sub Button_1_Click(sender As Object, e As EventArgs) Handles Button_1.ClickCalSubBox.Text += Button_1.TextEnd SubPrivate Sub Button_2_Click(sender As Object, e As EventArgs) Handles Button_2.ClickCalSubBox.Text += Button_2.TextEnd SubPrivate Sub Button_3_Click(sender As Object, e As EventArgs) Handles Button_3.ClickCalSubBox.Text += Button_3.TextEnd SubPrivate Sub Button_4_Click(sender As Object, e As EventArgs) Handles Button_4.ClickCalSubBox.Text += Button_4.TextEnd SubPrivate Sub Button_5_Click(sender As Object, e As EventArgs) Handles Button_5.ClickCalSubBox.Text += Button_5.TextEnd SubPrivate Sub Button_6_Click(sender As Object, e As EventArgs) Handles Button_6.ClickCalSubBox.Text += Button_6.TextEnd SubPrivate Sub Button_7_Click(sender As Object, e As EventArgs) Handles Button_7.ClickCalSubBox.Text += Button_7.TextEnd SubPrivate Sub Button_8_Click(sender As Object, e As EventArgs) Handles Button_8.ClickCalSubBox.Text += Button_8.TextEnd SubPrivate Sub Button_9_Click(sender As Object, e As EventArgs) Handles Button_9.ClickCalSubBox.Text += Button_9.TextEnd SubPrivate Sub Button_0_Click(sender As Object, e As EventArgs) Handles Button_0.ClickCalSubBox.Text += Button_0.TextEnd SubPrivate Sub Button_Add_Click(sender As Object, e As EventArgs) Handles Button_Add.ClickCalSubBox.Text += Button_Add.TextEnd SubPrivate Sub Button_Sub_Click(sender As Object, e As EventArgs) Handles Button_Sub.ClickCalSubBox.Text += Button_Sub.TextEnd SubPrivate Sub Button_Mul_Click(sender As Object, e As EventArgs) Handles Button_Mul.ClickCalSubBox.Text += Button_Mul.TextEnd SubPrivate Sub Button_Exc_Click(sender As Object, e As EventArgs) Handles Button_Exc.ClickCalSubBox.Text += Button_Exc.TextEnd SubPrivate Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.ClickCalSubBox.Text += "."End SubPrivate Sub Button_CE_Click(sender As Object, e As EventArgs) Handles Button_CE.ClickResultBox.Text = vbNullStringCalSubBox.Text = ""N_S = ""End SubPrivate Sub Button_C_Click(sender As Object, e As EventArgs) Handles Button_C.ClickResultBox.Text = vbNullStringCalSubBox.Text = ""N_S = ""End SubPublic Result As DoublePublic ErrMsg As String = "正无穷大"Private Sub Button_Equal_Click(sender As Object, e As EventArgs) Handles Button_Equal.ClickTryDim result = New DataTable().Compute(CalSubBox.Text, Nothing) '将一个表达式进行运算ResultBox.Text = result '将结果显示出来If (ErrMsg = ResultBox.Text) ThenResultBox.Text = "除数不能为0"End IfCatch ex As ExceptionMsgBox("无法计算:" & ex.Message)End TryDim Split_Str As String() = CalSubBox.Text.Split({"+"c, "-"c, "/"c, "*"c}) 'Split_Str是分割后的单个字符串DelZero(Split_Str)Console.WriteLine("新字符串:" & N_S)CalSubBox.Text = DelLastChar(N_S) '去掉最后一个字符,展示到文本框中End SubFunction DelLastChar(s As String)Return s.Substring(0, s.Length - 1)End FunctionFunction DelZero(ByVal StrArr As String())Dim Op_StrArr As String = GetOperateStr(CalSubBox.Text) '[+,-] 先获取整个字符串列表Dim Op_Str As StringFor i = 0 To StrArr.Length - 1 ' strArr实质上是分割后的单个字符串Console.WriteLine("第" & i + 1 & "次" & vbNewLine)Dim DelZero_S As String = StrArr(i).TrimStart({"0"c}) 'DelZero_S是抹零后的单个字符串、Console.WriteLine("抹零后:" & DelZero_S)If Not i = Op_StrArr.Length Then '最后一次不需要获取Op_Str = Op_StrArr(i) '获取当前的单个操作数,从字符串数组中获取End IfConsole.WriteLine("操作数:" + Op_Str)N_S += DelZero_S + Op_Str '将抹零的字符串 与 当前操作数拼接 '最后一次的单个操作数不变,用DelLastChar去掉即可Console.WriteLine("N_S:" & N_S)Console.WriteLine(" DelZero_S:" & DelZero_S)NextReturn N_SEnd FunctionFunction GetOperateStr(ByVal StrArr As String) '获取操作数,返回一个存放操作数的字符串数组Dim NewStrArr As String = ""For i = 0 To StrArr.Length - 1 '获取下标,从Index开始寻找第一个操作数,而不是重新开始If StrArr(i) = "+" Or StrArr(i) = "-" Or StrArr(i) = "*" Or StrArr(i) = "/" ThenNewStrArr += StrArr(i)End IfNextReturn NewStrArr '字符串数组[+,-,]End FunctionPrivate Sub Button_Backspace_Click(sender As Object, e As EventArgs) Handles Button_Backspace.ClickTryCalSubBox.Text = CalSubBox.Text.Substring(0, CalSubBox.Text.Length - 1) '回退一个字符:将字符串截取0,end-1赋值回去即可Catch ex As ExceptionMsgBox("发生错误:" & ex.Message)CalSubBox.Text = ""End TryEnd SubEnd Class
Demo2:对文件进行处理
要求:1.去掉当前文件的第一行
2.从第二行开始算,保留奇数行,去掉偶数行
3.将每一行的分隔符改为“/”
4.将每一行的2位和3位数字调换位置
5.将每一个数字保留2位小数
Imports System.IOModule Module1Sub Main()Dim fr As New StreamReader("E:\T4-2.txt") '这里存放要处理的文件路径Dim fw As New StreamWriter("C:\Users\Public\Desktop\T4-2-处理.txt") '写到桌面的fw流的路径Dim data As String = ""Dim data1 As String = ""Dim new_data As String = ""fr.ReadLine() '跳过第一行中文字体,相当于第0行While data <> " " 'data读奇数行,data1读偶数行,遇到空格行就退出data = fr.ReadLine() '读取第一/三/五...行数据,并把读的内容赋值给当前data(data每次循环都会被重新赋值)If data = Nothing Then Exit While '当data读到空格行就退出循环,下同new_data = replace_splitChar(data) '将读取的data替换字符并返回给新data,此时的新data可以被写出Console.WriteLine("newData:" + new_data)fw.WriteLine(new_data, True) '写出 (重写)data1 = fr.ReadLine() '跳过第二/四/六...行,If data1 = Nothing Then Exit While 'End Whilefr.Close()fw.Close()Console.WriteLine("已输出到桌面、按下任意键退出.....")Console.ReadKey()End SubFunction replace_splitChar(ByVal charStr As String)Dim new_str As String = "" '用一个新的Str来拼接/Dim new_str1 As String = ""Dim strArr As String() = charStr.Split(",") '分隔为字符串数组["110","23","12"]For Each s In strArr'把每个字符串保留两位小数Dim f_s = Double.Parse(s).ToString("0.00") 'f_s是保留两位小数的当前字符串new_str += f_s & "/" '拿到每一个字符串并拼接/ 赋值给新StrNext'交换的代码Dim strArr1 As String() = new_str.Split("/") '对newstr进行交换操作For i = 0 To new_str.Length - 1 'strArr就是字符串数组new_str1 = strArr1(0) + "/" + exchange(strArr(1), strArr(2)) + "/" + strArr1(3)NextReturn new_str1End FunctionFunction exchange(ByVal StrA As String, ByVal StrB As String) As String '将两个字符串进行交换位置的函数StrA = Double.Parse(StrA).ToString("0.00") '先转换再交换StrB = Double.Parse(StrB).ToString("0.00")Return StrB + "/" + StrAEnd FunctionEnd Module
Demo3:OPenDialog和SaveDialog对话框案例(包括ToolStripMenu)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickOpenFileDialog1.InitialDirectory = "D:\"OpenFileDialog1.Filter = "纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"OpenFileDialog1.Title = "打开文件"' .RestoreDirectory = True '对话框关闭之前还原至当前目录If OpenFileDialog1.ShowDialog() = DialogResult.OK ThenfileName = OpenFileDialog1.FileName 'OpenFileDialog1已经拿到文件名了fr = New StreamReader(fileName) '创建fileReader流通过文件名来读取文件RichTextBox1.Text = fr.ReadToEnd() 'fr已经获取到文件的所有信息,使用ReadToEnd()获取其内容fr.Close()End IfEnd SubPrivate Sub 打开文件ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 打开文件ToolStripMenuItem.ClickOpenFileDialog1.InitialDirectory = "D:\"OpenFileDialog1.Filter = "纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"OpenFileDialog1.Title = "打开文件"' .RestoreDirectory = True '对话框关闭之前还原至当前目录If OpenFileDialog1.ShowDialog() = DialogResult.OK ThenfileName = OpenFileDialog1.FileName 'OpenFileDialog1已经拿到文件名了fr = New StreamReader(fileName) '创建fileReader流通过文件名来读取文件RichTextBox1.Text = fr.ReadToEnd() 'fr已经获取到文件的所有信息,使用ReadToEnd()获取其内容fr.Close()End IfEnd Sub