服务器之家

服务器之家 > 正文

C#通过反射获取当前工程中所有窗体并打开的方法

时间:2021-10-20 11:50     来源/作者:我心依旧

本文实例讲述了C#通过反射获取当前工程中所有窗体并打开的方法。分享给大家供大家参考。具体实现方法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace TestAppHelperMSDNSample
{
 static class Program
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   Form startup = new Form();
   startup.Text = "Choose a form to run";
   startup.Size = new System.Drawing.Size(300, 300);
   startup.StartPosition = FormStartPosition.CenterScreen;
   startup.Load += new EventHandler(startup_Load);
   ComboBox cboForms = new ComboBox();
   cboForms.Name = "cboForms";
   cboForms.DropDownStyle = ComboBoxStyle.DropDownList;
   cboForms.Size = new System.Drawing.Size(250, 20);
   cboForms.Location = new System.Drawing.Point(25, 75);
   startup.Controls.Add(cboForms);
   Button btnOpenForm = new Button();
   btnOpenForm.Text = "Open Form";
   btnOpenForm.Size = new System.Drawing.Size(100, 30);
   btnOpenForm.Location = new System.Drawing.Point(100, 150);
   btnOpenForm.Click += new EventHandler(btnOpenForm_Click);
   startup.Controls.Add(btnOpenForm);
   Application.Run(startup);
  }
  static void btnOpenForm_Click(object sender, EventArgs e)
  {
   ComboBox cbo = ((sender as Button).Parent as Form).Controls["cboForms"] as ComboBox;
   Properties.Settings.Default.LastFormFullName = cbo.SelectedItem.ToString();
   Properties.Settings.Default.Save();
   Form f = Activator.CreateInstance(Type.GetType(cbo.SelectedItem.ToString())) as Form;
   f.ShowDialog();
  }
  static void startup_Load(object sender, EventArgs e)
  {
   ComboBox cbo = ((sender as Form).Controls["cboForms"] as ComboBox);
   // load all the Forms in executing assembly
   Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetExportedTypes();
   foreach (Type t in types)
   {
    if (t.BaseType == typeof(Form))
    {
     cbo.Items.Add(t.FullName);
    }
   }
   // select the last used
   if (!string.IsNullOrEmpty(Properties.Settings.Default.LastFormFullName))
   {
    if(cbo.Items.Contains(Properties.Settings.Default.LastFormFullName))
    {
     int index = cbo.FindString(Properties.Settings.Default.LastFormFullName);
     if (index >= 0)
      cbo.SelectedIndex = index;
    }
   }
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部