走近VB.Net(四) 關於資料型別與示例 (轉)

gugu99發表於2008-06-25
走近VB.Net(四) 關於資料型別與示例 (轉)[@more@]

 

 

 

word.Document name=ProgId> microsoft Word 9" name=Generator> .NET4.files/filelist." rel=File-List>

走近(四) 關於資料型別與示例

在前面幾章談得最多的是Variant()到(vb.net)的轉換,Object被稱為通用的資料型別。另外是32位的long(vb6)被integer(vb.net)所取代,在vb.net中long儲存64位的帶符號整數。而short16位的數字取代vb6的integer的位置。而我們在下面要談的是Decimal資料型別。

在初學vb6的時候,可能所有的人都做過同一個“計算器”,你可能看到在計算大一點的數字的時候,結果以指數形式出現,如:xxxxE+yyyy。這往往會讓使用這個計算器的人莫名其妙,有些人甚至不理解是什麼意思。有一些的可能會使用format使他用實際的數字出現,可是問題又有了,formatr的小數位是固定的,如果你定為5位小數,那麼只有一位小數,他也會在後面出現4個零。當然你很快會用字元處理的方法清除後面的零,這對於你輕而易舉。可是你會發現他的計算結果被限定於一定的位,後面全部是零,而不管他實際是什麼,就是完全的不精確,甚至於不可靠。而VB6中提供了Currency型別,一般稱為貨幣型別。提供精確的定點運算,不過他只有四位小數,也就是說,哪怕你實際需要的是八位,他也只能有四位。而且在超出了有限的範圍時,你必須捕獲這個錯誤,並把他地轉換到浮點運算。

而在VB.et中為你提供了Decimal的資料型別取代原有的Currency,Decimal儲存帶有符號的96位整數,以及28位小數,而且他能動地支配小數的位數,當你的數字表現為很大的整數時,他會根據資料的大小減少小數位數而獲取最大的準確度。也就是說,我們可以做出一個比自帶的計算器更精確的計算器了,想到這麼容易,應該可以堅定你繼續學習VB.Net的信心。

新建一個工程,新增一個welcome窗體

一定要弄一幅很cool的圖片作welcome的背景圖片
 
新增timer1

在工程管理視窗右鍵點選工程名,點選最後一項屬性,設定startup object welcome

form1中新增textbox1在上,textbox2在下,button1在兩者中間,text=+.button2在下為“=”。Button3為“退出”

welcome的程式碼如下:

Option Strict Off '開啟Option Strict以後調節透明透的數字會有麻煩,總之用這個是沒辦法

Imports System.Drawing

Imports System.s

Imports System.ComponentModel

Public Class welcome

  Inherits System.WinForms.Form

 

  Public Sub New()

  MyBase.New()

  welcome = Me

  'This call is required by the Win FoDesigner.

  InitializeComponent()

  Me.BorderStyle = WinForms.FormBorderStyle.None '不要標題欄

  Me.Height = Me.BackgroundImage.Height '設定窗體高度等於圖片高度

  Me.Width = Me.BackgroundImage.Width  '設定窗體寬度等於圖片寬度

  timer1.Enabled = True '啟動定時器

  timer1.Interval = 1 : Me.Opacity = 0 '讓窗體 全透明

  'TODO: Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.

  Public Overrides Sub Dispose()

  MyBase.Dispose()

  components.Dispose()

  End Sub

 

#Region " Windows Form Designer generated code "

  'Required by the Windows Form Designer

  Private components As System.ComponentModel.Container

 

  Private WithEvents Timer1 As System.WinForms.Timer

 

  Dim WithEvents welcome As System.WinForms.Form

 

  'NOTE: The following procedure is required by the Windows Form Designer

  'It can be modified using the Windows Form Designer. 

  'Do not modify it using the code editor.

  Private Sub InitializeComponent()

  Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(welcome))

 

  Me.components = New System.ComponentModel.Container()

  Me.Timer1 = New System.WinForms.Timer(components)

 

  '@design Me.TrayHeight = 90

  '@design Me.TrayLargeIcon = False

  '@design Me.TrayAutoArrange = True

  '@design Timer1.SetLocation(New System.Drawing.Point(7, 7))

  Me.Text = "welcome"

  Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)

  Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)

 

  End Sub

 

#End Region

  Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)

  If Me.Opacity = 1 Then '顯示出來以後,就下面的程式碼

  Dim fm As Form1 = New form1() '這form1創造一個例項(instance),形成一個(object),分配一點。

  fm.Visible = True '顯示主窗體

  Me.Dispose() '你把他看作VB6的unload me吧

  Else

  Me.Opacity = Me.Opacity + 0.01 '慢慢地顯示窗體

  End If

  End Sub

End Class

猜猜這個窗體會產生什麼。。。。。。

在form1新增以下程式碼

Imports System.ComponentModel

Imports System.Drawing

Imports System.WinForms

 

 

