Load Window Form inside VB.net Container

Asked

Viewed 292 times

0

Opa

I have a function for opening photons inside a general container.

Function carrega_form(ByRef ctl As Control, ByRef frm As Form)
    If ctl IsNot Nothing AndAlso frm IsNot Nothing Then
        frm.TopLevel = False
        frm.FormBorderStyle = FormBorderStyle.None
        frm.Dock = DockStyle.Fill
        frm.Visible = True
        ctl.Controls.Add(frm)
    End If
End Function

To call it use:

carrega_form(container, New frm_content_cadastro_botoes)

Password that the containeris in a main form, what I have to do is just load other Forms inside this container, the problem is that it occurs only once, ie when executing the form function is opened, but when executing again nothing happens.

I tried before calling use function: Me.hide, but, still does not call the form as it should.

Using the call to different containers works.

The container object is a Panel

1 answer

0

Comments explain all the changes I’ve made. Some of them might solve your problem.

'                                              | você não precisa de uma
'                                              | referência para uma forma
'                                              | que está chamando sem
'                                              | referência
' Isso não retorna nada, então                 |
' não precisa ser uma Function                \ /
Public Sub carrega_form(ByRef ctl As Control, ByVal frm As Form)
    ' Verificação nula sempre no início do código
    ' Vamos economizar processador, galera
    If ctl Is Nothing Then Exit Sub
    ' Remove os controles antigos
    For i As Integer = 0 To ctrl.Controls.Count - 1
        ctrl.Controls.RemoveAt(i)
    Next
    ' Converte implícitamente a form para control
    Dim c As Control = frm
    ' Não precisamos mais da form
    frm.Dispose()
    ' Isso já define local e tamanho do controle.
    c.Dock = DockStyle.Fill
    ' Será visível?
    c.Visible = True
    ' Adiciona o controle.
    ctl.Controls.Add(c)
End Sub 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.