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 Class Form1
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 Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.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 Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
strvalue = True
strdx(0) = strdx(0) & "."
TextBox1.Text = strdx(0)
End Sub
|