每单击一次按钮添加一条完全不同的行

时间:2019-08-29 07:25:40

标签: vb.net

在这里完成vb.net(和一般编程)的noob的操作,我真正想要的是每次单击按钮时,文本框中的数字加1,但是新数字显示在下一行。尝试过Google一百次,但没有任何帮助。

我不想使用循环,因为我不想一次显示所有数字,而只是想在单击特定按钮(在新行上)后显示添加的数字。

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim txtoutput As String = ""
    Dim a As Integer = 1

    txtoutput &= "the value of a =" & a & Environment.NewLine
    a = a + 1

    TextBox1.Text = txtoutput

End Sub

2 个答案:

答案 0 :(得分:2)

您要替换文本,要添加新行,因此需要执行以下操作:

Private a As Int32 = 0

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    a += 1
    Dim newLine = $"the value of a = {a}"
    TextBox1.Text = TextBox1.Text & Environment.NewLine & newLine
End Sub

如果要保留旧值并将其递增,则还必须使用字段而不是局部变量。否则,它将始终重置为初始值。

答案 1 :(得分:1)

请尝试将dim a更改为static a

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim txtoutput As String = ""
    Static a As Integer = 1

    txtoutput &= "the value of a =" & a & Environment.NewLine
    a = a + 1

    TextBox1.Text = txtoutput

End Sub