VB窗體動(dòng)態(tài)創(chuàng)建較少,今天,試著弄一個(gè),專(zhuān)為解答之用。
首先創(chuàng)建一個(gè)空白窗體,調(diào)整大小,修改名稱(chēng),雙擊窗體進(jìn)入代碼編輯界面:
- Public Class VB解答專(zhuān)用窗體
- Dim 數(shù)據(jù)列表 As DataGridView
- Dim 輸入框 As TextBox
- Private Sub VB解答專(zhuān)用窗體_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- 數(shù)據(jù)列表 = New DataGridView
- 數(shù)據(jù)列表.Parent = Me
- 數(shù)據(jù)列表.Columns.Add("序列", "序列")
- 數(shù)據(jù)列表.Columns.Add("內(nèi)容", "內(nèi)容")
- '數(shù)據(jù)列表.Rows.Add(5)
- AddHandler 數(shù)據(jù)列表.RowStateChanged, AddressOf 數(shù)據(jù)列表行狀態(tài)更改
- 輸入框 = New TextBox
- 輸入框.Parent = Me
- AddHandler 輸入框.TextChanged, AddressOf 輸入框輸入更改
- 數(shù)據(jù)列表.Location = New Point(輸入框.Width + 5, 2)
- AddHandler 數(shù)據(jù)列表.CellDoubleClick, AddressOf 單元格雙擊發(fā)生
- End Sub
- Private Sub 數(shù)據(jù)列表行狀態(tài)更改(sender As Object, e As DataGridViewRowStateChangedEventArgs)
- 數(shù)據(jù)列表.Rows(e.Row.Index).Cells(0).Value = e.Row.Index
- e.Row.HeaderCell.Value = String.Format("{0}", e.Row.Index + 1)
- End Sub
- Private Sub 輸入框輸入更改(sender As Object, e As EventArgs)
- 數(shù)據(jù)列表.Rows.Add(Integer.Parse(輸入框.Text))
- End Sub
- Private Sub 單元格雙擊發(fā)生(sender As Object, e As DataGridViewCellEventArgs)
- 數(shù)據(jù)列表.Rows.Insert(e.RowIndex, 1)
- End Sub
- End Class
接著創(chuàng)建一個(gè)控件,并在窗體加載事件中對(duì)控件進(jìn)行一些設(shè)置和添加一個(gè)事件:
從上圖看到有波浪線(xiàn),鼠標(biāo)移動(dòng)到波浪線(xiàn)處,在彈出的糾正錯(cuò)誤框中選擇生成方法存根:
刪除自動(dòng)生成事件中的Throw New NotImplementedException代碼,寫(xiě)自己要處理的代碼,這里以論壇提問(wèn)為例,鼠標(biāo)點(diǎn)擊自動(dòng)填充序號(hào):
添加事件處理代碼:
運(yùn)行程序:
再次創(chuàng)建一個(gè)文本框控件,如上所述,同時(shí)簡(jiǎn)單設(shè)置2個(gè)控件的位置:
再次運(yùn)行程序,在文本框中輸入數(shù)字2,看運(yùn)行效果圖:
可以看到創(chuàng)建了2行,同時(shí)自動(dòng)添加序列各行的序值。
聯(lián)系客服