vb.net 计算机

Visual Studio2008教程

Visual Studio2008发布后,有大量的VB.net爱好者抛弃原来的Visual Studio2005平台转向现在流行的Visual Studio2008平台。但是Visual Studio2008有一个缺点就是需要比较高的硬件配置才能正常平稳的运行,所以到现在为止还有一部分人处于观望中,并没有转向这个平台。然而Visual Studio2008的一些新的功能以及优化性比先前版本有了很大的进步。本篇文章将主要介绍怎样利用VB.net去编写一个计算器程序,程序虽然不复杂,但是其中所运用的知识和代码非常具有代表性,在平时开发中也是经常需要用到的。希望对VB.net2008的初学者带来帮助。

 打开 Visual Studio 2008。在文件 (File) 菜单上,单击新建项目 (New Project)。 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中,单击 Windows 应用程序(Windows Application)。单击确定 (OK)。

      在Form1窗体中添加一个TextBox1控件和19个Button按钮,详细属性设置请查看以下的表格。

 
 

TextBox1

 
 
 

数据显示框

 
 
 

Button1

 
 
 

0

 
 
 

Button2

 
 
 

.

 
 
 

Button3

 
 
 

归零

 
 
 

Button4

 
 
 

 
 
 

Button5

 
 
 

 
 
 

Button6

 
 
 

1

 
 
 

Button7

 
 
 

2

 
 
 

Button8

 
 
 

3

 
 
 

Button9

 
 
 

+

 
 
 

Button10

 
 
 

-

 
 
 

Button11

 
 
 

4

 
 
 

Button12

 
 
 

5

 
 
 

Button13

 
 
 

6

 
 
 

Button14

 
 
 

*

 
 
 

Button15

 
 
 

/

 
 
 

Button16

 
 
 

7

 
 
 

Button17

 
 
 

8

 
 
 

Button18

 
 
 

9

 
 
 

Button19

 
 
 

ON

 
 
 

Button20

 
 
 

OFF

 
 

 

     

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

详细的界面请查看图1

                             
图1

好了基本的界面已经设计好了,接下来我们来添加相应的代码了,首先进行必要的声明事件。

Public ClassForm1

 

   Dim strdx() As String = {"0", "0", "0"} '声明一个字符串,用以存取数值

   Dim calcount1 As String = "0"

   Dim calcount2 As String = "0"

Dim strvalue As Boolean = False

 

然后需要进入Button1.Click事件中.代码如下

Private SubButton1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click

       If strdx(0) = "0" Then

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "0"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "0"

            TextBox1.Text = strdx(0)

       End If

End Sub

进入Button2.Click事件中.代码如下

  Private SubButton2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click

       strvalue = True

       strdx(0) = strdx(0) & "."

       TextBox1.Text = strdx(0)

 

End Sub

进入Button3.Click事件中.代码如下

Private SubButton3_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button3.Click

       strdx(0) = "0"

       strdx(1) = "0"

       strdx(2) = "0"

       calcount1 = "0"

       calcount2 = "0"

       strvalue = False

       TextBox1.Text = "0."

End Sub

进入Button5.Click事件中.代码如下

