通过反射技术创建窗体实例然后给控件赋值(C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace CSFramework.Tech2011
{
/// <summary>
/// 测试窗体
/// </summary>
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//不直接创建
// frmReflactForm form = new frmReflactForm();
//为了演示,通过反射创建一个窗体
object o = typeof(frmReflactForm).Assembly.CreateInstance("puhlish.frmReflactForm");
frmReflactForm form = o as frmReflactForm;
form.Show();
}
private void button2_Click(object sender, EventArgs e)
{
//在Assembly内通过Application查找frmReflactForm
Form findform = GetReflactForm("frmReflactForm");
if (findform != null) //找到, 哈哈
SetFormValue(findform, "textBox2", textBox1.Text);
}
private void SetFormValue(Form form, string controlName, object value)
{
//重点部分!!!!
FieldInfo[] fs = form.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
//代码值千金啊!
foreach (FieldInfo fi in fs)
{
if (fi.Name.ToUpper() == controlName.ToUpper())
{
Control[] ctls = form.Controls.Find(controlName, true);
if (ctls.Length > 0) //找到, 哈哈
{
ctls[0].Text = Convert.ToString(value); //直接赋值
//如想搞得高级点,通过反射赋值.xxx.setvalue(obj,value)
}
break;
}
}
}
//从Application查找窗体
private Form GetReflactForm(string formName)
{
foreach (Form form in Application.OpenForms)
{
if (form.GetType().Name.ToUpper() == formName.ToUpper())
{
return form;
}
}
return null;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace CSFramework.Tech2011
{
/// <summary>
/// 测试窗体
/// </summary>
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//不直接创建
// frmReflactForm form = new frmReflactForm();
//为了演示,通过反射创建一个窗体
object o = typeof(frmReflactForm).Assembly.CreateInstance("puhlish.frmReflactForm");
frmReflactForm form = o as frmReflactForm;
form.Show();
}
private void button2_Click(object sender, EventArgs e)
{
//在Assembly内通过Application查找frmReflactForm
Form findform = GetReflactForm("frmReflactForm");
if (findform != null) //找到, 哈哈
SetFormValue(findform, "textBox2", textBox1.Text);
}
private void SetFormValue(Form form, string controlName, object value)
{
//重点部分!!!!
FieldInfo[] fs = form.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
//代码值千金啊!
foreach (FieldInfo fi in fs)
{
if (fi.Name.ToUpper() == controlName.ToUpper())
{
Control[] ctls = form.Controls.Find(controlName, true);
if (ctls.Length > 0) //找到, 哈哈
{
ctls[0].Text = Convert.ToString(value); //直接赋值
//如想搞得高级点,通过反射赋值.xxx.setvalue(obj,value)
}
break;
}
}
}
//从Application查找窗体
private Form GetReflactForm(string formName)
{
foreach (Form form in Application.OpenForms)
{
if (form.GetType().Name.ToUpper() == formName.ToUpper())
{
return form;
}
}
return null;
}
}
}
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 |
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 |
在线客服
