Public Sub GenerateDynamiceButton()
成都創(chuàng)新互聯(lián)公司從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元港北做網(wǎng)站,已為上家服務(wù),為港北各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
'建立 坐標(biāo)位置的變數(shù), 方便一會(huì)兒安排 自動(dòng)生產(chǎn)的button 的位置
Dim y As Integer = 15
Dim x As Integer = 0
' 用for loop 來建立 這些button 做 18只button
For ButtonIndex As Integer = 0 To 17
' 建造新的 button
Dim MyButton As New Button
'設(shè)定這新的button 的闊度
MyButton.Width = 80
'設(shè)定這新的button 的高度
MyButton.Height = 20
'我們暫定每行有4個(gè)BUTTON, 當(dāng)每行有多個(gè)4個(gè)BUTTON 時(shí)重設(shè)行距, 令到下一行的和這一行的距離有10 個(gè)PIXEL
' 和重設(shè)x 坐標(biāo)` = 0, 令到位置由頭開始
If ButtonIndex Mod 4 = 0 Then
'設(shè)定 TOP 的 坐標(biāo)位置令他可以和上一排的有10個(gè)PIXEL 的距離
y += MyButton.Height + 10
x = 0
End If
'設(shè)定文字 和 位置
' Set the text and set its top and left based on its dimensions and count
MyButton.Text = "My Button" ButtonIndex
'設(shè)定 TOP 的 坐標(biāo)位置
MyButton.Top = y
'下面的CODE 可以令到這個(gè)BUTTON 和另一個(gè)BUTTON 有5個(gè)PIXEL 的距離
MyButton.Left = 26 + (x * (MyButton.Width + 5))
x += 1
'設(shè)定 BUTTON CLICK EVENT
AddHandler MyButton.Click, AddressOf MyButton_Click
'把button 加到 form 中
Me.Controls.Add(MyButton)
Next
End Sub
'處理 button click event
Private Sub MyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = sender
'pop up 一個(gè)自己button 的字的message
MsgBox(btn.Text)
先得到目標(biāo)窗體的 handle (句柄) 或整個(gè)對(duì)象, 然后實(shí)例化一個(gè)button 并加入到窗體對(duì)象中. 如: 在 form2 點(diǎn)擊 add 按鈕后, form1 會(huì)新添加一個(gè)按鈕,單擊顯示hello , 下面是兩個(gè)窗口類. public class form1 inherits system.windows.forms.form '這是一個(gè)什么都沒有的空窗體 public sub new() me.size=new size(200,200) end subend class public class form2 inherits system.windows.forms.form private button1 as button '添加按鈕 private frm as form public sub new() me.size=new size(200,200) button1= new button() '實(shí)例化 button1.text="add" '名字就叫 add button1.location=new point(50,50) addhandle button1.click, addressof add_click me.controls.add(button1) end sub '用于記錄form1對(duì)象的屬性 public property form1() as form get return frm end get set (byval value as form) frm = value end set end property '添加按鈕 private sub add_click(byval o as object, byval e as eventargs) '當(dāng)form1屬性被指定,向form1 添加按鈕 if frm isnot nothing then dim btn as button = new button() btn.text ="new button" btn.location=new point(50,50) addhandle btn.click, addressof button_click frm.controls.add (btn) else msgbox ("未指定form1") end if end sub '新按鈕的單擊事件 private sub button_click(byval o as object, byval e as eventargs) msgbox("hello!") end subend class 兩個(gè)窗體類完成了,然后在模塊寫如下代碼,程序設(shè)置為從模塊啟動(dòng):public module module1 public sub main() dim frm1= new form1() dim frm2 = new form2() frm2.form1=frm1 frm2.show() frm1.show() application.run(frm2) end subend module
'vb.net2008
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim j_MyButton As New ArrayList
For i = 0 To 5
Dim k1 As New Button
k1.SetBounds(20, 22 * i, 100, 20) '設(shè)置位置和大小(x y w h)
k1.Visible = True
AddHandler k1.Click, AddressOf my_button '委托點(diǎn)擊事件
Me.Controls.Add(k1) '在主窗口內(nèi)加入控件
j_MyButton.Add(k1)
j_MyButton(i).name = "發(fā)現(xiàn)" i "個(gè)MM"
j_MyButton(i).text = "My butten " i
Next
End Sub
Private Sub my_button(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.AppendText(sender.name vbCrLf)
End Sub
End Class
制作一個(gè)按鈕:
Dim my_Button As New Button
my_Button.SetBounds(Me.Width - 75, 0, 75, 23)
Me.Controls.Add(my_Button)
在軟件運(yùn)行前是看不到這個(gè)按鈕,運(yùn)行后可見,運(yùn)行后想更改按鈕位置,簡(jiǎn)單的方法啟動(dòng)一個(gè)事件,如textbox輸入數(shù)值后通過Button的點(diǎn)擊事件修改my_Button的頂點(diǎn)位置,還有的方法就是拖動(dòng),拖動(dòng)應(yīng)該是高級(jí)編程部分而且?guī)资写a,沒有人愿意寫
是有錯(cuò)誤嘛,因?yàn)楸砻鹒amily后應(yīng)該有一個(gè)空格才能接(num,name,pwd,qx,bz)