当前位置:编程问答 > C#/ASP.NET >

走近VB.Net(四) 关于数据类型与示例

答案:走近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

上一个:走近VB.Net(五) VB.Net核心概念
下一个:走近VB.Net(三) 源码详解—运用颜色的初步探讨

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,