vb.net 将动态文本框添加到动态添加的面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16719564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:43:02  来源:igfitidea点击:

Adding Dynamic TextBoxes to Dynamically added Panels

vb.netwinformscontrols

提问by Pakk

Windows Forms, VS 2012, VB.NET

Windows 窗体、VS 2012、VB.NET

I've tried some code but somehow the dang Textboxes aren't being place inside the panels, i can only assume that im not actually placing them inside the panel, anyone able give me some code to fix my issue ?

我已经尝试了一些代码,但不知何故,dang Textboxes 没有被放置在面板内,我只能假设我实际上没有将它们放置在面板内,任何人都可以给我一些代码来解决我的问题?

Sample code :

示例代码:

 For Each name As String In li
                Dim Answer As String() = AnsStrPerItem(i).Split(",")
                'addField(NameOfPanel,Width,Height,LocationX,LocationY,color White=white else gray)
                'addPanel("Panel" & i.ToString, 1100, 529, 3, (12 + (i * 89)), "White")
                'addField(NameOfButton,TextOfButton,Width,Height,LocationX,LocationY,readonly 0 = true)
                addField("Q" & i.ToString, name.ToString, 533, 64, 14, (69 + (i * 89)), 0)
                addField("A" & i.ToString, Answer(1), 553, 64, 597, (69 + (i * 89)), 1)
                i += 1
            Next

and the code to actually add the panel / txtbox

以及实际添加面板/txtbox的代码

Public Function addPanel(ByVal NameOfPanel As String, ByVal Width As Integer, ByVal Height As Integer, ByVal LocationX As Integer, ByVal LocationY As Integer, ByVal color As String)

    '------------------------ADDING FIELDS INTO FORM TO SIMULATE A DATAREPEATER

    'INSTANTIATE CONTROL
    Dim Panel As System.Windows.Forms.Panel

    'CREATE CONTROL
    Panel = New System.Windows.Forms.Panel()

    'SET TEXT PROPERTIES
    Panel.Name = NameOfPanel
    Panel.Size = New System.Drawing.Size(Width, Height)
    Panel.Location = New System.Drawing.Point(LocationX, LocationY)

    If color = "White" Then Panel.BackColor = Drawing.Color.White Else Panel.BackColor = Drawing.Color.LightGray

    'ADD CONTROL TO FORM1'S COLLECTION
    Form1.Panel5.Controls.Add(Panel)

    Return Nothing

End Function

Public Function addField(ByVal NameOfButton As String, ByVal TextOfButton As String, ByVal Width As Integer, ByVal Height As Integer, ByVal LocationX As Integer, ByVal LocationY As Integer, ByVal ro As Integer)

    '------------------------ADDING FIELDS INTO FORM TO SIMULATE A DATAREPEATER

    'INSTANTIATE CONTROL
    Dim txtb As System.Windows.Forms.TextBox

    'CREATE CONTROL
    txtb = New System.Windows.Forms.TextBox()

    'SET TEXT PROPERTIES
    txtb.Name = NameOfButton
    txtb.Size = New System.Drawing.Size(Width, Height)
    txtb.Location = New System.Drawing.Point(LocationX, LocationY)
    txtb.Text = TextOfButton
    txtb.Multiline = True
    Dim myfont As New Font("Microsoft Sans Serif", 16, FontStyle.Regular)
    txtb.Font = myfont
    If ro = 0 Then txtb.ReadOnly = True

    'ADD CONTROL TO FORM1'S COLLECTION
    Form1.Panel5.Controls.Add(txtb)

    Return Nothing

End Function

回答by tinstaafl

You might need to clarify a bit. In the first function, you're adding a panel to a panel. In the second function your adding a textbox to the parent panel. If the child panel is over top of the textbox it is probably hiding it. If you want the textbox added to the child panel something like this should work:

你可能需要澄清一下。在第一个函数中,您将面板添加到面板。在第二个函数中,您将文本框添加到父面板。如果子面板位于文本框上方,则它可能将其隐藏。如果您希望将文本框添加到子面板中,则应该可以:

