Error opening form inside panel

Asked

Viewed 92 times

4

I would like to open a form inside a panel, but when it opens, it is always the same size and in the same place regardless of the size of the form or the location.

inserir a descrição da imagem aqui

Dim rv As New formRV

        rv.link = lvPasta.SelectedItems(0).ImageKey
        rv.FormBorderStyle = FormBorderStyle.None
        rv.Dock = DockStyle.Fill
        rv.TopLevel = False


        Dim p As New Panel
        With p
            Size = New Size(Me.Width, Me.Height)
            Location = New Point(Me.Location.X, Me.Location.Y)
        End With


        Me.Controls.Add(p)
        p.BringToFront()

        p.Controls.Add(rv)

        p.Show()
        rv.Show()

Edit:

Besides the panel i tried to just create the new form and display it, hiding the old form, but I had the screen problem "blink" while the form change (main form some - piece of windows - new form appears) and the buttons so that when the new form was closed by "X" (where a program normally closes) the first form was closed, but when you click another button to "go back" it closed form2 but not the first form.

Dim rv As New formRodaVideo

        rv.linkvideo = lvPasta.SelectedItems(0).ImageKey
        rv.FormBorderStyle = FormBorderStyle.Sizable
        rv.TopLevel = True
        rv.Width = Me.Width
        rv.Height = Me.Height
        rv.Location = Me.Location
        rv.WindowState = FormWindowState.Normal
        rv.StartPosition = FormStartPosition.WindowsDefaultBounds

        rv.Show()
        Me.Hide()
  • Hi @Lucas. What’s your idea with this? What do you want to do?

  • I have the main form and when I press a button on it, I need to call a second form, so that, the second form, is opened in the same place with the same size as the first form and keeping the first form hidden until the second form is closed. And right after the second form is closed I would need.

1 answer

0


Apparently, from the image you put up, it seems that the Form are in a MdiContainer, where the code below can help you achieve what you want:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim form1 = Me.MdiChildren().Where(Function(r) r.Name = "Form1").FirstOrDefault()

    If Not form1 Is Nothing Then
        form1.Hide()

        Dim form2 As Form1 = New Form1

        AddHandler form2.FormClosed, AddressOf form2_FormClosed

        With form2
            .StartPosition = FormStartPosition.Manual
            .Text = "Form2"
            .MdiParent = form1.MdiParent
            .Size = form1.Size
            .Location = form1.Location
            .Show()
        End With
    End If
End Sub

Private Sub form2_FormClosed(sender As Object, e As FormClosedEventArgs)
    Dim form1 = Me.MdiChildren().Where(Function(r) r.Name = "Form1").FirstOrDefault()
    Dim form2 As Form1 = sender

    If Not form1 Is Nothing Then
        With form1
            .StartPosition = FormStartPosition.Manual
            .BringToFront()
            .Location = form2.Location
            .Size = form2.Size
            .Show()
        End With
    End If
End Sub

All you have to do is adapt the names of Form for what you need.

EDIT

If the Form are not MdiChild then you can do it this way (something similar to the previous one):

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Hide()

        Dim form2 As Form1 = New Form1

        AddHandler form2.FormClosed, AddressOf form2_FormClosed

        With form2
            .StartPosition = FormStartPosition.Manual
            .Text = "Form2"
            .Size = Me.Size
            .Location = Me.Location
            .Show()
        End With
    End Sub

    Private Sub form2_FormClosed(sender As Object, e As FormClosedEventArgs)
        Dim form2 As Form1 = sender

        With Me
            .StartPosition = FormStartPosition.Manual
            .BringToFront()
            .Location = form2.Location
            .Size = form2.Size
            .Show()
        End With
    End Sub
End Class

Presupposes the existence of a button Button1 in the Form.

That way no longer has the "blink" between the Form, due to property StartPosition = FormStartPosition.Manual, that avoids that by doing the Show() the location is reset (I think the location will also be related).

  • It’s not mdi, it’s a panel that had been created in the code that I put in. I was trying to open inside it. I tried something else too instead of opening on panel. I edited the question.

Browser other questions tagged

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