Demo1:使用窗体控件实现一个简易版计算器
c
Public Class Form1
Private Sub Button_1_Click(sender As Object, e As EventArgs) Handles Button_1.Click
CalSubBox.Text += Button_1.Text
End Sub
Private Sub Button_2_Click(sender As Object, e As EventArgs) Handles Button_2.Click
CalSubBox.Text += Button_2.Text
End Sub
Private Sub Button_3_Click(sender As Object, e As EventArgs) Handles Button_3.Click
CalSubBox.Text += Button_3.Text
End Sub
Private Sub Button_4_Click(sender As Object, e As EventArgs) Handles Button_4.Click
CalSubBox.Text += Button_4.Text
End Sub
Private Sub Button_5_Click(sender As Object, e As EventArgs) Handles Button_5.Click
CalSubBox.Text += Button_5.Text
End Sub
Private Sub Button_6_Click(sender As Object, e As EventArgs) Handles Button_6.Click
CalSubBox.Text += Button_6.Text
End Sub
Private Sub Button_7_Click(sender As Object, e As EventArgs) Handles Button_7.Click
CalSubBox.Text += Button_7.Text
End Sub
Private Sub Button_8_Click(sender As Object, e As EventArgs) Handles Button_8.Click
CalSubBox.Text += Button_8.Text
End Sub
Private Sub Button_9_Click(sender As Object, e As EventArgs) Handles Button_9.Click
CalSubBox.Text += Button_9.Text
End Sub
Private Sub Button_0_Click(sender As Object, e As EventArgs) Handles Button_0.Click
CalSubBox.Text += Button_0.Text
End Sub
Private Sub Button_Add_Click(sender As Object, e As EventArgs) Handles Button_Add.Click
CalSubBox.Text += Button_Add.Text
End Sub
Private Sub Button_Sub_Click(sender As Object, e As EventArgs) Handles Button_Sub.Click
CalSubBox.Text += Button_Sub.Text
End Sub
Private Sub Button_Mul_Click(sender As Object, e As EventArgs) Handles Button_Mul.Click
CalSubBox.Text += Button_Mul.Text
End Sub
Private Sub Button_Exc_Click(sender As Object, e As EventArgs) Handles Button_Exc.Click
CalSubBox.Text += Button_Exc.Text
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
CalSubBox.Text += "."
End Sub
Private Sub Button_CE_Click(sender As Object, e As EventArgs) Handles Button_CE.Click
ResultBox.Text = vbNullString
CalSubBox.Text = ""
N_S = ""
End Sub
Private Sub Button_C_Click(sender As Object, e As EventArgs) Handles Button_C.Click
ResultBox.Text = vbNullString
CalSubBox.Text = ""
N_S = ""
End Sub
Public Result As Double
Public ErrMsg As String = "正无穷大"
Private Sub Button_Equal_Click(sender As Object, e As EventArgs) Handles Button_Equal.Click
Try
Dim result = New DataTable().Compute(CalSubBox.Text, Nothing) '将一个表达式进行运算
ResultBox.Text = result '将结果显示出来
If (ErrMsg = ResultBox.Text) Then
ResultBox.Text = "除数不能为0"
End If
Catch ex As Exception
MsgBox("无法计算:" & ex.Message)
End Try
Dim 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 Sub
Function DelLastChar(s As String)
Return s.Substring(0, s.Length - 1)
End Function
Function DelZero(ByVal StrArr As String())
Dim Op_StrArr As String = GetOperateStr(CalSubBox.Text) '[+,-] 先获取整个字符串列表
Dim Op_Str As String
For 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 If
Console.WriteLine("操作数:" + Op_Str)
N_S += DelZero_S + Op_Str '将抹零的字符串 与 当前操作数拼接 '最后一次的单个操作数不变,用DelLastChar去掉即可
Console.WriteLine("N_S:" & N_S)
Console.WriteLine(" DelZero_S:" & DelZero_S)
Next
Return N_S
End Function
Function 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) = "/" Then
NewStrArr += StrArr(i)
End If
Next
Return NewStrArr '字符串数组[+,-,]
End Function
Private Sub Button_Backspace_Click(sender As Object, e As EventArgs) Handles Button_Backspace.Click
Try
CalSubBox.Text = CalSubBox.Text.Substring(0, CalSubBox.Text.Length - 1) '回退一个字符:将字符串截取0,end-1赋值回去即可
Catch ex As Exception
MsgBox("发生错误:" & ex.Message)
CalSubBox.Text = ""
End Try
End Sub
End Class
Demo2:对文件进行处理
要求:1.去掉当前文件的第一行
2.从第二行开始算,保留奇数行,去掉偶数行
3.将每一行的分隔符改为"/"
4.将每一行的2位和3位数字调换位置
5.将每一个数字保留2位小数
c
Imports System.IO
Module Module1
Sub 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 While
fr.Close()
fw.Close()
Console.WriteLine("已输出到桌面、按下任意键退出.....")
Console.ReadKey()
End Sub
Function 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 & "/" '拿到每一个字符串并拼接/ 赋值给新Str
Next
'交换的代码
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)
Next
Return new_str1
End Function
Function 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 + "/" + StrA
End Function
End Module
Demo3:OPenDialog和SaveDialog对话框案例(包括ToolStripMenu)
c
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.InitialDirectory = "D:\"
OpenFileDialog1.Filter = "纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
OpenFileDialog1.Title = "打开文件"
' .RestoreDirectory = True '对话框关闭之前还原至当前目录
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
fileName = OpenFileDialog1.FileName 'OpenFileDialog1已经拿到文件名了
fr = New StreamReader(fileName) '创建fileReader流通过文件名来读取文件
RichTextBox1.Text = fr.ReadToEnd() 'fr已经获取到文件的所有信息,使用ReadToEnd()获取其内容
fr.Close()
End If
End Sub
Private Sub 打开文件ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 打开文件ToolStripMenuItem.Click
OpenFileDialog1.InitialDirectory = "D:\"
OpenFileDialog1.Filter = "纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
OpenFileDialog1.Title = "打开文件"
' .RestoreDirectory = True '对话框关闭之前还原至当前目录
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
fileName = OpenFileDialog1.FileName 'OpenFileDialog1已经拿到文件名了
fr = New StreamReader(fileName) '创建fileReader流通过文件名来读取文件
RichTextBox1.Text = fr.ReadToEnd() 'fr已经获取到文件的所有信息,使用ReadToEnd()获取其内容
fr.Close()
End If
End Sub