[VISUAL BASIC .net][CASE4_3]一個計算器。
一個計算器。自己心血來潮用VB2008和SHARPDEVELOP分別寫了一個計算器。
VB2008部分:
建立新的WPF應用程式專案,命名為Case4_3。
(由於WPF控件沒有NUMERICUPANDDOWN控件,所以我自己湊了一個)
Window1.xaml(窗體布局用):
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Case4_3" Height="241" Width="331" FontFamily="Microsoft Jhenghei,微軟正黑體">
<Grid Background="LightSteelBlue">
<TextBox Height="24" HorizontalAlignment="Left" Margin="19,22,0,0" Name="txtOperand1" VerticalAlignment="Top" Width="112" />
<ComboBox Height="24" HorizontalAlignment="Left" Margin="20,58,0,0" Name="cboOperator" VerticalAlignment="Top" Width="112">
<ComboBoxItem>+</ComboBoxItem>
<ComboBoxItem>-</ComboBoxItem>
<ComboBoxItem>*</ComboBoxItem>
<ComboBoxItem>/</ComboBoxItem>
<ComboBoxItem>\</ComboBoxItem>
<ComboBoxItem>^</ComboBoxItem>
<ComboBoxItem>Mod</ComboBoxItem>
</ComboBox>
<TextBox Height="24" HorizontalAlignment="Left" Margin="20,94,0,0" Name="numupdOperand2" VerticalAlignment="Top" Width="61" />
<Button Height="24" HorizontalAlignment="Left" Margin="87,94,0,0" Name="numPlus" VerticalAlignment="Top" Width="20" BorderThickness="0" Visibility="Visible" ClipToBounds="False">▲</Button>
<Button Height="24" HorizontalAlignment="Left" Margin="113,94,0,0" Name="numMinus" VerticalAlignment="Top" Width="20">▼</Button>
<CheckBox HorizontalAlignment="Left" Margin="22,130,0,0" Name="chkSave" Width="113" Height="24" VerticalAlignment="Top" IsChecked="False">存儲運算結果</CheckBox>
<Button HorizontalAlignment="Left" Margin="21,156,0,0" Name="btnCalculate" Width="30" Height="24" VerticalAlignment="Top">=</Button>
<Label Height="24" HorizontalAlignment="Left" Margin="57,156,0,0" Name="lblResult" VerticalAlignment="Top" Width="78" BorderThickness="0" Background="LightGray"></Label>
<ListBox Margin="148,22,27,23" Name="lstResults" />
</Grid>
</Window>
Window1.xaml.vb:
Class Window1
Private Sub numPlus_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles numPlus.Click
Try
numupdOperand2.Text = numupdOperand2.Text + 1
Catch ex As InvalidCastException
MessageBox.Show("您貌似沒有輸入正確的數值呢。", "提示資訊")
Exit Sub
End Try
End Sub
Private Sub numMinus_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles numMinus.Click
Try
numupdOperand2.Text = numupdOperand2.Text - 1
Catch ex As InvalidCastException
MessageBox.Show("您貌似沒有輸入正確的數值呢。", "提示資訊")
Exit Sub
End Try
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCalculate.Click
Dim iOperand1 As Long, iOperand2 As Long, iResult As Long, cOperator As Char
Try
iOperand1 = CInt(txtOperand1.Text)
iOperand2 = numupdOperand2.Text
cOperator = cboOperator.Text
Select Case cOperator
Case "+"
iResult = iOperand1 + iOperand2
Case "-"
iResult = iOperand1 - iOperand2
Case "*"
iResult = iOperand1 * iOperand2
Case "^"
iResult = iOperand1 ^ iOperand2
Case "/"
iResult = iOperand1 / iOperand2
Case "\"
iResult = iOperand1 \ iOperand2
Case "Mod"
iResult = iOperand1 Mod iOperand2
Case Else
MessageBox.Show("您貌似還沒有選擇正確的運算符號呢。", "提示資訊")
Exit Sub
End Select
lblResult.Content = CStr(iResult)
If chkSave.IsChecked = True Then lstResults.Items.Add(CStr(iResult))
Catch ex As OverflowException
MessageBox.Show("發生運算溢出現象,貌似您的運算域取值過大。", "提示資訊")
Exit Sub
Catch ex As InvalidCastException
MessageBox.Show("您貌似沒有輸入正確的數值呢。", "提示資訊")
Exit Sub
Catch ex As DivideByZeroException
MessageBox.Show("除數怎麼可以是零呢?", "提示資訊")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "錯誤提示資訊")
Exit Sub
End Try
End Sub
End Class
這樣寫出來的程式估計對WindowsXP無法很好地支援,所以下面用#develop的VB模式寫一個標準.net EXE專案(VB2008中也可):
------------------------------------------------
SharpDevelop 3.x 部分:
建立新的VB Windowsapplication應用程式專案,命名為Case4_3。
Mainform.designer.vb(窗體布局相關):
'
' Created by SharpDevelop.
' User: ShinSabre
' Date: 2009/3/21
' Time: 11:33
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Partial Class MainForm
Inherits System.Windows.Forms.Form
''' <summary>
''' Designer variable used to keep track of non-visual components.
''' </summary>
Private components As System.ComponentModel.IContainer
''' <summary>
''' Disposes resources used by the form.
''' </summary>
''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
''' <summary>
''' This method is required for Windows Forms designer support.
''' Do not change the method contents inside the source code editor. The Forms designer might
''' not be able to load this method if it was changed manually.
''' </summary>
Private Sub InitializeComponent()
Me.txtOperand1 = New System.Windows.Forms.TextBox
Me.cboOperator = New System.Windows.Forms.ComboBox
Me.numupdOperand2 = New System.Windows.Forms.NumericUpDown
Me.chkSave = New System.Windows.Forms.CheckBox
Me.btnCalculate = New System.Windows.Forms.Button
Me.lblResult = New System.Windows.Forms.Label
Me.lstResults = New System.Windows.Forms.ListBox
CType(Me.numupdOperand2,System.ComponentModel.ISupportInitialize).BeginInit
Me.SuspendLayout
'
'txtOperand1
'
Me.txtOperand1.Font = New System.Drawing.Font("宋体", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136,Byte))
Me.txtOperand1.Location = New System.Drawing.Point(12, 12)
Me.txtOperand1.Name = "txtOperand1"
Me.txtOperand1.Size = New System.Drawing.Size(112, 21)
Me.txtOperand1.TabIndex = 0
'
'cboOperator
'
Me.cboOperator.FormattingEnabled = true
Me.cboOperator.Items.AddRange(New Object() {"+", "-", "*", "/", "\", "^", "Mod"})
Me.cboOperator.Location = New System.Drawing.Point(12, 39)
Me.cboOperator.Name = "cboOperator"
Me.cboOperator.Size = New System.Drawing.Size(112, 20)
Me.cboOperator.TabIndex = 1
'
'numupdOperand2
'
Me.numupdOperand2.Location = New System.Drawing.Point(12, 64)
Me.numupdOperand2.Name = "numupdOperand2"
Me.numupdOperand2.Size = New System.Drawing.Size(112, 21)
Me.numupdOperand2.TabIndex = 2
'
'chkSave
'
Me.chkSave.Location = New System.Drawing.Point(12, 91)
Me.chkSave.Name = "chkSave"
Me.chkSave.Size = New System.Drawing.Size(112, 17)
Me.chkSave.TabIndex = 3
Me.chkSave.Text = "存儲運算結果"
Me.chkSave.UseVisualStyleBackColor = true
'
'btnCalculate
'
Me.btnCalculate.Location = New System.Drawing.Point(12, 113)
Me.btnCalculate.Name = "btnCalculate"
Me.btnCalculate.Size = New System.Drawing.Size(25, 23)
Me.btnCalculate.TabIndex = 4
Me.btnCalculate.Text = "="
Me.btnCalculate.UseVisualStyleBackColor = true
AddHandler Me.btnCalculate.Click, AddressOf Me.BtnCalculateClick
'
'lblResult
'
Me.lblResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.lblResult.Location = New System.Drawing.Point(43, 113)
Me.lblResult.Name = "lblResult"
Me.lblResult.Size = New System.Drawing.Size(81, 23)
Me.lblResult.TabIndex = 5
Me.lblResult.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'lstResults
'
Me.lstResults.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom)_
Or System.Windows.Forms.AnchorStyles.Left)_
Or System.Windows.Forms.AnchorStyles.Right),System.Windows.Forms.AnchorStyles)
Me.lstResults.FormattingEnabled = true
Me.lstResults.ItemHeight = 12
Me.lstResults.Location = New System.Drawing.Point(139, 12)
Me.lstResults.Name = "lstResults"
Me.lstResults.Size = New System.Drawing.Size(164, 124)
Me.lstResults.TabIndex = 6
'
'MainForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6!, 12!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(315, 152)
Me.Controls.Add(Me.lstResults)
Me.Controls.Add(Me.lblResult)
Me.Controls.Add(Me.btnCalculate)
Me.Controls.Add(Me.chkSave)
Me.Controls.Add(Me.numupdOperand2)
Me.Controls.Add(Me.cboOperator)
Me.Controls.Add(Me.txtOperand1)
Me.Font = New System.Drawing.Font("宋体", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136,Byte))
Me.Name = "MainForm"
Me.Text = "Case4_3"
CType(Me.numupdOperand2,System.ComponentModel.ISupportInitialize).EndInit
Me.ResumeLayout(false)
Me.PerformLayout
End Sub
Private lstResults As System.Windows.Forms.ListBox
Private lblResult As System.Windows.Forms.Label
Private btnCalculate As System.Windows.Forms.Button
Private chkSave As System.Windows.Forms.CheckBox
Private numupdOperand2 As System.Windows.Forms.NumericUpDown
Private cboOperator As System.Windows.Forms.ComboBox
Private txtOperand1 As System.Windows.Forms.TextBox
End Class
Mainform.vb
'
' Created by SharpDevelop.
' User: ShinSabre
' Date: 2009/3/21
' Time: 11:33
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Public Partial Class MainForm
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
'
' TODO : Add constructor code after InitializeComponents
'
End Sub
Sub BtnCalculateClick(sender As Object, e As EventArgs)
Dim iOperand1 As Long, iOperand2 As Long, iResult As Long, cOperator As Char
Try
iOperand1 = CInt(txtOperand1.Text)
iOperand2 = numupdOperand2.Text
cOperator = cboOperator.Text
Select Case cOperator
Case "+"
iResult = iOperand1 + iOperand2
Case "-"
iResult = iOperand1 - iOperand2
Case "*"
iResult = iOperand1 * iOperand2
Case "^"
iResult = iOperand1 ^ iOperand2
Case "/"
iResult = iOperand1 / iOperand2
Case "\"
iResult = iOperand1 \ iOperand2
Case "Mod"
iResult = iOperand1 Mod iOperand2
Case Else
MessageBox.Show("您貌似還沒有選擇正確的運算符號呢。", "提示資訊")
Exit Sub
End Select
lblResult.Text = CStr(iResult)
If chkSave.Checked = True Then lstResults.Items.Add(CStr(iResult))
Catch ex As OverflowException
MessageBox.Show("發生運算溢出現象,貌似您的運算域取值過大。", "提示資訊")
Exit Sub
Catch ex As InvalidCastException
Exit Sub
Catch ex As DivideByZeroException
MessageBox.Show("除數怎麼可以是零呢?", "提示資訊")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "錯誤提示資訊")
Exit Sub
End Try
End Sub
End Class
這個由夏娜測試可以在Windows XP+.net Framework 2.x上運行。
页:
[1]