如何基于按钮单击添加到列表框 [英] How Do I Add To Listbox Based On Button Click
问题描述
Public class f
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
'TODO:这行代码将数据加载到'LoftDataSet.Services'表中。你可以根据需要移动或移除它。
Me.ServicesTableAdapter.Fill(Me.LoftDataSet.Services)
'循环通过数据集中的所有行
For Each dr As DataRow In LoftDataSet.Services.Rows
Dim btn As New Button'Instantiate一个按钮
btn.Text = dr(service_name)。ToString'UserName是我的用户表中的一个字段
btn.Size =新尺寸(60,40)
btn.Tag = dr(ID)'这里我们将标签设置为主键(ID)
'因为我们'使用flowlayoutpanel,我们不需要担心设置位置属性
FlowLayoutPanel1.Controls.Add(btn)'将按钮添加到流布局面板
AddHandler btn.Click,AddressOf UserClick'这里我们给按钮一个点击事件的处理程序
下一页
End Sub
'我们在这里迭代我们创建的按钮的点击事件的方法
私人子用户点击(ByVal发送者为对象,ByVal e为EventArgs)
'我们设置一个过滤器到绑定源传递它ID =< and whatever =is =stored =in =the =tag = property =>
ServicesBindingSource.Filter =ID =& DirectCast(发件人,按钮).Tag.ToString
结束Sub
Public Class f
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'LoftDataSet.Services' table. You can move, or remove it, as needed.
Me.ServicesTableAdapter.Fill(Me.LoftDataSet.Services)
'Loop through all the rows that are in the dataset
For Each dr As DataRow In LoftDataSet.Services.Rows
Dim btn As New Button 'Instantiate a button
btn.Text = dr("service_name").ToString 'UserName is a field in my Users Table
btn.Size = New Size(60, 40)
btn.Tag = dr("ID") 'Here we set the tag to the primary key (ID)
'Since we're using a flowlayoutpanel, we don't need to worry about setting the location property
FlowLayoutPanel1.Controls.Add(btn) 'Add the button to the flow layout panel
AddHandler btn.Click, AddressOf UserClick 'Here we give the button a handler for the click event
Next
End Sub
'Here we write our method for the click event of the button(s) we created
Private Sub UserClick(ByVal sender As Object, ByVal e As EventArgs)
'We set a filter to the binding source passing it ID=<and whatever="" is="" stored="" in="" the="" tag="" property="">
ServicesBindingSource.Filter = "ID = " & DirectCast(sender, Button).Tag.ToString
End Sub
推荐答案
结帐这可能有帮助你...
如何在Visual Basic.NET中向ListBox添加项目 [ ^ ]
在C#中:
Checkout this it may help you..
How to Add Items to a ListBox in Visual Basic.NET[^]
In C#:
private void button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Add(&amp;amp;quot;Sally&amp;amp;quot;);
listBox1.Items.Add(&amp;amp;quot;Craig&amp;amp;quot;);
}
private void button2_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
}
这篇关于如何基于按钮单击添加到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!