简体   繁体   中英

open specific windows form using database value in textbox in c#


I have 3 existing windows forms
Now i want to type the existing windows form name in the textbox and it will display on the screen.
The textbox value come from database column.
How will i do that please help.

In this case, I have made the assumption that the forms are in the same namespace as the one that is trying to call them. It is important to validate any input from the user because there could be typos in there.

Try the following code...

var thisType = this.GetType().ToString();
var nameSpace = thisType.Substring(0, thisType.LastIndexOf('.') + 1);
try
{
    var form = Activator.CreateInstance(this.GetType().Assembly.FullName, nameSpace + textBox1.Text).Unwrap() as Form;
    form?.Show();
}
catch
{
    // Chances are there is no such form.
}

I would also suggest another strategy, since you suggest a database may have these values, I would rather load the names into a combo box, while also having stored the full name, including the namespace and then create and show the form from that instead of user input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM