vb.NET 生成按钮
添加一个FlowLayoutPanel1(在工具箱--容器里面) 一个Button1
代码如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim url As String = InputBox("输入网站地址?", "输入", "http://")
If url = "" Then Return
Dim link As New LinkLabel
link.Text = url
link.AutoSize = True
FlowLayoutPanel1.Controls.Add(link)
AddHandler link.LinkClicked, AddressOf Link_LinkClicked
End Sub
Private Sub Link_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
Dim url As String = CType(sender, LinkLabel).Text
Process.Start(url)
End Sub
效果如图:
''' <summary>
''' 添加多个按钮到一个容器中.
''' </summary>
''' <param name="StartPos">所有按钮开始点.</param>
''' <param name="ButtonSize">按钮的大小.</param>
''' <param name="ButtonSpace">按钮的间距</param>
''' <param name="ButtonCount">按钮的总数</param>
''' <param name="AddingContainer">被添加到的容器</param>
''' <remarks></remarks>
Private Sub AddButtons(ByVal StartPos As Point, ByVal ButtonSize As Size, _
ByVal ButtonSpace As Size, ByVal ButtonCount As Integer, _
ByVal AddingContainer As Control)
Dim mBtnPos As Point = StartPos
For ItemTemp As Integer = 0 To ButtonCount - 1
Dim NewButton As Button = New Button() With
mBtnPos.Y += ButtonSpace.Height + ButtonSize.Height
If mBtnPos.Y > AddingContainer.Height Then
mBtnPos.Y = StartPos.Y
mBtnPos.X += ButtonSpace.Width + ButtonSize.Width
End If
NewButton.Text = String.Format(":", CStr(ItemTemp + 1))
AddHandler NewButton.Click, AddressOf btnzh_Click
AddingContainer.Controls.Add(NewButton)
Next
End Sub
Private Sub btnzh_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnzh.Click
Dim ItemCount As Integer = 0
Try
ItemCount = CInt(txtChoice.Text)
Catch ex As Exception
MessageBox.Show("您输入的数字无法解析为数字.")
Return
End Try
AddButtons(New Point(2, 2), New Size(60, 40), New Size(20, 30), ItemCount, Me)
End Sub
Private Sub btnzh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
'建立 坐标位置的变数, 方便一会儿安排 自动生产的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
'设定这新的button 的阔度
MyButton.Width = 80
'设定这新的button 的高度
MyButton.Height = 20
'我们暂定每行有4个BUTTON, 当每行有多个4个BUTTON 时重设行距, 令到下一行的和这一行的距离有10 个PIXEL
' 和重设x 坐标` = 0, 令到位置由头开始
If ButtonIndex Mod 4 = 0 Then
'设定 TOP 的 坐标位置令他可以和上一排的有10个PIXEL 的距离
y += MyButton.Height + 10
x = 0
End If
'设定文字 和 位置
' Set the text and set its top and left based on its dimensions and count
MyButton.Text = "My Button" & ButtonIndex
'设定 TOP 的 坐标位置
MyButton.Top = y
'下面的CODE 可以令到这个BUTTON 和另一个BUTTON 有5个PIXEL 的距离
MyButton.Left = 26 + (x * (MyButton.Width + 5))
x += 1
'设定 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 一个自己button 的字的message
MsgBox(btn.Text)
楼上的代码太乱了吧!
用个双重循环就可以轻松搞定了
首先最好是在界面上加一个Panel板块控件,在不需要那么多按钮的时候有利于清除。其名字为Panel1
以下是生成控件的过程和控件单击事件
‘如需生成 8 X 8 的阵列 只需调用drawbtn(8)
Sub drawbtn(ByVal num As Integer)
Me.Width = 440
Me.Height = 500
‘这里调整一下窗口的大小让可以看到所有按钮
Panel1.Width = num * 50
Panel1.Height = num * 50
Panel1.Controls.Clear() ‘清除原有按钮控件,防止一直在堆叠控件都没释放,浪费系统资源
Dim i, j As Integer
For i = 1 To num
For j = 1 To num
Dim b As New Button
b.Width = 50
b.Height = 50
b.Left = (j - 1) * 50
b.Top = (i - 1) * 50
b.Text = (i - 1) * num + j
b.Tag = "list"
Panel1.Controls.Add(b) ‘添加控件到Panel1上
AddHandler b.Click, AddressOf Button_Click ‘添加按钮控件单击事件
Next
Next
End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)‘响应按钮单击事件!
MsgBox("单击了第" & CType(sender, Button).Text & "个按钮!")
End Sub
这个问题很有意思,给你个最简单的。
这是一个过程,调用的时候给出行和列就可以了,程序需要一个Panel对象
Private Sub AddButtons(ByVal Row As Integer, ByVal Col As Integer)
Dim Buttons As Button
Dim PWidth As Integer, PHeight As Integer
PWidth = Panel1.Width / Col
PHeight = Panel1.Height / Row
For i As Integer = 0 To Col
For i1 As Integer = 0 To Row
Buttons = New Button
Buttons.Width = PWidth
Buttons.Height = PHeight
Buttons.Top = i * PHeight
Buttons.Left = i1 * PWidth
Panel1.Controls.Add(Buttons)
Next
Next
End Sub
当涂享半夏: 窗口边框(FormBorderStyle)设为None,窗口(WindowState)设为Maximized,如果想让窗口背景透明,将窗口背景颜色和TransparencyKey设为同一种颜色,按钮用Label(标签)代替,背景设为Transparent,边框设为None,文字位置一...
江阴市13345579036: VB如何实现一按按钮就生成一个TEXTBOX?
当涂享半夏: '先在窗体添加两个按钮:Command1、Command2'再添加一个文本框:Text1,然后在属性窗口将 Text1 的 Index 属性设为 0Private Sub Command1_Click()'单击一次添加一个文本框 Dim I As Long I = Text1.Count Load Text1(I) Text1(I).Visible ...
江阴市13345579036: vb.net 里面如何动态生成多组单选按钮组? - ?
当涂享半夏: 这样循环<% Dim numi As Integer For numi = 1 To TextBox1.Text %><asp:RadioButton ID="RadioButton<%=numi%>" runat="server" GroupName="leibie<%=numi%>" /><br /> <% next %>
江阴市13345579036: 我想在我编的vb.net程序里面添加一个按钮,单击此按钮便可以打开本地的一个word文档,应该怎样写代码呢? - ?
当涂享半夏: 文档位置没有限制,不过推荐放置在程序目录下.Process.start("文档路径") 这样就可以打开
江阴市13345579036: VB.NET 如何动态生成一个控件并把它放入指定的容器中 - ?
当涂享半夏: 比如定义一个button,将它放入form里边吧 Public Class Form1 Dim btn As Button Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load btn = New Button With btn .Parent = Me .Text = ...
江阴市13345579036: vb.net图片按钮如何实现?用什么控件? - ?
当涂享半夏: Button控件的Image属性,BackGroundImage好像不能播放动态图片. 在代码Button1.MouseEnter里写一个 Button1.Image = System.Drawing.Image.FromFile("图片路径") 这个就是让你的鼠标到Button上时load一张图. 如果图是动态的比如Gif之类的就能实现动态效果.你有事可以直接找我啊,.NET的教程网上实在太少了!
江阴市13345579036: vb.net用代码向另一个窗体添加命令按钮 - ?
当涂享半夏: 先得到目标窗体的 handle (句柄) 或整个对象, 然后实例化一个button 并加入到窗体对象中. 如: 在 form2 点击 add 按钮后, form1 会新添加一个按钮,单击显示hello , 下面是两个窗口类. public class form1 inherits system.windows.forms....
江阴市13345579036: vb.net 怎么在右上角增加按钮 - ?
当涂享半夏: 制作一个按钮: Dim my_Button As New Button my_Button.SetBounds(Me.Width - 75, 0, 75, 23) Me.Controls.Add(my_Button) 在软件运行前是看不到这个按钮,运行后可见,运行后想更改按钮位置,简单的方法启动一个事件,如textbox输入数值后通过Button的点击事件修改my_Button的顶点位置,还有的方法就是拖动,拖动应该是高级编程部分而且几十行代码,没有人愿意写
江阴市13345579036: 请问用VB.NET编写的计算器加号按钮的代码应该怎么写我用VB. ?
当涂享半夏: 定义一个全局的变量存放你点击操作符时框内的数值再变量加框中的值不过你点加时要判断一下(A B C )有没有别一个加数
江阴市13345579036: vb.net 如何自定义事件 - ?
当涂享半夏: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For i = 1 To 10 For j = 1 To 10 Dim b As New Button b.Name = "btn" & (i * 10 + j).ToString b.Text = "按钮" & (i * 10 + j)....