走近VB.Net(四) 關於資料類型與樣本

來源:互聯網
上載者:User
樣本|資料|資料類型 走近VB。Net(四) 關於資料類型與樣本
在前面幾章談得最多的是Variant(vb6)到Object(vb.net)的轉換,Object被稱為通用的資料類型。另外是32位的long(vb6)被integer(vb.net)所取代,在vb.net中long儲存64位的帶正負號的整數。而short儲存16位的數字取代vb6的integer的位置。而我們在下面要談的是Decimal資料類型。

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

而在VB.Net中為你提供了Decimal的資料類型取代原有的Currency,Decimal儲存帶有符號的96位整數,以及28位小數,而且他能動地支配小數的位元,當你的數字表現為很大的整數時,他會根據資料的大小減少小數位元而擷取最大的準確度。也就是說,我們可以做出一個比windows內建的計算機更精確的計算機了,想到這麼容易,應該可以堅定你繼續學習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.WinForms

Imports System.ComponentModel

Public Class welcome

Inherits System.WinForms.Form



Public Sub New()

MyBase.New()

welcome = Me

'This call is required by the Win Form Designer.

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 '釋放滑鼠捕獲,等同於API的ReleaseCapture()

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

End Sub



End Class



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。