C# 如何在自定义控件的属性中列出所有形式的解决方案资源管理器?
我已经创建了一个登录验证自定义控件。它有两个名为C# 如何在自定义控件的属性中列出所有形式的解决方案资源管理器?,c#,winforms,C#,Winforms,我已经创建了一个登录验证自定义控件。它有两个名为用户名和密码的属性,当包含在项目中时,它们会显示在属性窗口中 我想创建一个属性,它将以类似组合框的方式向属性窗口显示所有表单列表,以便项目程序员可以指定登录成功时打开的表单。自定义控件有两个文本框和一个按钮。我该怎么做 namespace WindowsFormsApplication18 { public partial class UserControl1 : UserControl { private stri
用户名
和密码
的属性,当包含在项目中时,它们会显示在属性窗口中
我想创建一个属性,它将以类似组合框的方式向属性窗口显示所有表单列表,以便项目程序员可以指定登录成功时打开的表单。自定义控件有两个文本框和一个按钮。我该怎么做
namespace WindowsFormsApplication18
{
public partial class UserControl1 : UserControl
{
private string uname=null;
private string pword=null;
public string username
{
get
{
return uname;
}
set
{
uname = value;
}
}
public string password
{
get
{
return pword;
}
set
{
pword=value;
}
}
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == username && textBox2.Text == password)
{
MessageBox.Show("login successful");
}
else
MessageBox.Show("wrong password");
}
}
}
以便项目程序员可以指定登录成功时打开的表单
这实际上不是usercontrol的任务。也许程序员想在成功登录后做些别的事情
创建一个LoggedIn
事件,在成功登录时触发该事件,并从使用控件的代码订阅该事件。然后,在该事件处理程序中,程序员可以按照自己的意愿进行操作
以便项目程序员可以指定登录成功时打开的表单
这实际上不是usercontrol的任务。也许程序员想在成功登录后做些别的事情
创建一个
LoggedIn
事件,在成功登录时触发该事件,并从使用控件的代码订阅该事件。然后,在该事件处理程序中,程序员可以按照自己的意愿进行操作。要获取当前正在执行的模块中的所有表单,可以执行以下操作:
//Must add using System.Reflection; first
public class LoginForm : Form {
public LoginForm(){
InitializeComponent();
Load += (s,e) => {
forms = GetAllForms();
comboBox1.DataSource = forms;
comboBox1.DisplayMember = "Text";//Show the caption
};
}
List<Form> forms;
public List<Form> GetAllForms(){
List<Form> forms = new List<Form>();
foreach (Type t in Assembly.GetExecutingAssembly().GetModules()[0].GetTypes())
{
if (t == GetType()) continue;//Don't add LoginForm
if (t.IsSubclassOf(typeof(Form)))
{
forms.Add((Form)t.GetConstructor(Type.EmptyTypes).Invoke(null));
}
}
return forms;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == username && textBox2.Text == password)
{
MessageBox.Show("login successful");
//Show the selected form
forms[comboBox1.SelectedIndex].Show();
}
else
MessageBox.Show("wrong password");
}
}
//必须使用System.Reflection添加;第一
公共类LoginForm:Form{
公共登录表单(){
初始化组件();
荷载+=(s,e)=>{
forms=GetAllForms();
comboBox1.DataSource=表单;
comboBox1.DisplayMember=“Text”//显示标题
};
}
列表表格;
公共列表GetAllForms(){
列表形式=新列表();
foreach(在Assembly.GetExecutionGassembly().GetModules()[0].GetTypes()中键入t)
{
如果(t==GetType())继续;//不添加LoginForm
if(t.IsSubclassOf(typeof(Form)))
{
Add((Form)t.GetConstructor(Type.EmptyTypes.Invoke(null));
}
}
申报表;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
if(textBox1.Text==username&&textBox2.Text==password)
{
MessageBox.Show(“登录成功”);
//显示所选表单
表单[comboBox1.SelectedIndex].Show();
}
其他的
MessageBox.Show(“错误密码”);
}
}
要获取当前正在执行的模块中的所有表单
,可以执行以下操作:
//Must add using System.Reflection; first
public class LoginForm : Form {
public LoginForm(){
InitializeComponent();
Load += (s,e) => {
forms = GetAllForms();
comboBox1.DataSource = forms;
comboBox1.DisplayMember = "Text";//Show the caption
};
}
List<Form> forms;
public List<Form> GetAllForms(){
List<Form> forms = new List<Form>();
foreach (Type t in Assembly.GetExecutingAssembly().GetModules()[0].GetTypes())
{
if (t == GetType()) continue;//Don't add LoginForm
if (t.IsSubclassOf(typeof(Form)))
{
forms.Add((Form)t.GetConstructor(Type.EmptyTypes).Invoke(null));
}
}
return forms;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == username && textBox2.Text == password)
{
MessageBox.Show("login successful");
//Show the selected form
forms[comboBox1.SelectedIndex].Show();
}
else
MessageBox.Show("wrong password");
}
}
//必须使用System.Reflection添加;第一
公共类LoginForm:Form{
公共登录表单(){
初始化组件();
荷载+=(s,e)=>{
forms=GetAllForms();
comboBox1.DataSource=表单;
comboBox1.DisplayMember=“Text”//显示标题
};
}
列表表格;
公共列表GetAllForms(){
列表形式=新列表();
foreach(在Assembly.GetExecutionGassembly().GetModules()[0].GetTypes()中键入t)
{
如果(t==GetType())继续;//不添加LoginForm
if(t.IsSubclassOf(typeof(Form)))
{
Add((Form)t.GetConstructor(Type.EmptyTypes.Invoke(null));
}
}
申报表;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
if(textBox1.Text==username&&textBox2.Text==password)
{
MessageBox.Show(“登录成功”);
//显示所选表单
表单[comboBox1.SelectedIndex].Show();
}
其他的
MessageBox.Show(“错误密码”);
}
}