暱稱: 隨風飄蕩的羽毛 頭銜: [御用]潛水艇
高中生 
- 帖子
- 852
- 主題
- 79
- 精華
- 0
- 積分
- 918
- 點名
- 0
- 作業系統
- Windows 7 , XP
- 軟體版本
- Office 2007, Office 2003,Office 2010,YoZo Office
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 宇宙
- 註冊時間
- 2011-4-8
- 最後登錄
- 2024-2-21
|
[發問] [發問]關於 動態新增元件的程式碼重複區域問題
問題敘述:
新增動態元件,有時候必須設定該元件之相關設定(如:位置、大小、顏色..等)
會造成一些原始代碼的重複(如下列原始碼的 .Font = New Font(fontFamily, 14, FontStyle.Regular, GraphicsUnit.Pixel) ' 字體、大小、類型 )
如果元件為 陣列元件 可以省下不少成式碼 藉此縮短程式運行的時間
相同道理
如果該相同區域有方式代替的話,對於程式運行應該有一定的加分效果。
如 sclab和 sccob 的 .Height 和.width (此兩種為數值)
想請教
1.
.Font = New Font(fontFamily, 14, FontStyle.Regular, GraphicsUnit.Pixel) ' 字體、大小、類型
這種不屬於數字和文字的程式碼
是否有方式可以取代?
2.
新增動態元件 是否可將不相同類型的元件( Combobox Textbox Button )
用陣列的方式宣告
原始碼如下:
Dim fontFamily As New FontFamily("微軟正黑體")
With sclab
.Width = (Me.Width) / 6
.Height = 24
.Location = New Point(15, 15)
.BorderStyle = BorderStyle.FixedSingle
.Font = New Font(fontFamily, 14, FontStyle.Regular, GraphicsUnit.Pixel) ' 字體、大小、類型
.AutoSize = False
.TextAlign = ContentAlignment.MiddleCenter
End With
With sccob
.Width = (Me.Width) / 3
.Height = sclab.Height
.Location = New Point(sclab.Width + sclab.Location.X + 10, 15)
.Font = New Font(fontFamily, 14, FontStyle.Regular, GraphicsUnit.Pixel) ' 字體、大小、類型
.AutoSize = False
End With
With sccob1
.Width = (Me.Width) / 5
.Height = sclab.Height
.Location = New Point(sccob.Width + sccob.Location.X + 10, 15)
.Font = New Font(fontFamily, 14, FontStyle.Regular, GraphicsUnit.Pixel) ' 字體、大小、類型
.AutoSize = False
End With
With sctext1
.Width = (Me.Width) / 4.25
.Height = sclab.Height
.Location = New Point(sccob1.Width + sccob1.Location.X + 10, 15)
.BorderStyle = BorderStyle.FixedSingle
.Font = New Font(fontFamily, 16, FontStyle.Regular, GraphicsUnit.Pixel) ' 字體、大小、類型
.AutoSize = False
'.TextAlign = ContentAlignment.MiddleCenter
End With
With lab3x '顯示結果
.Height = sclab.Height + 20
.Width = (sctext1.Width + sctext1.Location.X) - sclab.Location.X
.Location = New Point(sclab.Location.X, sclab.Height + sclab.Location.Y + 10)
.BorderStyle = BorderStyle.FixedSingle
.Font = New Font(fontFamily, 14, FontStyle.Regular, GraphicsUnit.Pixel) ' 字體、大小、類型
.AutoSize = False
.TextAlign = ContentAlignment.MiddleCenter
End With |
|