国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#通過反射獲取當前工程中所有窗體并打開的方法

2020-01-24 01:31:27
字體:
來源:轉載
供稿:網友

本文實例講述了C#通過反射獲取當前工程中所有窗體并打開的方法。分享給大家供大家參考。具體實現方法如下:

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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 房产| 平谷区| 英德市| 竹山县| 拉萨市| 奇台县| 丰都县| 宿迁市| 陆川县| 东丽区| 抚顺县| 泰来县| 永胜县| 宿州市| 达尔| 互助| 余姚市| 逊克县| 墨竹工卡县| 长沙市| 蓝山县| 阳高县| 青神县| 南雄市| 于田县| 襄汾县| 南漳县| 麟游县| 类乌齐县| 潜江市| 井研县| 民县| 什邡市| 海安县| 涟水县| 温州市| 正宁县| 阿拉尔市| 始兴县| 弋阳县| 富川|