您的位置:首页 > 其它

利用反射动态创建控件,且修改其属性

2009-11-10 12:36 543 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
//using System.Data;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
//DataTable dt = new DataTable();
Assembly asm;
private void Form1_Activated(object sender, EventArgs e)
{
fillDgv(toolStripTextBox1.Text);
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
splitContainer3.Panel2.Controls.Clear();
System.Type t = asm.GetType((string)dataGridView1.CurrentRow.Cells[0].Value);
object obj = Activator.CreateInstance(t);
propertyGrid1.SelectedObject = obj;
Control c = obj as Control;
splitContainer3.Panel2.Controls.Add(c);
}
catch
{
}
}

private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
fillDgv(toolStripTextBox1.Text);
}

private void fillDgv(string assemblyPath)
{
dataGridView1.Rows.Clear();
asm = Assembly.LoadFrom(assemblyPath);
System.Type[] ts = asm.GetTypes();
foreach (System.Type t in ts)
{
dataGridView1.Rows.Add(t.FullName);
}
}

}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: