VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net 教程 3-11 窗体控件综合编程1

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
通过之前的学习,大家对窗体和控件知识已经有了初步的理解,本节开始将进行一些综合编程训练。

 

先从简单开始,综合编程1 计算器。

先考虑一下计算器的设计,可以参考windows的计算器。最基本那种就可以了。

下面是一个计算器控件摆放和命名:

txtValue的readonly属性设置为true;按钮的FlatStyle属性设置为Popup。

代码比较容易懂,我直接给出来:

 


  1.  
    '做运算的第一个数字
  2.  
    Dim numFirst As Double
  3.  
    '做运算的第二个数字
  4.  
    Dim numSecond As Double
  5.  
    '运算符号
  6.  
    Dim operatesymbol As String
  7.  
     
  8.  
    '清除
  9.  
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
  10.  
    txtValue.Text = "0"
  11.  
    operatesymbol = ""
  12.  
    End Sub
  13.  
     
  14.  
    '删除最后一个数字
  15.  
    Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
  16.  
    If txtValue.Text.Length = 1 Then
  17.  
    txtValue.Text = "0"
  18.  
    ElseIf txtValue.Text.Length > 1 Then
  19.  
    txtValue.Text = txtValue.Text.Substring(0, txtValue.Text.Length - 1)
  20.  
    End If
  21.  
    End Sub
  22.  
     
  23.  
    '按下数字按钮
  24.  
    Private Sub btn1_Click(sender As Object, e As EventArgs) Handles _
  25.  
    btn1.Click, btn2.Click, btn3.Click,
  26.  
    btn4.Click, btn5.Click, btn6.Click,
  27.  
    btn7.Click, btn8.Click, btn9.Click
  28.  
    Dim btnclick As Button = CType(sender, Button)
  29.  
    Dim addnumber As String = btnclick.Text
  30.  
    Select Case txtValue.Text
  31.  
    Case "0"
  32.  
    txtValue.Text = addnumber
  33.  
    Case "-0"
  34.  
    txtValue.Text = "-" & addnumber
  35.  
    Case Else
  36.  
    txtValue.Text &= addnumber
  37.  
    End Select
  38.  
    End Sub
  39.  
     
  40.  
    '按下小数点
  41.  
    Private Sub btnDot_Click(sender As Object, e As EventArgs) Handles btnDot.Click
  42.  
    txtValue.Text &= "."
  43.  
    End Sub
  44.  
     
  45.  
    '按下正负号按钮
  46.  
    Private Sub btnNegPos_Click(sender As Object, e As EventArgs) Handles btnNegPos.Click
  47.  
    If txtValue.Text.Substring(0, 1) = "-" Then
  48.  
    txtValue.Text = txtValue.Text.Substring(1, txtValue.Text.Length - 1)
  49.  
    Else
  50.  
    txtValue.Text = "-" & txtValue.Text
  51.  
    End If
  52.  
    End Sub
  53.  
     
  54.  
    '按下运算符按钮
  55.  
    Private Sub btnOperate_Click(sender As Object, e As EventArgs) Handles btnAdd.Click, btnSub.Click, btnDiv.Click, btnMul.Click
  56.  
    Dim btnclick As Button = CType(sender, Button)
  57.  
    numFirst = Double.Parse(txtValue.Text)
  58.  
    operatesymbol = btnclick.Text
  59.  
    txtValue.Text = "0"
  60.  
    End Sub
  61.  
     
  62.  
    '按下等于按钮
  63.  
    Private Sub btnEqual_Click(sender As Object, e As EventArgs) Handles btnEqual.Click
  64.  
    numSecond = Double.Parse(txtValue.Text)
  65.  
    Dim dbValue As Double
  66.  
     
  67.  
    Select Case operatesymbol
  68.  
    Case "+"
  69.  
    dbValue = numFirst + numSecond
  70.  
    Case "-"
  71.  
    dbValue = numFirst - numSecond
  72.  
    Case "*"
  73.  
    dbValue = numFirst * numSecond
  74.  
    Case "/"
  75.  
    dbValue = numFirst / numSecond
  76.  
    Case ""
  77.  
    Exit Sub
  78.  
    Case Else
  79.  
    Exit Sub
  80.  
    End Select
  81.  
    Dim strValue As String = dbValue.ToString
  82.  
     
  83.  
    txtValue.Text = strValue
  84.  
    End Sub
  85.  
     
  86.  
    '按下平方按钮
  87.  
    Private Sub btnSquare_Click(sender As Object, e As EventArgs) Handles btnSquare.Click
  88.  
    Dim btnclick As Button = CType(sender, Button)
  89.  
    numFirst = Double.Parse(txtValue.Text)
  90.  
     
  91.  
    Dim dbValue As Double
  92.  
    dbValue = numFirst ^ 2
  93.  
    Dim strValue As String = dbValue.ToString
  94.  
     
  95.  
    txtValue.Text = strValue
  96.  
    End Sub

 

 

 

 

 

 

也显而易见代码中还存在些bug,请试着改正:

1、运行完毕后,数字按钮按下将会添加到运算结果后面;

2、数字除以0或者结果超大的时候会显示“正无穷大”,任然可以参与运算,但是会导致错误;

3、多次按下运算符会导致误认为第一个做计算的数字为0,导致结果不正确。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/UruseiBest/article/details/72824935

相关教程