Michael
Michael

Reputation: 13636

Why after GroupBox control deleted from Form, the TextBox is not created on the Form?

I'm newer in WinForms VB NET programming.

I need to create text box at a run time.

I found the following VB NET code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim textbox1 As New TextBox
    textbox1.Name = "Textbox1"
    textbox1.Size = New Size(170, 20)
    textbox1.Location = New Point(70, 32)
    textbox1.Visible = True
    GroupBox1.Controls.Add(textbox1)  

End Sub

When this row GroupBox1.Controls.Add(textbox1) and GroupBox control are being deleted from Form, the TextBox isn't created on the Form after the event is fired .

Any idea why it happens?

Thank you in advance.

Upvotes: 0

Views: 305

Answers (1)

Hans Passant
Hans Passant

Reputation: 942197

A child control, like a TextBox, must have a parent to be visible. You give it a parent by setting its Parent property or more commonly by adding it to the Controls collection of the parent. So if you delete the group box then you indeed can't see the text box anymore, it won't have a parent.

Arbitrarily, add it to the form instead:

Me.Controls.Add(textbox1) 

Upvotes: 1

Related Questions