João Ferreira
João Ferreira

Reputation: 44

Create controls dinamically in a form got by a string value

In my current project I intend to do something like a file explorer. When I click with the right mouse button over the form it opens another form that simulates a MsgBox with 2 buttons, one to create a folder and another to create a text file. The program stores the name of the form that was open when clicked with the mouse and should create the desired option. The problem is that it does not create and does not give errors.

prevForm is a string which contains the previous form name.

Sorry for bad English

Private Sub pasta_Click(sender As Object, e As EventArgs) Handles pasta.Click
    Dim nomePasta = InputBox("Nome da Pasta: ", "Nova Pasta", "Nova pasta")

    Dim novoForm As New Form With {
        .Name = nomePasta,
        .Text = nomePasta,
        .Size = New Point(816, 489),
        .BackColor = Color.FromName("ActiveCaption")
    }

    Dim novaPicBox As PictureBox

    novaPicBox = New PictureBox With {
        .Size = New Point(60, 60),
        .Location = New Point(720, 21)
    }

    System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaPicBox)

    Dim novaLbl As Label

    novaLbl = New Label With {
        .Text = nomePasta,
        .Location = New Point(720, 85)
    }

    System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaLbl)

    Me.Close()
End Sub

Private Sub fichtexto_Click(sender As Object, e As EventArgs) Handles fichtexto.Click
    Dim nomeFichTexto = InputBox("Nome do Ficheiro de Texto: ", "Novo Ficheiro de Texto", "Novo Ficheiro de Texto")

    Dim novaPicBox As PictureBox

    novaPicBox = New PictureBox With {
        .Size = New Point(60, 60),
        .Location = New Point(720, 111),
        .Image = Image.FromFile(".\txt.png"),
        .SizeMode = PictureBoxSizeMode.StretchImage
    }

    System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaPicBox)

    Dim novaLbl As Label

    novaLbl = New Label With {
        .Text = nomeFichTexto,
        .Location = New Point(720, 174)
    }

    System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaLbl)

    Me.Close()
End Sub

Upvotes: 1

Views: 86

Answers (1)

djv
djv

Reputation: 15774

You keep calling

System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm)

which keeps creating instances. You don't want to create an instance since there is surely already one. You need to find it, or, better yet, just set it when the message is shown.

Add a property to your class which can be set before it's shown, which is the actual previous Form.

Public Class MsgForm

    Public Property PreviousForm As Form

    Private Sub pasta_Click(sender As Object, e As EventArgs) Handles pasta.Click
        Dim nomePasta = InputBox("Nome da Pasta: ", "Nova Pasta", "Nova pasta")

        Dim novoForm As New Form With {
            .Name = nomePasta,
            .Text = nomePasta,
            .Size = New Point(816, 489),
            .BackColor = Color.FromName("ActiveCaption")
        }

        Dim novaPicBox = New PictureBox With {
            .Size = New Point(60, 60),
            .Location = New Point(720, 21)
        }
        PreviousForm.Controls.Add(novaPicBox)

        Dim novaLbl = New Label With {
            .Text = nomePasta,
            .Location = New Point(720, 85)
        }
        PreviousForm.Controls.Add(novaLbl)

        Me.Close()
    End Sub

    Private Sub fichtexto_Click(sender As Object, e As EventArgs) Handles fichtexto.Click
        Dim nomeFichTexto = InputBox("Nome do Ficheiro de Texto: ", "Novo Ficheiro de Texto", "Novo Ficheiro de Texto")

        Dim novaPicBox = New PictureBox With {
            .Size = New Point(60, 60),
            .Location = New Point(720, 111),
            .Image = Image.FromFile(".\txt.png"),
            .SizeMode = PictureBoxSizeMode.StretchImage
        }
        PreviousForm.Controls.Add(novaPicBox)

        Dim novaLbl = New Label With {
            .Text = nomeFichTexto,
            .Location = New Point(720, 174)
        }
        PreviousForm.Controls.Add(novaLbl)

        Me.Close()
    End Sub

End Class

And when you call the message box form, set the property

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim m = New MsgForm With {.PreviousForm = Me}
        m.Show()
    End Sub

End Class

Upvotes: 1

Related Questions