大家好日子我想问一下,如果这是一个很好的代码,可以使用一个按钮将第一个表单中的文本放入第二个表单中的多个标签?提前致谢并祝贺所有人。
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim lbl As Label = New Label 'Create your Label
'change the location..
lbl.Location = New Point(50, 50) 'Set Label Location
lbl.Text = TextBox1.Text 'Set Label Text
lbl.ForeColor = Color.Black 'Set Label ForeColor
frm2.Controls.Add(lbl) 'Add Label to it
Return
'change the location..
lbl.Location = New Point(10, 20) 'Set Label Location
lbl.Text = TextBox1.Text 'Set Label Text
lbl.ForeColor = Color.Black 'Set Label ForeColor
frm2.Controls.Add(lbl) 'Add Label to it
Return
End Sub
它只显示第一个标签,但它没有显示我要输入的下一个标签......
form2代码
Public Class Form2
Public lbl As New Label
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lbl)
End Sub
结束班
问题解决了......非常感谢Plutonix爵士和Steve的帮助,我非常感谢:)我接受了Plutonix的建议,使用多个文本框而不是一个文本框,我在开始我的项目时使用了Steve的示例代码。再次非常感谢:)
答案 0 :(得分:2)
您需要声明一个全局类级别变量来保持对Form2的引用,然后当您单击该按钮并且全局类级别变量为零(第一次创建)时,您创建Form2实例,添加标签和大多数重要的是添加一个事件处理程序,将在关闭frm2实例时调用它。然后展示表格并将其带到最前沿 如果用户重新点击按钮,则不会创建新表单(此时frm2不是什么),相反,如果用户关闭frm2,将调用事件处理程序并将内部类级别变量重置为空。现在,如果用户再次点击按钮,将重新创建frm2
Private frm2 As Form2
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
if frm2 Is Nothing then
frm2 = new Form2
AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed
Dim lbl As Label = New Label
lbl.Location = New Point(50, 50)
lbl.Text = "FirstLabelText"
lbl.ForeColor = Color.Black
frm2.Controls.Add(lbl)
Dim lbl2 = New Label
lbl2.Location = New Point(10, 20)
lbl2.Text = "TextForSecondLabel"
lbl2.ForeColor = Color.Black
frm2.Controls.Add(lbl2)
End if
frm2.Show()
frm2.BringToFront
End Sub
Sub Form2HasBeenClosed(sender As Object, e As FormClosedEventArgs)
frm2 = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
if frm2 IsNot Nothing Then
frm2.Show(Me) 'Show Second Form
End If
End Sub
答案 1 :(得分:2)
Private LabelCount As Integer = 0
Private Sub Button3_Click(yada yada yada
' I think this is actually fixing a nonexistant problem
' stemming from misunderstanding the issue
if frm2 Is Nothing then
frm2 = new Form2
AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed
Dim lbl As Label = New Label
lbl.Text = TextBox1.Text
lbl.ForeColor = Color.Black
Select Case LabelCount
Case 0
lbl.Location = New Point(50, 50)
Case 1
lbl.Location = New Point(10, 20)
Case Else
' not specified
Exit Sub
End Select
LabelCount +=1 ' dont forget this
frm2.Controls.Add(lbl)
End if
frm2.Show()
frm2.BringToFront
End Sub