我想了解更多关于VB.NET和多个窗体,所以我可以让我的代码更好。窗体内的窗体上的文本框不更新
我有一个SQL数据库表,它包含所有14个进程的实时数据,监视程序更新显示所有进程进度的窗体。多年前在MS Access中,我只是简单地使用滚动子窗体来显示表格的内容。然而,我第一次尝试在VB.NET中是有“很多”文本框,基本上14行文本框,我的代码有14个非常相似的部分更新所有的文本框。必须有一个更好的办法:(
例如:
txtProcessID1.Text TxtStatus1.Text ProgressBar1 ......
txtProcessID2.Text TxtStatus2.Text ProgressBar2 ......
txtProcessID3.Text TxtStatus3.Text ProgressBar3 ......
所以,我想拿出我在那里创建子窗体,看起来像一个控制器行代码,然后创建的14个实例在我的mainform上的这个子表格。
我的测试代码似乎工作,但子窗体上的文本框没有更新屏幕上的内容!即使当我回拨.text的内容时,这也是我所期望的。
为什么此示例代码不起作用,而我的解决方案是完成此操作的最佳方式?
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SubForm.SetText = Me.TextBox1.Text ' Try to change contents of a TextBox on the SubForm
Me.TextBox2.Text = SubForm.SetText ' Data comes back as expected, but the subform textbox remains unchanged.
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim objSubForm As New SubForm()
objSubForm.TopLevel = False
Me.Panel1.Controls.Add(objSubForm)
objSubForm.Show()
End Sub
End Class
Public Class SubForm
Public Property SetText() As String
Get
SetText = TextBox1.Text
End Get
Set(ByVal Value As String)
Me.TextBox1.Text = Value ' Control is not updated of the SubForm.
Debug.Print("Value = " & Value) ' The Value is Passed Correctly.
Debug.Print("Text = " & TextBox1.Text) ' And the property of the control has been updated.
End Set
End Property
End Class
非常感谢
千电子伏
谢谢,这工作得很好。我编辑了一下你的代码,以从Form_Load事件中移除DIM,因为我们现在已经在全局引用了该DIM。 – Kev 2014-12-07 10:08:46
是的,通常的复制/粘贴错误。我说,如果我每次能够得到1欧元,我就会陷入这个困境中,现在我将成为百万富翁 – Steve 2014-12-07 10:10:46