走近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技術!