【问题标题】:VB form buttons and their textVB 表单按钮及其文本
【发布时间】:2015-02-23 15:31:12
【问题描述】:

寻求帮助。我想要一个最多包含 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

【问题讨论】:

  • 没有代码就无法知道您的代码出了什么问题。此外,如果它是 VB.NET,则它不是 access-vba,反之亦然。见How to Ask
  • 我正在使用 vb studio,我已经连接到 .mdb 并且连接正常
  • 您已经问过这个问题,尽管您在该问题中的数据在文本文件中。您需要显示您的代码....我假设您正在读取数据,但没有遍历每条记录以填充您的按钮...因此您在所有按钮中都获得了第一条记录...
  • 好的,我已经粘贴了代码。谢谢
  • 我的代码对我有帮助吗?

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


【解决方案1】:

公开课 f 02

03 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 MyBase.Load 04 'TODO:这行代码将数据加载到'LoftDataSet.Services'表中。您可以根据需要移动或移除它。 05 Me.ServicesTableAdapter.Fill(Me.LoftDataSet.Services) 06

07 '遍历数据集中的所有行 08 对于 LoftDataSet.Services.Rows 中的每个 dr As DataRow 09

10 Dim btn As New Button '实例化一个按钮 11 btn.Text = dr("service_name").ToString 'UserName 是我的用户表中的一个字段 12 btn.Size = 新尺寸(140, 80) 13 btn.Tag = dr("ID") '这里我们设置标签为主键(ID) 14

15 '因为我们使用的是 flowlayoutpanel,所以我们不需要担心设置 location 属性 16 FlowLayoutPanel1.Controls.Add(btn) '将按钮添加到流布局面板 17 AddHandler btn.Click, AddressOf UserClick '这里我们给按钮一个点击事件的处理程序 18

19 下一个 20 结束子 21

22 公共类产品 23 公共属性 ProductName 作为字符串 24 公共财产 ProductCost 作为小数 25

26 公共覆盖函数 ToString() 为字符串 27 返回 ProductCost & " " & ProductName 28 '在这里你可以为用户构建一个格式化的字符串来包含成本 29 结束功能 30 结束类 31

32

33 '这里我们为我们创建的按钮的点击事件编写方法 34 将所选产品调暗为新产品 35

36

37 Private Sub UserClick(ByVal sender As Object, ByVal e As EventArgs) 38

39 '我们为绑定源设置了一个过滤器,通过它 ID= 40

41 ServicesBindingSource.Filter = "ID = " & 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) 处理 Button2.Click 50 For i As Integer = ListBox1.SelectedIndices.Count - 1 到 0 步 -1 51 ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(i)) 52 下一个 53 结束子 54

【讨论】:

    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 2016-06-29
    • 2011-01-10
    • 1970-01-01
    • 2013-08-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多