幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2890|回复: 0

[VISUAL BASIC .net][CASE4_3]一個計算器。

[复制链接]

49

主题

189

帖子

2339

积分

禁止发言

CSCN 汉化组

积分
2339
发表于 2009-3-21 11:59:30 | 显示全部楼层 |阅读模式
[VISUAL BASIC .net][CASE4_3]一個計算器。
自己心血來潮用VB2008和SHARPDEVELOP分別寫了一個計算器。

VB2008部分:
建立新的WPF應用程式專案,命名為Case4_3。
(由於WPF控件沒有NUMERICUPANDDOWN控件,所以我自己湊了一個)

Window1.xaml(窗體布局用):
  1. <Window x:Class="Window1"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.     Title="Case4_3" Height="241" Width="331" FontFamily="Microsoft Jhenghei,微軟正黑體">
  5.     <Grid Background="LightSteelBlue">
  6.         <TextBox Height="24" HorizontalAlignment="Left" Margin="19,22,0,0" Name="txtOperand1" VerticalAlignment="Top" Width="112" />
  7.         <ComboBox Height="24" HorizontalAlignment="Left" Margin="20,58,0,0" Name="cboOperator" VerticalAlignment="Top" Width="112">
  8.             <ComboBoxItem>+</ComboBoxItem>
  9.             <ComboBoxItem>-</ComboBoxItem>
  10.             <ComboBoxItem>*</ComboBoxItem>
  11.             <ComboBoxItem>/</ComboBoxItem>
  12.             <ComboBoxItem>\</ComboBoxItem>
  13.             <ComboBoxItem>^</ComboBoxItem>
  14.             <ComboBoxItem>Mod</ComboBoxItem>
  15.         </ComboBox>
  16.         <TextBox Height="24" HorizontalAlignment="Left" Margin="20,94,0,0" Name="numupdOperand2" VerticalAlignment="Top" Width="61" />
  17.         <Button Height="24" HorizontalAlignment="Left" Margin="87,94,0,0" Name="numPlus" VerticalAlignment="Top" Width="20" BorderThickness="0" Visibility="Visible" ClipToBounds="False">▲</Button>
  18.         <Button Height="24" HorizontalAlignment="Left" Margin="113,94,0,0" Name="numMinus" VerticalAlignment="Top" Width="20">▼</Button>
  19.         <CheckBox HorizontalAlignment="Left" Margin="22,130,0,0" Name="chkSave" Width="113" Height="24" VerticalAlignment="Top" IsChecked="False">存儲運算結果</CheckBox>
  20.         <Button HorizontalAlignment="Left" Margin="21,156,0,0" Name="btnCalculate" Width="30" Height="24" VerticalAlignment="Top">=</Button>
  21.         <Label Height="24" HorizontalAlignment="Left" Margin="57,156,0,0" Name="lblResult" VerticalAlignment="Top" Width="78" BorderThickness="0" Background="LightGray"></Label>
  22.         <ListBox Margin="148,22,27,23" Name="lstResults" />
  23.     </Grid>
  24. </Window>
复制代码

