【问题标题】:How do I add buttons with event handlers to a form dynamically?如何动态地将带有事件处理程序的按钮添加到表单中?
【发布时间】:2016-06-12 18:18:37
【问题描述】:

是否可以动态执行此操作?怎么样?

代码:

Public Class Form1

Dim c(40) As Integer
Dim IMG As PictureBox
Dim lbl_n(40) As Label
Dim lbl_pr(40) As Label
Dim lbl_ref(40) As Label
Dim lbl_dim(40) As Label
Dim lbl_col(40) As Label
Dim btn_add(40) As Button
Dim btn_rmv(40) As Button
Dim tb_qt(40) As TextBox


AddHandler btn_add(0).Click, AddressOf btn_add0_Click
AddHandler btn_add(1).Click, AddressOf btn_add1_Click
[...]
AddHandler btn_add(40).Click, AddressOf btn_add40_Click

Public Sub btn_add0_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        c(0) = c(0) + 1
        tb_qt(0).Text = c(0)
    End Sub

    Public Sub btn_add1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        c(1) = c(1) + 1
        tb_qt(1).Text = c(1)
    End Sub

    [...]

    Public Sub btn_add40_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        c(40) = c(40) + 1
        tb_qt(40).Text = c(40)
    End Sub

这些是程序正在运行的图像(它们已被编辑):

Form1

Form2

我要动态,因为我可以使用超过 40 种产品!而且我需要做 40 个添加处理程序,以便删除更多 40 个! 我该怎么做?

【问题讨论】:

  • 你为什么不用 "this" 的话来描述,而不是让我们阅读你的代码墙来弄清楚问题是什么
  • 你不需要看“墙”,只有在必要的时候,人们可能听不懂我在说什么
  • 回答您的问题:是的,您可以动态制作。
  • 但是我该怎么做呢?
  • 从这个stackoverflow.com/questions/8608311/…开始然后这个stackoverflow.com/questions/14705126/…试试吧,如果你有问题在这里发布新问题

标签: vb.net


【解决方案1】:

是的,您可以动态执行此操作。

在本例中,我将buttonstextboxes 放入Panel

有 cmets 的例子,哪一行是什么:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'dynamically adding buttons (so You don't need create every button separately)
        For x = 1 To 40
            Dim btn As New Button  'create new button
            btn.Name = "btn_add" & x.ToString  'set ID for button (example: btn_add1, btn_add2, ... btn_add40)
            btn.Text = "+"  'set button text
            btn.Tag = x.ToString 'set button NO. for finding corrent textbox whos belong to this button (for example: tb_qt1 belong to buttons btn_add1 and btn_rem1)
            btn.Width = 24 'this is for styling
            btn.Top = (x * btn.Height) + 3 'for styling, too. Top poistion of button
            btn.Left = 0 'for styling, too. Left position of button
            AddHandler btn.Click, AddressOf btnAdd_Click 'creating sub called btnAdd_Click (this sub will handle all, in this case 40, buttons)
            Panel1.Controls.Add(btn) 'for this example I put buttons and textboxes into panel (autoscroll set to True)
            btn = New Button 'same thing just for remove button
            btn.Name = "btn_rem" & x.ToString
            btn.Text = "-"
            btn.Tag = x.ToString
            btn.Width = 24
            btn.Top = (x * btn.Height) + 3
            btn.Left = btn.Width + 3
            AddHandler btn.Click, AddressOf btnRem_Click 'creating sub called btnRem_Click (this sub will handle all, in this case 40, buttons)
            Panel1.Controls.Add(btn)
            Dim txt As New TextBox 'same thing for textboxes
            txt.Name = "tb_qt" & x.ToString
            txt.Text = "0"
            txt.Tag = x.ToString
            txt.Top = (x * btn.Height) + 3
            txt.Left = btn.Left + btn.Width + 3
            txt.TextAlign = HorizontalAlignment.Right
            Panel1.Controls.Add(txt)
        Next
    End Sub

    Public Sub btnAdd_Click(sender As Object, e As EventArgs)
        Dim btn As Button = DirectCast(sender, Button)  'detect which button clicked. You can add line: MsgBox(btn.Name) to see what happening
        Dim txt As TextBox = Panel1.Controls.Find("tb_qt" & btn.Tag.ToString, True)(0) 'find textbox which belong to this button. this is why set .Tag into buttons
        txt.Text = CInt(txt.Text) + 1 'just do math and change value, by adding one(1), in textbox (by this way I avoid using Your Dim c(40) As Integer)
    End Sub
    Public Sub btnRem_Click(sender As Object, e As EventArgs)
        Dim btn As Button = DirectCast(sender, Button) 'same thing like for btnAdd_Click, but we doing subtract for one(1) value in textbox
        Dim txt As TextBox = Panel1.Controls.Find("tb_qt" & btn.Tag.ToString, True)(0)
        txt.Text = CInt(txt.Text) - 1
    End Sub

因为我对所有buttonstextboxes 使用panel 就像container,所以您必须为panel 设置AutoScroll=True

我希望你会明白它是如何开始的。

【讨论】:

  • 非常感谢!但是使用tb_qt.Text。但是它只使用最后创建的控件的文本!
  • @Silvestre 我不明白你在说什么......你是说,发布的代码只影响最后创建的文本框还是......?
  • 不,它工作正常。但我想使用文本框值'在ListBox 中显示,但如果我使用tb_qt.Text,它仅适用于最后创建的TextBox
  • @Silvestre 好的...如何在ListBox 中显示?如果某些textbox 值大于0(零),则必须在ListBox 中显示,或者您将在ListBox 中拥有例如40 个项目并且您需要更新这些项目?或者...您必须更具体...(顺便说一句。ListBoxListView?)
  • 这是ListBox... 这样做:If tb_qt.Text > 0 Then Form2.ListBox1.Items.Add(lbl_ref.Text) Form2.ListBox2.Items.Add(lbl_col.Text) Form2.ListBox3.Items.Add(lbl_n.Text) Form2.ListBox4.Items.Add(lbl_pr.Text) Form2.ListBox5.Items.Add(tb_qt.Text) Form2.ListBox6.Items.Add(tb_qt.Text * lbl_pr.Text) End If 它只适用于最后创建的textbox
猜你喜欢
  • 2018-04-23
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多