我试图更多地了解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 ......
所以,我试图想出一个代码,我创建一个看起来像一个控制器行的SubForm,然后在我的主窗体上创建这个子窗体的14个实例。
我的测试代码似乎有效,但子窗体上的文本框没有更新屏幕上的内容!即使我回调.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
非常感谢
千电子伏
答案 0 :(得分:1)
在按钮单击中,您引用的是类名SubForm
,这在VB.NET中被称为表单的default automatic instance,但这与您在{中显示的实例不同{1}}。在这里,您可以创建一个名为Form_Load
的不同实例,这是您显示的实例。
要解决此问题,您需要将objSubForm
保留为全局实例,并在单击
objSubForm
请记住,在此更改之后,您有责任有效地关闭和处置全局实例。
Public Class MainForm
Dim objSubForm As SubForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
objSubForm.SetText = Me.TextBox1.Text ' Try to change contents of a TextBox on the SubForm
Me.TextBox2.Text = objSubForm.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
objSubForm = New SubForm()
objSubForm.TopLevel = False
Me.Panel1.Controls.Add(objSubForm)
objSubForm.Show()
End Sub
End Class
答案 1 :(得分:0)
谢谢Steve,我根据您的修复程序在半小时内应用了新代码。
这是我的代码的基本示例,它为我提供了14个可以更新的子表单。
我已经不再在子窗体中创建属性,而是直接更新TextBox控件。
解决方案给了我" MUCH"更少的代码,现在我可以为一个子表单添加另一个细节,并显示所有进程。
再次感谢! 千电子伏
Public Class MainForm
Dim objSubForm(14) As SubForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Update the 14 forms
For n = 0 To 13
objSubForm(n).TextBox1.Text = Me.TextBox1.Text & " " & n
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' create 14 subforms
For n = 0 To 13
objSubForm(n) = New SubForm()
objSubForm(n).TopLevel = False
Me.Panel1.Controls.Add(objSubForm(n))
objSubForm(n).Location = New Point(0, 20 * n)
objSubForm(n).Show()
Next
End Sub
Private Sub MainForm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
' CLear up
If objSubForm IsNot Nothing Then
For n = 0 To 13
objSubForm(n).Close()
Next
End If
For n = 0 To 13
objSubForm(n) = Nothing
Next
End Sub
End Class