VB.NET技巧_用程式碼自動生成按鈕技巧_VB特殊語法

今天要和大家介紹兩個程式語法技巧(時常用在簡化程式碼)

先備知識  特別語法介紹

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)

執行結果就會因應使用loop跑了10次
生成10個寬高皆為30且按鈕上文字都自動指定
不用自己還要去右側旁邊屬性欄位自己一個一個對應
填寫輸入更改
可以節省時間又方便快速喔



那如果不要加在 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區域

 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








留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題