奇怪的控制行为Visual Basic Windows窗体

时间:2015-11-17 19:29:49

标签: vb.net

我已按下按钮动态地在主表单上添加了一个文本框:

Public Widget As UUTWidgetPanel 
...
 Public Sub New(ByVal UnitNo As Integer)
    Me.UnitNo = UnitNo

    Widget = New UUTWidgetPanel(UnitNo)
End Sub
...
Public Class UUTWidgetPanel
     Public ContainerWidget As New FlowLayoutPanel
     Public UnitLabel as New Label
     Public CurrDrawTB As New TextBox

     Public Sub New(ByVal UnitNo As Integer)

       With UnitLabel
        .Text = "Unit " & UnitNo.ToString
        .Height = 25
        .Anchor = AnchorStyles.Top + AnchorStyles.Left
        .TextAlign = ContentAlignment.MiddleCenter
    End With

     With CurrDrawTB
        .Width = 123
        .Height = 25
        .Text = "some text"
        .ReadOnly = False
        .Visible = True
        .ForeColor = Color.Blue
        '.Text = "CurrDrawTB"
    End With

  With ContainerWidget
     .Controls.Add(UnitLabel)
     .Controls.Add(CurrDrawTB)

我添加" ContainerWidget" FlowLayoutPanel到我的主窗体,从一个按钮调用的方法点击如下:

 Private Sub AddUUT()   'adds instance of TestUnit to TestUnits, then it's controls to main form's "FlowLayoutPanel1"
    Dim highestArrIdx As Integer = TestUnits.Count
    TestUnits.Add(New TestUnit(highestArrIdx + 1))
  FlowLayoutPanel1.Controls.Add(TestUnits.Item(highestArrIdx).Widget.ContainerWidget)
End Sub

奇怪的是,当我尝试从CurrDrawTB检索信息时,我收到一个空字符串,而不是值"some text"

  Dim text As String
  text = Me.Widget.CurrDrawTB.Text

但写入文本框可以:

  Me.Widget.CurrDrawTB.Text = "Hi!"    <--I see "Hi!" in the debugger and on the form.

提前致谢:)

1 个答案:

答案 0 :(得分:1)

您的CurrDrawTB Text属性正在被覆盖。这是一个非常普遍的问题。你发布的逻辑与此有关。