How to open a form with just the name of the type

Asked

Viewed 101 times

2

I have a DataGridView in my form, remembering that it gets the name of the Mysql database forms.

I’ve tried a few ways to click the form name on DataGridView and open the same, but did not succeed.

Examples from the table

Column name:

id,  
nome - (nome fantasia do formulário) [Empresas],  
slug - (nome real do formulário na aplicação) [frm_empresas]

The idea and click on the record on DataGridView and open the form, but collecting the form name on DataGridView column slug.

  • I have a meeting in 5 minutes, but I think I have a solution for you. If you don’t have any answers when I get back, I’ll write a.

  • Thank you very much, I’ll be waiting.

1 answer

2


Assuming you know how to get the value of DataGridView, I’ll stick my answer to how to open a form using only the name of the type.

Reflection solves your problem very well and is nothing complicated. Suppose I want to open the form called Form1, which is in the namespace AbrindoForms.

Note: you need to add the namespace System.Reflection

Dim slugform As string
slugform = "AbrindoForms.Form1"

Dim form = DirectCast(Assembly.GetExecutingAssembly().CreateInstance(slugform), Form)
form.ShowDialog()

Realize it’s made a DirectCast of the return of CreateInstance (which is aobject) for the guy Form. Since all application forms inherit from this class, this is quiet and allows you to call the native methods of the type Form, like the ShowDialog what I use in the example.

  • Instead of '"Abrindoforms.Form1"' I cannot put the variable I used to collect 'Slug' from datagridview.

  • Is that a question or a statement? Surely you can

  • Dim slugform As string slugform = dgvFavoritos.CurrentRow.Cells("slug_frm").Value Dim form = DirectCast(Assembly.GetExecutingAssembly().CreateInstance(slugform), Form) form. Show()`

  • Nice. And what’s the problem?

  • I click on Datagridview and collect the name of the form, then step slug_frm instead of "Abrindoforms.Form1" and it doesn’t work. I think I’m doing it wrong :(

  • 1

    You need to check the value of the variable slugForm. I setei manually and it worked well. Try to put the value of the variable in a msgbox to be able to see it, or check it via debug. The error is in another part of the code, what I gave you is functional. Note that you need to concatenate the namespace before the form name.

  • Sorry, really it was my fault, thank you @jbueno

  • The mood. Glad I could help =D

Show 3 more comments

Browser other questions tagged

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