Window1.xaml.vb:
  1. Class Window1
  2.     Private Sub numPlus_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles numPlus.Click
  3.         Try
  4.             numupdOperand2.Text = numupdOperand2.Text + 1
  5.         Catch ex As InvalidCastException
  6.             MessageBox.Show("您貌似沒有輸入正確的數值呢。", "提示資訊")
  7.             Exit Sub
  8.         End Try
  9.     End Sub
  10.     Private Sub numMinus_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles numMinus.Click
  11.         Try
  12.             numupdOperand2.Text = numupdOperand2.Text - 1
  13.         Catch ex As InvalidCastException
  14.             MessageBox.Show("您貌似沒有輸入正確的數值呢。", "提示資訊")
  15.             Exit Sub
  16.         End Try
  17.     End Sub
  18.     Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCalculate.Click
  19.         Dim iOperand1 As Long, iOperand2 As Long, iResult As Long, cOperator As Char
  20.         Try
  21.             iOperand1 = CInt(txtOperand1.Text)
  22.             iOperand2 = numupdOperand2.Text
  23.             cOperator = cboOperator.Text
  24.             Select Case cOperator
  25.                 Case "+"
  26.                     iResult = iOperand1 + iOperand2
  27.                 Case "-"
  28.                     iResult = iOperand1 - iOperand2
  29.                 Case "*"
  30.                     iResult = iOperand1 * iOperand2
  31.                 Case "^"
  32.                     iResult = iOperand1 ^ iOperand2
  33.                 Case "/"
  34.                     iResult = iOperand1 / iOperand2
  35.                 Case ""
  36.                     iResult = iOperand1 \ iOperand2
  37.                 Case "Mod"
  38.                     iResult = iOperand1 Mod iOperand2
  39.                 Case Else
  40.                     MessageBox.Show("您貌似還沒有選擇正確的運算符號呢。", "提示資訊")
  41.                     Exit Sub
  42.             End Select
  43.             lblResult.Content = CStr(iResult)
  44.             If chkSave.IsChecked = True Then lstResults.Items.Add(CStr(iResult))
  45.         Catch ex As OverflowException
  46.             MessageBox.Show("發生運算溢出現象,貌似您的運算域取值過大。", "提示資訊")
  47.             Exit Sub
  48.         Catch ex As InvalidCastException
  49.             MessageBox.Show("您貌似沒有輸入正確的數值呢。", "提示資訊")
  50.             Exit Sub
  51.         Catch ex As DivideByZeroException
  52.             MessageBox.Show("除數怎麼可以是零呢?", "提示資訊")
  53.             Exit Sub
  54.         Catch ex As Exception
  55.             MessageBox.Show(ex.Message, "錯誤提示資訊")
  56.             Exit Sub
  57.         End Try
  58.     End Sub
  59. End Class
复制代码
這樣寫出來的程式估計對WindowsXP無法很好地支援,所以下面用#develop的VB模式寫一個標準.net EXE專案(VB2008中也可):
------------------------------------------------
SharpDevelop 3.x 部分:
建立新的VB Windowsapplication應用程式專案,命名為Case4_3。

