首页 > 编程 > .NET > 正文

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

2024-07-10 13:05:35
字体:
来源:转载
供稿:网友
走近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

  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表