Form1.Panel5.Controls(Form1.Panel5.Controls.IndexOfKey(NameOfPanel)).Controls.Add(txtb)

回答by Pakk

adding controls

添加控件

For Each name As String In li
                'addField(NameOfPanel,Width,Height,LocationX,LocationY,color White=white else gray)
                If ((i Mod 2) = 0) Then
                    addPanel("pQna" & i.ToString, 1138, 100, 3, (i * 89), "Lgray")
                Else
                    addPanel("pQna" & i.ToString, 1138, 100, 3, (i * 89), "White")
                End If
                'addField(NameOfButton,TextOfButton,Width,Height,LocationX,LocationY,readonly 0 = true)
                addField("Q" & i.ToString, name.ToString, 533, 64, 14, 14, 0, i)
                addField("A" & i.ToString, "", 553, 64, 597, 14, 1, i)
                i += 1
            Next
    Return Nothing
End Function

adding panels

添加面板

Public Function addPanel(ByVal NameOfPanel As String, ByVal Width As Integer, ByVal Height As Integer, ByVal LocationX As Integer, ByVal LocationY As Integer, ByVal color As String)

    '------------------------ADDING FIELDS INTO FORM TO SIMULATE A DATAREPEATER

    'INSTANTIATE CONTROL
    Dim Panel As System.Windows.Forms.Panel

    'CREATE CONTROL
    Panel = New System.Windows.Forms.Panel()

    'SET TEXT PROPERTIES
    Panel.Name = NameOfPanel
    Panel.Size = New System.Drawing.Size(Width, Height)
    Panel.Location = New System.Drawing.Point(LocationX, LocationY)

    If color = "White" Then Panel.BackColor = Drawing.Color.White Else Panel.BackColor = Drawing.Color.LightGray

    'ADD CONTROL TO FORM1'S COLLECTION
    Form1.pQnA.Controls.Add(Panel)

    Return Nothing

End Function

adding textboxes within the panel

在面板中添加文本框

Public Function addField(ByVal NameOfButton As String, ByVal TextOfButton As String, ByVal Width As Integer, ByVal Height As Integer, ByVal LocationX As Integer, ByVal LocationY As Integer, ByVal ro As Integer, ByVal i As Integer)
    '-----------------------FINDING PANEL

    For Each pnl As Control In Form1.pQnA.Controls
        If TypeOf pnl Is Panel Then
            Dim var As String = pnl.Name.ToString
            If pnl.Name.Equals("pQna" & i.ToString) Then


                '------------------------ADDING FIELDS INTO FORM TO SIMULATE A DATAREPEATER

                'INSTANTIATE CONTROL
                Dim txtb As System.Windows.Forms.TextBox

                'CREATE CONTROL
                txtb = New System.Windows.Forms.TextBox()

                'SET TEXT PROPERTIES
                txtb.Name = NameOfButton
                txtb.Size = New System.Drawing.Size(Width, Height)
                txtb.Location = New System.Drawing.Point(LocationX, LocationY)
                txtb.Text = TextOfButton
                txtb.Multiline = True
                Dim myfont As New Font("Microsoft Sans Serif", 16, FontStyle.Regular)
                txtb.Font = myfont
                If ro = 0 Then txtb.ReadOnly = True

                'ADD CONTROL TO FORM1'S COLLECTION
                'Form1.pQnA.Controls.Add(txtb)
                DirectCast(pnl, Panel).Controls.Add(txtb)

            End If
        End If
    Next

    Return Nothing

End Function

回答by David

Private Sub ReInitializeComponent()

   Me.TextBoxUserId = New System.Windows.Forms.TextBox()

   Me.TextBoxUserId.Location = New System.Drawing.Point(54, 1)

   Me.TextBoxUserId.Name = "TextBoxUserId"

   Me.TextBoxUserId.Size = New System.Drawing.Size(149, 20)

   Me.TextBoxUserId.TabIndex = 13

   Me.TextBoxUserId.Text = ""

end sub