ITPub博客

首页 > 应用开发 > IT综合 > 走近VB.Net(四) 关于数据类型与示例 (转)

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

原创 IT综合 作者:amyz 时间:2007-10-02 11:10:52 0 删除 编辑
走近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/10752019/viewspace-974434/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3982
  • 访问量
    7512383