VB.NET技巧_用程式碼自動生成按鈕技巧_VB特殊語法
今天要和大家介紹兩個程式語法技巧(時常用在簡化程式碼)
先備知識 特別語法介紹
1.
那如果不要加在 FlowLayoutPanel 之中的話
可以直接添加在
視窗上(programmatically)
使用 Me來代表這個視窗
先備知識 特別語法介紹
1.
Array of Buttons
(用在程式自動生成按鈕 or 指定相同功能/事件任務的大量按鈕)
2.
With 某物件
.屬性1
.屬性2
.XXX
.
End With
使用時機
當你有一個物件
比方說
高樓大廈的物件好了
那此時你會需要實體化 然後賦予值
Dim dp As Skyscraper
dp = New Skyscraper()
那這棟公寓你可能要呼叫裏頭的屬性
假設
是針對 101 樓層的每一個樓
那你此時用以前第一種指定欄位寫法
就會不斷要寫很長的 Skyscraper
Skyscraper.B1 = "B1"
Skyscraper.F1 = "1"
Skyscraper.F2 = "2"
Skyscraper.F31 = "3"
.
.
.
Skyscraper.F101 = "101"
那此時可以替換為
With Skyscraper
.B1 = "xxx"
.F1 = "xxx"
.F2 = "xxx"
....
依此類推
End With
程式撰寫
練習1.示範使用程式產生元件
介面布局
程式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim i As Integer For i = 1 To 10 Dim Btn As New Button Btn.Width = 30 Btn.Height = 30 Btn.Text = i Btn.Visible = True FloLayPan.Controls.Add(Btn) Next End Sub End Class |
這裡的主要特別語法
是
控件A.Controls.Add(某特定控件B)
是
控件A.Controls.Add(某特定控件B)
那如果不要加在 FlowLayoutPanel 之中的話
可以直接添加在
視窗上(programmatically)
使用 Me來代表這個視窗
程式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim i As Integer For i = 1 To 10 Dim Btn As New Button Btn.Width = 30 Btn.Height = 30 Btn.Text = i Btn.Visible = True FloLayPan.Controls.Add(Btn) Next Dim Btn2 As Button = New Button Btn2.Height = 65 Btn2.Width = 100 Btn2.Left = Me.Width / 2 - Btn2.Width / 2 Btn2.Top = Me.Height / 2 - Btn2.Height / 2 'Btn2.Left = Me.Width / 2 - Btn2.Width / 2 'Btn2.Top = Me.Height - Btn2.Height * (3 / 2) Me.Controls.Add(Btn2) End Sub End Class |
當然相對位置的調整可以自己設定
那此時眼尖的人
會發現
視窗大小調動
並不會因應跟著做響應式變化
只會靜靜待在原處
此時到右上 添加 Resize事件區塊
更改Btn2為全域變數(提到最上方)
還有添加指定Button相對於視窗上位置之程式
在Resize區域
這樣就會每次都跟著做響應式位置更新
練習2.按鈕、標籤元件(控件)陣列技巧
程式碼
那此時眼尖的人
會發現
視窗大小調動
並不會因應跟著做響應式變化
只會靜靜待在原處
此時到右上 添加 Resize事件區塊
更改Btn2為全域變數(提到最上方)
還有添加指定Button相對於視窗上位置之程式
在Resize區域
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | Public Class Form1 Dim Btn2 As Button = New Button Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim i As Integer For i = 1 To 10 Dim Btn As New Button Btn.Width = 30 Btn.Height = 30 Btn.Text = i Btn.Visible = True FloLayPan.Controls.Add(Btn) Next 'Dim Btn2 As Button = New Button Btn2.Height = 65 Btn2.Width = 100 Btn2.Left = Me.Width / 2 - Btn2.Width / 2 Btn2.Top = Me.Height / 2 - Btn2.Height / 2 'Btn2.Left = Me.Width / 2 - Btn2.Width / 2 'Btn2.Top = Me.Height - Btn2.Height * (3 / 2) Me.Controls.Add(Btn2) End Sub Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize Btn2.Left = Me.Width / 2 - Btn2.Width / 2 Btn2.Top = Me.Height / 2 - Btn2.Height / 2 End Sub End Class |
這樣就會每次都跟著做響應式位置更新
練習2.按鈕、標籤元件(控件)陣列技巧
程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | Public Class Form1 Dim Btn2 As Button = New Button Dim Btn3(4) As Button 'array of 5 buttons (0~4) Dim labels(4) As Label 'display number of clicks for each button Dim count(4) As Integer 'track number of clicks for each button Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load For k = 0 To Btn3.Length - 1 Btn3(k) = New Button Btn3(k).Height = 75 Btn3(k).Width = 100 Btn3(k).Left = 120 * k + 20 Btn3(k).Top = Me.Height / 2 - Btn3(k).Height * (3 / 2) Me.Controls.Add(Btn3(k)) labels(k) = New Label labels(k).Height = 75 labels(k).Width = 100 labels(k).Left = 120 * k + 70 labels(k).Top = Me.Height / 2 + Btn3(k).Height / 2 labels(k).Text = 0 Me.Controls.Add(labels(k)) Next 'Dim i As Integer 'For i = 1 To 10 ' Dim Btn As New Button ' Btn.Width = 30 ' Btn.Height = 30 ' Btn.Text = i ' Btn.Visible = True ' ' FloLayPan.Controls.Add(Btn) 'Next ' ''Dim Btn2 As Button = New Button 'Btn2.Height = 65 'Btn2.Width = 100 'Btn2.Left = Me.Width / 2 - Btn2.Width / 2 'Btn2.Top = Me.Height / 2 - Btn2.Height / 2 ' ''Btn2.Left = Me.Width / 2 - Btn2.Width / 2 ''Btn2.Top = Me.Height - Btn2.Height * (3 / 2) 'Me.Controls.Add(Btn2) End Sub Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize 'Btn2.Left = Me.Width / 2 - Btn2.Width / 2 'Btn2.Top = Me.Height / 2 - Btn2.Height / 2 End Sub End Class |
留言
張貼留言