2014-12-07 93 views
0

我想了解更多關於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 

非常感謝

千電子伏

回答

1

在你點擊鏈接你引用的類名SubForm,這在VB.NET被稱爲一種形式的default automatic instance,但這是不一樣的您在中顯示的實例。在此創建一個名爲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 

請記住,這改變後,你有責任有效地關閉和處置的全局實例。

Private Sub Form1_FormClosed(sender as Object, e as FormClosedEventArgs) _ 
    Handles Form1.FormClosed 
    if objSubForm IsNot Nothing 
     objSubForm.Close 
    End If 
    objSubForm = Nothing 
End Sub 
+1

謝謝,這工作得很好。我編輯了一下你的代碼,以從Form_Load事件中移除DIM,因爲我們現在已經在全局引用了該DIM。 – Kev 2014-12-07 10:08:46

+0

是的,通常的複製/粘貼錯誤。我說,如果我每次能夠得到1歐元,我就會陷入這個困境中,現在我將成爲百萬富翁 – Steve 2014-12-07 10:10:46

0

謝謝史蒂夫,我根據您的修復在半小時內應用了新代碼。

下面是我的代碼的一個基本示例,它給了我可以更新的14個子表單。

我已經不再在子表單中創建屬性,而是直接更新TextBox控件。

該解決方案給了我「更多」更少的代碼,現在我可以添加另一個細節到一個子窗體並顯示所有進程。

再次感謝! Kev

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 
+0

恩,我會離開這個地產。直接訪問Form的內部控件似乎並不是遵循面向對象的範式的最明智的方式。將來,如果你需要改變這個文本框的內容,你的手就會被直接訪問這個控件並且你的表單變得緊密結合在一起。 – Steve 2014-12-08 09:11:25