Mainform.designer.vb(窗體布局相關):
  1. &#39;
  2. &#39; Created by SharpDevelop.
  3. &#39; User: ShinSabre
  4. &#39; Date: 2009/3/21
  5. &#39; Time: 11:33
  6. &#39;
  7. &#39; To change this template use Tools | Options | Coding | Edit Standard Headers.
  8. &#39;
  9. Partial Class MainForm
  10.     Inherits System.Windows.Forms.Form
  11.    
  12.     &#39;&#39;&#39; <summary>
  13.     &#39;&#39;&#39; Designer variable used to keep track of non-visual components.
  14.     &#39;&#39;&#39; </summary>
  15.     Private components As System.ComponentModel.IContainer
  16.    
  17.     &#39;&#39;&#39; <summary>
  18.     &#39;&#39;&#39; Disposes resources used by the form.
  19.     &#39;&#39;&#39; </summary>
  20.     &#39;&#39;&#39; <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  21.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  22.         If disposing Then
  23.             If components IsNot Nothing Then
  24.                 components.Dispose()
  25.             End If
  26.         End If
  27.         MyBase.Dispose(disposing)
  28.     End Sub
  29.    
  30.     &#39;&#39;&#39; <summary>
  31.     &#39;&#39;&#39; This method is required for Windows Forms designer support.
  32.     &#39;&#39;&#39; Do not change the method contents inside the source code editor. The Forms designer might
  33.     &#39;&#39;&#39; not be able to load this method if it was changed manually.
  34.     &#39;&#39;&#39; </summary>
  35.     Private Sub InitializeComponent()
  36.         Me.txtOperand1 = New System.Windows.Forms.TextBox
  37.         Me.cboOperator = New System.Windows.Forms.ComboBox
  38.         Me.numupdOperand2 = New System.Windows.Forms.NumericUpDown
  39.         Me.chkSave = New System.Windows.Forms.CheckBox
  40.         Me.btnCalculate = New System.Windows.Forms.Button
  41.         Me.lblResult = New System.Windows.Forms.Label
  42.         Me.lstResults = New System.Windows.Forms.ListBox
  43.         CType(Me.numupdOperand2,System.ComponentModel.ISupportInitialize).BeginInit
  44.         Me.SuspendLayout
  45.         &#39;
  46.         &#39;txtOperand1
  47.         &#39;
  48.         Me.txtOperand1.Font = New System.Drawing.Font("宋体", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136,Byte))
  49.         Me.txtOperand1.Location = New System.Drawing.Point(12, 12)
  50.         Me.txtOperand1.Name = "txtOperand1"
  51.         Me.txtOperand1.Size = New System.Drawing.Size(112, 21)
  52.         Me.txtOperand1.TabIndex = 0
  53.         &#39;
  54.         &#39;cboOperator
  55.         &#39;
  56.         Me.cboOperator.FormattingEnabled = true
  57.         Me.cboOperator.Items.AddRange(New Object() {"+", "-", "*", "/", "", "^", "Mod"})
  58.         Me.cboOperator.Location = New System.Drawing.Point(12, 39)
  59.         Me.cboOperator.Name = "cboOperator"
  60.         Me.cboOperator.Size = New System.Drawing.Size(112, 20)
  61.         Me.cboOperator.TabIndex = 1
  62.         &#39;
  63.         &#39;numupdOperand2
  64.         &#39;
  65.         Me.numupdOperand2.Location = New System.Drawing.Point(12, 64)
  66.         Me.numupdOperand2.Name = "numupdOperand2"
  67.         Me.numupdOperand2.Size = New System.Drawing.Size(112, 21)
  68.         Me.numupdOperand2.TabIndex = 2
  69.         &#39;
  70.         &#39;chkSave
  71.         &#39;
  72.         Me.chkSave.Location = New System.Drawing.Point(12, 91)
  73.         Me.chkSave.Name = "chkSave"
  74.         Me.chkSave.Size = New System.Drawing.Size(112, 17)
  75.         Me.chkSave.TabIndex = 3
  76.         Me.chkSave.Text = "存儲運算結果"
  77.         Me.chkSave.UseVisualStyleBackColor = true
  78.         &#39;
  79.         &#39;btnCalculate
  80.         &#39;
  81.         Me.btnCalculate.Location = New System.Drawing.Point(12, 113)
  82.         Me.btnCalculate.Name = "btnCalculate"
  83.         Me.btnCalculate.Size = New System.Drawing.Size(25, 23)
  84.         Me.btnCalculate.TabIndex = 4
  85.         Me.btnCalculate.Text = "="
  86.         Me.btnCalculate.UseVisualStyleBackColor = true
  87.         AddHandler Me.btnCalculate.Click, AddressOf Me.BtnCalculateClick
  88.         &#39;
  89.         &#39;lblResult
  90.         &#39;
  91.         Me.lblResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
  92.         Me.lblResult.Location = New System.Drawing.Point(43, 113)
  93.         Me.lblResult.Name = "lblResult"
  94.         Me.lblResult.Size = New System.Drawing.Size(81, 23)
  95.         Me.lblResult.TabIndex = 5
  96.         Me.lblResult.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
  97.         &#39;
  98.         &#39;lstResults
  99.         &#39;
  100.         Me.lstResults.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom)  _
  101.                         Or System.Windows.Forms.AnchorStyles.Left)  _
  102.                         Or System.Windows.Forms.AnchorStyles.Right),System.Windows.Forms.AnchorStyles)
  103.         Me.lstResults.FormattingEnabled = true
  104.         Me.lstResults.ItemHeight = 12
  105.         Me.lstResults.Location = New System.Drawing.Point(139, 12)
  106.         Me.lstResults.Name = "lstResults"
  107.         Me.lstResults.Size = New System.Drawing.Size(164, 124)
  108.         Me.lstResults.TabIndex = 6
  109.         &#39;
  110.         &#39;MainForm
  111.         &#39;
  112.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6!, 12!)
  113.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  114.         Me.ClientSize = New System.Drawing.Size(315, 152)
  115.         Me.Controls.Add(Me.lstResults)
  116.         Me.Controls.Add(Me.lblResult)
  117.         Me.Controls.Add(Me.btnCalculate)
  118.         Me.Controls.Add(Me.chkSave)
  119.         Me.Controls.Add(Me.numupdOperand2)
  120.         Me.Controls.Add(Me.cboOperator)
  121.         Me.Controls.Add(Me.txtOperand1)
  122.         Me.Font = New System.Drawing.Font("宋体", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136,Byte))
  123.         Me.Name = "MainForm"
  124.         Me.Text = "Case4_3"
  125.         CType(Me.numupdOperand2,System.ComponentModel.ISupportInitialize).EndInit
  126.         Me.ResumeLayout(false)
  127.         Me.PerformLayout
  128.     End Sub
  129.     Private lstResults As System.Windows.Forms.ListBox
  130.     Private lblResult As System.Windows.Forms.Label
  131.     Private btnCalculate As System.Windows.Forms.Button
  132.     Private chkSave As System.Windows.Forms.CheckBox
  133.     Private numupdOperand2 As System.Windows.Forms.NumericUpDown
  134.     Private cboOperator As System.Windows.Forms.ComboBox
  135.     Private txtOperand1 As System.Windows.Forms.TextBox
  136. End Class
