在c#.net Windows中将字符串转换为Type形式 [英] convert string to type Form in c#.net windows

查看:54
本文介绍了在c#.net Windows中将字符串转换为Type形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个c#.net Windows应用程序.为此,我制作了一个包含linklabel的用户控件.的属性Formname.像

Am developing a c#.net windows application.In that i made a usercontrol which contains a linklabel.I set  a property Formname for that.like

私人

 

字符串

string

表单名;

formname;

 

public 字符串

public string

表单名

Formname

{

 

获取

{

 

返回

return

表单名;

formname;

}

 

set

{

formname =

formname =

value

;

;

}

}

点击事件,例如

and i wrote the folloing code on the click event like



 

{

 

frm.Name =表单名称;

frm.Name = formname;

frm.Show();

frm.Show();

}



我 意图是显示用户在属性Formname中指定的表单.

my intension is to show the form which user given in the property Formname.



但是 它不起作用,它只是显示一个空白表格

but its not working it just shows a blank form

请 任何人都可以帮助我..am是C#的新功能

please any one help me..am new in c#

推荐答案

灰烬,

欢迎使用C#.首先,您的应用程序似乎是WinForms,因此您在错误的论坛上发布了内容,因为在此论坛中我们讨论的是WPF,它是不同的UI堆栈.

Welcome to C#. First off your application seems to be WinForms so you posted in the wrong forum as in this forum we discuss WPF which is a different UI stack.

但是要回答您的问题:在单击事件中使用的方法不起作用,因为您正在创建常规表单并为其分配名称,该名称可以是任何形式,并且仍然只是您拥有的常规表单打开并给予不同的 姓名.您的申请书中有什么表格?

But to answer your question: the approach you are using in the click event is not working as you are creating a general form and assigning it a name, the name can be anything and it will still just be a general form you have opened and given a different name. What forms do you have in your application?

假设您有一个名为MainForm的表单,那么您需要使用以下代码打开它:

Lets say you have a form called MainForm, then you need to open it using this code:

MainForm frm = new MainForm();
frm.Show();

如果要基于字符串名称打开表单,请使用以下代码(其中 WindowsFormsApplication2 是您的应用程序的名称)

If you want to open a form based on a string name use this code (where WindowsFormsApplication2 is the name of your application)

Form frm = (Form)(Assembly.GetExecutingAssembly().CreateInstance("WindowsFormsApplication2.MainForm"));
frm.Show();

在上面的示例中,我们使用反射,因此必须使用以下命令添加它:

In the above example we use reflection so you must add this using:

using System.Reflection;

我希望这会有所帮助.


这篇关于在c#.net Windows中将字符串转换为Type形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