单击按钮时,如何继续添加与所写内容相关的按钮

时间:2018-08-23 14:40:54

标签: vb.net visual-studio

Public Class Form3
  Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  End Sub

  Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
  End Sub

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim oForm As Form2 = New Form2
    Dim btn As Button = New Button With {
        .Location = New Point(300, 100),
        .Text = TextBox1.Text,
        .ForeColor = Color.Black
    }
    oForm.Controls.Add(btn)
    oForm.StartPosition = FormStartPosition.CenterScreen
    oForm.Show(Me)
    Me.Hide()
  End Sub
End Class

我想在单击按钮时继续添加新按钮,但不要在同一位置,我希望按钮继续向下添加,如下所示:

[Button1]

[Button2]

[Button3]

1 个答案:

答案 0 :(得分:1)

您可以获得Button1的位置,然后每次单击都会增加Y值。

Public Class Form1
    Dim Button1Coordinate As Point
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim btn As Button = New Button
        Button1Coordinate.Y += 46
        With btn
            .Location = New Point(Button1Coordinate)
            .Text = TextBox1.Text
            .ForeColor = Color.Black
        End With
        Me.Controls.Add(btn)
        Me.StartPosition = FormStartPosition.CenterScreen
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1Coordinate = Button1.Location
    End Sub
End Class

N.B。(按钮的默认高度为23像素)