VB表单按钮和他们的文本

时间:2015-02-23 15:31:12

标签: vb.net forms button connection access-vba

寻求帮助。我希望有一个最多约20个按钮的表单。我希望他们的显示名称来自我销售的产品的访问数据库表。我可以将表单连接到我的数据库并获取表中的第一条记录以显示在第一个按钮上,但是当我对第二个按钮执行相同操作时,它只会再次显示第一条记录。有没有办法将按钮名称指向表中的第二条记录?我想将产品名称硬编码到代码中,因为它们将来可能会发生变化: - )

提前致谢

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

1 个答案:

答案 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