C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
更新時間:2015年08月20日 16:52:17 作者:我心依舊
這篇文章主要介紹了C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法,涉及C#針對窗體的獲取與顯示等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法。分享給大家供大家參考。具體實現(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#程序設(shè)計有所幫助。
您可能感興趣的文章:
- 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#實現(xiàn) Server-sent Events的步驟
這篇文章主要介紹了C#實現(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#使用foreach循環(huán)遍歷數(shù)組完整實例
這篇文章主要介紹了C#使用foreach循環(huán)遍歷數(shù)組,結(jié)合完整實例形式較為詳細的分析了C#遍歷數(shù)組的相關(guān)技巧,需要的朋友可以參考下2016-06-06