Private SubButton5_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button5.Click

       If strdx(2) = "0" Then

            Select Casecalcount1

                Case "+"

                    TextBox1.Text =Str(Val(strdx(1)) + Val(strdx(0)))

                Case "-"

                    TextBox1.Text =Str(Val(strdx(1)) - Val(strdx(0)))

                Case "*"

                    TextBox1.Text =Str(Val(strdx(1)) * Val(strdx(0)))

                Case "/"

                    If strdx(0) = "0" Then

                        TextBox1.Text = "error!"

                    Else

                        TextBox1.Text =Str(Val(strdx(1)) / Val(strdx(0)))

                    End If

            End Select

       ElseIf calcount2 = "*" Then

           strdx(0) = Str(Val(strdx(0))* Val(strdx(2)))

            Select Casecalcount1

                Case "+"

                    TextBox1.Text =Str(Val(strdx(1)) + Val(strdx(0)))

                Case "-"

                    TextBox1.Text =Str(Val(strdx(1)) - Val(strdx(0)))

                Case "*"

                    TextBox1.Text =Str(Val(strdx(1)) * Val(strdx(0)))

                Case "/"

                    If strdx(0) = "0" Then

                        TextBox1.Text = "error!"

                    Else

                        TextBox1.Text =Str(Val(strdx(1)) / Val(strdx(0)))

                    End If

            End Select

       Else : calcount2 = "/"

            strdx(0) = Str(Val(strdx(2)) /Val(strdx(0)))

            Select Casecalcount1

               Case "+"

                    TextBox1.Text =Str(Val(strdx(1)) + Val(strdx(0)))

                Case "-"

                    TextBox1.Text =Str(Val(strdx(1)) - Val(strdx(0)))

                Case "*"

                    TextBox1.Text = Str(Val(strdx(1))* Val(strdx(0)))

                Case "/"

                    If strdx(0) = "0" Then

                        TextBox1.Text = "error!"

                    Else

                        TextBox1.Text =Str(Val(strdx(1)) / Val(strdx(0)))

                    End If

            End Select

       End If

   End Sub

进入Button6.Click事件中.代码如下

 

Private SubButton6_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button6.Click

       If strdx(0) = "0" Then

            strdx(0) = "1"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "1"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "1"

            TextBox1.Text = strdx(0)

       End If

    End Sub

 

进入Button7.Click事件中.代码如下

Private SubButton7_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button7.Click

       If strdx(0) = "0" Then

            strdx(0) = "2"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "2"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "2"

            TextBox1.Text = strdx(0)

       End If

End Sub

 

进入Button8.Click事件中.代码如下

Private SubButton8_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button8.Click

       If strdx(0) = "0" Then

            strdx(0) = "3"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "3"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "3"

            TextBox1.Text = strdx(0)

       End If

End Sub

 

进入Button9.Click事件中.代码如下

Private SubButton9_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button9.Click

       If calcount1 = "0" Then

            calcount1 = "+"

            strdx(1) = strdx(0)

            strdx(0) = "0"

       Else : Select Casecalcount1

                Case "+"

                    strdx(1) =Str(Val(strdx(0)) + Val(strdx(1)))

                    strdx(0) = "0"

                    calcount1 = "+"

                Case "-"

                    strdx(1) = Str(Val(strdx(1))- Val(strdx(0)))

                    strdx(0) = "0"

                    calcount1 = "+"

                Case "*"

                    strdx(1) =Str(Val(strdx(0)) * Val(strdx(1)))

                    strdx(0) = "0"

                    calcount1 = "+"

                Case "/"

                    strdx(1) =Str(Val(strdx(1)) / Val(strdx(0)))

                    strdx(0) = "0"

                    calcount1 = "+"

            End Select

       End If

End Sub

 

进入Button10.Click事件中.代码如下

Private SubButton10_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button10.Click

       If calcount1 = "0" Then

            calcount1 = "-"

            strdx(1) = strdx(0)

            strdx(0) = "0"

       Else : Select Casecalcount1

               Case "+"

                    strdx(1) =Str(Val(strdx(0)) + Val(strdx(1)))

                    strdx(0) = "0"

                    calcount1 = "-"

                Case "-"

                    strdx(1) =Str(Val(strdx(1)) - Val(strdx(0)))

                   strdx(0) = "0"

                    calcount1 = "-"

                Case "*"

                    strdx(1) =Str(Val(strdx(0)) * Val(strdx(1)))

                    strdx(0) = "0"

                    calcount1 = "-"

                Case "/"

                   strdx(1) =Str(Val(strdx(1)) / Val(strdx(0)))

                    strdx(0) = "0"

                    calcount1 = "-"

            End Select

       End If

End Sub

 