复制代码
Mainform.vb
  1. &#39;
  2. &#39; Created by SharpDevelop.
  3. &#39; User: ShinSabre
  4. &#39; Date: 2009/3/21
  5. &#39; Time: 11:33
  6. &#39;
  7. &#39; To change this template use Tools | Options | Coding | Edit Standard Headers.
  8. &#39;
  9. Public Partial Class MainForm
  10.     Public Sub New()
  11.         &#39; The Me.InitializeComponent call is required for Windows Forms designer support.
  12.         Me.InitializeComponent()
  13.         
  14.         &#39;
  15.         &#39; TODO : Add constructor code after InitializeComponents
  16.         &#39;
  17.     End Sub
  18.    
  19.     Sub BtnCalculateClick(sender As Object, e As EventArgs)
  20.         Dim iOperand1 As Long, iOperand2 As Long, iResult As Long, cOperator As Char
  21.         Try
  22.             iOperand1 = CInt(txtOperand1.Text)
  23.             iOperand2 = numupdOperand2.Text
  24.             cOperator = cboOperator.Text
  25.             Select Case cOperator
  26.                 Case "+"
  27.                     iResult = iOperand1 + iOperand2
  28.                 Case "-"
  29.                     iResult = iOperand1 - iOperand2
  30.                 Case "*"
  31.                     iResult = iOperand1 * iOperand2
  32.                 Case "^"
  33.                     iResult = iOperand1 ^ iOperand2
  34.                 Case "/"
  35.                     iResult = iOperand1 / iOperand2
  36.                 Case ""
  37.                     iResult = iOperand1 \ iOperand2
  38.                 Case "Mod"
  39.                     iResult = iOperand1 Mod iOperand2
  40.                 Case Else
  41.                     MessageBox.Show("您貌似還沒有選擇正確的運算符號呢。", "提示資訊")
  42.                     Exit Sub
  43.             End Select
  44.             lblResult.Text = CStr(iResult)
  45.             If chkSave.Checked = True Then lstResults.Items.Add(CStr(iResult))
  46.         Catch ex As OverflowException
  47.             MessageBox.Show("發生運算溢出現象,貌似您的運算域取值過大。", "提示資訊")
  48.             Exit Sub
  49.         Catch ex As InvalidCastException
  50.             Exit Sub
  51.         Catch ex As DivideByZeroException
  52.             MessageBox.Show("除數怎麼可以是零呢?", "提示資訊")
  53.             Exit Sub
  54.         Catch ex As Exception
  55.             MessageBox.Show(ex.Message, "錯誤提示資訊")
  56.             Exit Sub
  57.         End Try
  58.     End Sub
  59. End Class
复制代码
這個由夏娜測試可以在Windows XP+.net Framework 2.x上運行。
Cleaned.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-3-29 03:49 , Processed in 0.020378 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表