我对无法解决这个问题感到沮丧。尝试了几种不同类型的循环来显示乘法表。我所得到的只是标签中的一行。我在做什么错了?
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim intNum As Integer
Dim intCount As Integer
Dim intAnswer As Integer
Dim myString As String
Integer.TryParse(txtNumber.Text, intCount)
intNum = 0
Do While intNum < 12
intNum = intNum + 1
intAnswer = intNum * intCount
lblTable.Text = " " & intNum.ToString() & " * " & intCount.ToString() & " = " & intAnswer.ToString()
Loop
答案 0 :(得分:0)
首先,您要将循环结果分配给相同的标签,因此只有一个标签。
如果要创建多个标签,则应在创建过程中创建它们:
Dim top_pos as integer = 30 ' first label's Top
Dim left_pos as integer = 30 ' first label's Left
For intNum = 1 to 12 ' Im assuming you want the table from 1 to 12
intAnswer = intNum * intCount
Dim lbl As New Label
With lbl
.text = Cstr(intNum) & " * " & Cstr(intCount) & " = " & Cstr(intAnswer)
.location = New Point(left_pos,top_pos) 'set its position
... 'and so on
Me.Controls.Add(lbl)
End With
top_pos = top_pos + 30 ' moves the position for the next label.
Next
答案 1 :(得分:0)
从外观看,每次代码循环时,它将标签文本设置为当前结果,同时覆盖之前的结果。
如果您希望标签显示多行数据,可以尝试如下操作:
lblTable.Text &= " " & intNum.ToString() & " * " & intCount.ToString() & " = " & intAnswer.ToString() & Environment.NewLine
最后的“ Environment.NewLine”将在每个结果之间添加换行符。
“ =”之前的“&”用于附加到现有文本的末尾。这类似于这样做:
lblTable.Text = lblTable.Text & "..." & Environment.NewLine
此外,请注意。如果要在标签中显示多行,则可能需要将AutoSize属性设置为false并正确配置所需的标签大小。
答案 2 :(得分:0)
在这一行:
lblTable.Text = " " & intNum.ToString() & " * " & intCount.ToString() & " = " & intAnswer.ToString()
它仅存储最后的结果。
您需要将现有结果预先添加到新结果中。
Label1.Text &= " " & intNum.ToString() & " * " & intCount.ToString() & " = " & intAnswer.ToString() & Environment.NewLine
我在&
之前添加了=
,以便将新结果附加到Label1
现有内容的末尾,并在末尾添加一个换行符整洁