C#通過反射獲取當前工程中所有窗體并打開的方法
更新時間:2015年08月20日 16:52:17 作者:我心依舊
這篇文章主要介紹了C#通過反射獲取當前工程中所有窗體并打開的方法,涉及C#針對窗體的獲取與顯示等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#通過反射獲取當前工程中所有窗體并打開的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
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#程序設計有所幫助。
您可能感興趣的文章:
- C#動態(tài)生成按鈕及定義按鈕事件的方法
- C#鍵盤輸入回車鍵實現(xiàn)點擊按鈕效果的方法
- C#中Winform窗體Form的關(guān)閉按鈕變灰色的方法
- C# Winform實現(xiàn)捕獲窗體最小化、最大化、關(guān)閉按鈕事件的方法
- C#窗體編程不顯示最小化、最大化、關(guān)閉按鈕的方法
- c# winform取消右上角關(guān)閉按鈕的實現(xiàn)方法
- c#重寫TabControl控件實現(xiàn)關(guān)閉按鈕的方法
- 一個事半功倍的c#方法 動態(tài)注冊按鈕事件
- C#實現(xiàn)利用反射簡化給類字段賦值的方法
- C#利用反射技術(shù)實現(xiàn)去掉按鈕選中時的邊框效果
相關(guān)文章
C#使用LINQ中Enumerable類方法的延遲與立即執(zhí)行的控制
這篇文章主要介紹了C#的LINQ查詢中Enumerable類方法的延遲與立即執(zhí)行,LINQ語言集成查詢可以讓C#和VB以查詢數(shù)據(jù)庫相同的方式操作內(nèi)存數(shù)據(jù),需要的朋友可以參考下2016-03-03C#中實現(xiàn)向數(shù)組中動態(tài)添加元素
這篇文章主要介紹了C#中實現(xiàn)向數(shù)組中動態(tài)添加元素方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06C# Windows API應用之基于GetDesktopWindow獲得桌面所有窗口句柄的方法
這篇文章主要介紹了C# Windows API應用之基于GetDesktopWindow獲得桌面所有窗口句柄的方法,結(jié)合實例形式分析了GetDesktopWindow函數(shù)用于獲取窗口句柄的具體使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-08-08Winform消除button按下出現(xiàn)的虛線簡單實現(xiàn)方法
這篇文章主要介紹了Winform消除button按下出現(xiàn)的虛線簡單實現(xiàn)方法,通過重寫button設置Selectable參數(shù)實現(xiàn)該功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08c#數(shù)據(jù)綁定之向查詢中添加參數(shù)(.Net連接外部數(shù)據(jù)庫)
本實例主要練習了ADO.Net連接到外部數(shù)據(jù)庫的基礎上,向查詢中添加參數(shù)。使用的是ACCESS數(shù)據(jù)庫2014-04-04