Public Class Form1

  Inherits System.WinForms.Form

 

  Public Sub New()

  MyBase.New

 

  Form1 = Me

 

  'This call is required by the Win Form Designer.

  InitializeComponent() '在執行New過程(新建一個窗體)時初始化

  Me.BorderStyle = FormBorderStyle.None

  button2.Visible = False '隱藏button2

  textbox2.Visible = False '隱藏textbox2

 

 

 

  'TODO: Add any initialization after the InitializeComponent() call

  End Sub

 

  'Form overrides dispose to clean up the component list.

  Overrides Public Sub Dispose()

  MyBase.Dispose

  components.Dispose

  End Sub

 

#Region " Windows Form Designer generated code "

 

  'Required by the Windows Form Designer

  Private components As System.ComponentModel.Container

  Private WithEvents Button3 As System.WinForms.Button

  Private WithEvents Button2 As System.WinForms.Button

 

 

 

  Private WithEvents Button1 As System.WinForms.Button

  Private WithEvents TextBox2 As System.WinForms.TextBox

  Private WithEvents TextBox1 As System.WinForms.TextBox

 

  Dim WithEvents Form1 As System.WinForms.Form

 

  'NOTE: The following procedure is required by the Windows Form Designer

  'It can be modified using the Windows Form Designer. 

  'Do not modify it using the code editor.

  Private Sub InitializeComponent()

  Me.components = New System.ComponentModel.Container()

  Me.Button1 = New System.WinForms.Button()

  Me.TextBox2 = New System.WinForms.TextBox()

  Me.Button3 = New System.WinForms.Button()

  Me.TextBox1 = New System.WinForms.TextBox()

  Me.Button2 = New System.WinForms.Button()

 

  '@design Me.TrayHeight = 0

  '@design Me.TrayLargeIcon = False

  '@design Me.TrayAutoArrange = True

  Button1.Location = New System.Drawing.Point(176, 40)

  Button1.Size = New System.Drawing.Size(24, 24)

  Button1.TabIndex = 2

  Button1.Font = New System.Drawing.Font("宋體", 12!, System.Drawing.FontStyle.Bold)

  Button1.Text = "+"

 

  TextBox2.Location = New System.Drawing.Point(160, 72)

  TextBox2.TabIndex = 1

  TextBox2.Size = New System.Drawing.Size(256, 21)

 

  Button3.Location = New System.Drawing.Point(24, 104)

  Button3.Size = New System.Drawing.Size(64, 24)

  Button3.TabIndex = 4

  Button3.Text = "退出"

 

  TextBox1.Location = New System.Drawing.Point(16, 8)

  TextBox1.TabIndex = 0

  TextBox1.Size = New System.Drawing.Size(200, 21)

 

  Button2.Location = New System.Drawing.Point(360, 104)

  Button2.Size = New System.Drawing.Size(32, 24)

  Button2.TabIndex = 3

  Button2.Text = "="

  Me.Text = "Form1"

  Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)

  Me.BackColor = System.Drawing.Color.YellowGreen

  Me.ClientSize = New System.Drawing.Size(456, 141)

 

  Me.Controls.Add(Button3)

  Me.Controls.Add(Button2)

  Me.Controls.Add(Button1)

  Me.Controls.Add(TextBox2)

  Me.Controls.Add(TextBox1)

  End Sub

 

#End Region

 

  Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)

  End

  End Sub

 

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

  If TextBox1.Text = "" Then

  system.WinForms.MessageBox.Show("請正確輸入數字", "VB.Net計算器", messagebox.OK BitOr messagebox.IconAsterisk)

  Exit Sub

  End If

  textbox2.Visible = True '顯示textbox2

  textbox2.Text = "" '清空textbox2

  textbox2.Focus() '讓textbox2得到焦點

  button2.Visible = True '顯示button2

 

  End Sub

 

  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

  Dim Valx, Valy, Valz As Decimal '宣告三個decimal的變數

 

  Try

  valx = textbox1.Text.ToDecimal '取值一

  valy = textbox2.Text.ToDecimal '取值二

  Catch

  system.WinForms.MessageBox.Show("請正確輸入數字", "VB.Net計算器", messagebox.OK BitOr messagebox.IconAsterisk)

  Exit Sub

  End Try

  valy = valx + valy '計算結果

  textbox1.Text = valy.ToString '顯示結果

  textbox2.Visible = False  '隱藏textbox2

  button2.Visible = False '隱藏button2

  End Sub

 

  Private Sub Form1_MouseDown(ByVal eventSender As Object, ByVal e As System.WinForms.MouseEventArgs)

  'mousedown的事件中窗體中的下拉選單是找不到的,自己寫一個吧,加上(ByVal eventSender As Object, ByVal e As System.WinForms.MouseEventArgs)即可

  Me.Capture() = False '釋放滑鼠捕獲,等同於的ReleaseCapture()

  Me.SendMessage(&HA1S, 2, 0) '唔,這個就是哪個sendmessage的API了,只是第一個控制程式碼引數不再用了。

  End Sub

 

End Class

這是一個簡單得不能再簡單的程式了,有興趣接下去做吧,我以前在VB6做個一個語音計算器,相信不少人用個吧,你也做一個吧!練練手。

VB.Net中文站網址:


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-1006107/,如需轉載,請註明出處,否則將追究法律責任。

相關文章