C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
更新時(shí)間:2015年08月20日 16:52:17 作者:我心依舊
這篇文章主要介紹了C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法,涉及C#針對(duì)窗體的獲取與顯示等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法。分享給大家供大家參考。具體實(shí)現(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;
}
}
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- C#動(dòng)態(tài)生成按鈕及定義按鈕事件的方法
- C#鍵盤輸入回車鍵實(shí)現(xiàn)點(diǎn)擊按鈕效果的方法
- C#中Winform窗體Form的關(guān)閉按鈕變灰色的方法
- C# Winform實(shí)現(xiàn)捕獲窗體最小化、最大化、關(guān)閉按鈕事件的方法
- C#窗體編程不顯示最小化、最大化、關(guān)閉按鈕的方法
- c# winform取消右上角關(guān)閉按鈕的實(shí)現(xiàn)方法
- c#重寫TabControl控件實(shí)現(xiàn)關(guān)閉按鈕的方法
- 一個(gè)事半功倍的c#方法 動(dòng)態(tài)注冊(cè)按鈕事件
- C#實(shí)現(xiàn)利用反射簡(jiǎn)化給類字段賦值的方法
- C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果
相關(guān)文章
C#實(shí)現(xiàn) Server-sent Events的步驟
這篇文章主要介紹了C#實(shí)現(xiàn) Server-sent Events的步驟,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01
c++函數(shù)轉(zhuǎn)c#函數(shù)示例程序分享
這篇文章主要介紹了c++函數(shù)轉(zhuǎn)c#函數(shù)示例程序,大家參考使用吧2013-12-12
C#利用WebClient實(shí)現(xiàn)兩種方式下載文件
本篇文章主要介紹了C#利用WebClient 兩種方式下載文件,詳細(xì)的介紹了兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
C#使用foreach循環(huán)遍歷數(shù)組完整實(shí)例
這篇文章主要介紹了C#使用foreach循環(huán)遍歷數(shù)組,結(jié)合完整實(shí)例形式較為詳細(xì)的分析了C#遍歷數(shù)組的相關(guān)技巧,需要的朋友可以參考下2016-06-06
Unity3D移動(dòng)端實(shí)現(xiàn)搖一搖功能
這篇文章主要為大家詳細(xì)介紹了基于Unity3D移動(dòng)端實(shí)現(xiàn)搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10