进入Button11.Click事件中.代码如下

Private SubButton11_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button11.Click

       If strdx(0) = "0" Then

            strdx(0) = "4"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "4"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "4"

            TextBox1.Text = strdx(0)

       End If

End Sub

 

进入Button12.Click事件中.代码如下

 

Private SubButton12_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button12.Click

       If strdx(0) = "0" Then

            strdx(0) = "5"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "5"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "5"

            TextBox1.Text = strdx(0)

       End If

   End Sub

 

进入Button13.Click事件中.代码如下

Private SubButton13_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button13.Click

       If strdx(0) = "0" Then

            strdx(0) = "6"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "6"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "6"

            TextBox1.Text = strdx(0)

       End If

End Sub

进入Button14.Click事件中.代码如下

Private SubButton14_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button14.Click

       If calcount1 = "0" Then

            calcount1 = "*"

            strdx(1) = strdx(0)

            strdx(0) = "0"

       Else : Select Casecalcount1

                Case "+"

                    calcount2 = "*"

                    strdx(2) = strdx(0)

                    strdx(0) = "0"

                Case "-"

                    calcount2 = "*"

                    strdx(2) = strdx(0)

                    strdx(0) = "0"

                Case "*"

                    strdx(1) =Str(Val(strdx(0)) * Val(strdx(1)))

                    strdx(0) = "0"

                    calcount1 = "*"

                Case "/"

                    strdx(1) =Str(Val(strdx(1)) / Val(strdx(0)))

                    strdx(0) = "0"

                    calcount1 = "*"

            End Select

       End If

End Sub

 

进入Button15.Click事件中.代码如下

  Private SubButton15_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button15.Click

       If calcount1 = "0" Then

            calcount1 = "/"

            strdx(1) = strdx(0)

            strdx(0) = "0"

       Else : Select Casecalcount1

                Case "+"

                    calcount2 = "/"

                    strdx(2) = strdx(0)

                    strdx(0) = "0"

                Case "-"

                    calcount2 = "/"

                    strdx(2) = strdx(0)

                    strdx(0) = "0"

                Case "*"

                    strdx(1) =Str(Val(strdx(0)) * Val(strdx(1)))

                    strdx(0) = "0"

                    calcount1 = "/"

                Case "/"

                    strdx(1) =Str(Val(strdx(1)) / Val(strdx(0)))

                    strdx(0) = "0"

                    calcount1 = "/"

            End Select

       End If

End Sub

进入Button16.Click事件中.代码如下

Private SubButton16_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button16.Click

       If strdx(0) = "0" Then

            strdx(0) = "7"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "7"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "7"

            TextBox1.Text = strdx(0)

       End If

End Sub

 

进入Button17.Click事件中.代码如下

 

 

Private SubButton17_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button17.Click

       If strdx(0) = "0" Then

            strdx(0) = "8"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "8"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "8"

            TextBox1.Text = strdx(0)

       End If

End Sub

进入Button18.Click事件中.代码如下

 

   Private SubButton18_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button18.Click

       If strdx(0) = "0" Then

            strdx(0) = "9"

            TextBox1.Text = strdx(0) & "."

       ElseIf strvalue = False Then

            strdx(0) = strdx(0) & "9"

            TextBox1.Text = strdx(0) & "."

       Else

            strdx(0) = strdx(0) & "9"

            TextBox1.Text = strdx(0)

       End If

End Sub

 

进入Button19.Click事件中.代码如下

Private SubButton19_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button19.Click

       TextBox1.Text = "0."

End Sub

 

进入Button20.Click事件中.代码如下

Private SubButton20_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button20.Click

       Me.Close()

 

End Sub

 

最后的代码如图2:

 
图2

程序代码输入完成,接下来我们来运行一下程序,进行测试。最后程序运行效果如图3

 
图3

程序运行稳定能够进行正常的加减乘除等,如果你觉得功能不够,你还可以再去添加一些新的功能。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值