提前致谢
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
答案 0 :(得分:0)
Public Class f 02
03 Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load 04 &#39; TODO:这行代码将数据加载到&lt; LoftDataSet.Services&#39;表。您可以根据需要移动或删除它。 05 Me.ServicesTableAdapter.Fill(Me.LoftDataSet.Services) 06
07 &#39;循环遍历数据集中的所有行 08 对于LoftDataSet.Services.Rows中的每个dr As DataRow 09
10 Dim btn As New Button&#39;实例化一个按钮 11 btn.Text = dr(&#34; service_name&#34;)。ToString&#39; UserName是我的用户表中的一个字段 12 btn.Size =新尺码(140,80) 13 btn.Tag = dr(&#34; ID&#34;)&#39;这里我们将标签设置为主键(ID) 14
15 &#39;由于我们正在使用flowlayoutpanel,因此我们不必担心设置位置属性 16 FlowLayoutPanel1.Controls.Add(btn)&#39;将按钮添加到流布局面板 17 AddHandler btn.Click,AddressOf UserClick&#39;这里我们给按钮一个click事件的处理程序 18
19 下一个 20 结束子 21
22 公共类产品 23 公共属性ProductName As String 24 公共财产产品成本为十进制 25
26 Public Overrides Function ToString()As String 27 返回ProductCost&amp; &#34; &#34; &安培;产品名称 28 &#39;在这里,您可以为用户构建格式化字符串以包含成本 29 结束功能 三十 结束班 31
32
33 &#39;在这里,我们为我们创建的按钮的click事件编写方法 34 Dim SelectedProduct作为新产品 35
36
37 Private Sub UserClick(ByVal sender As Object,ByVal e As EventArgs) 38
39 &#39;我们为绑定源设置过滤器,传递ID = 40
41 ServicesBindingSource.Filter =&#34; ID =&#34; &安培; DirectCast(sender,Button).Tag.ToString 42
43 SelectedProduct.ProductName = DirectCast(sender,Button).Text 44 SelectedProduct.ProductCost = DirectCast(ServicesBindingSource(0),DataRowView).DataView(0)(2) 45 ListBox1.Items.Add(SelectedProduct) 46
47 结束子 48
49 Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button2.Click 50 对于i As Integer = ListBox1.SelectedIndices.Count - 1到0步-1 51 ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(i))的 52 下一个 53 结束子 54