C#通過反射獲取當前工程中所有窗體并打開的方法
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次
本文實例講述了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#程序設計有所幫助。
您可能感興趣的文章
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#獲取進程或線程相關信息的方法
- 01-10C#通過Semaphore類控制線程隊列的方法
- 01-10C#調用dos窗口獲取相關信息的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#獲取任務欄顯示進程的方法
- 01-10C#利用反射技術實現去掉按鈕選中時的邊框效果
- 01-10C#及WPF獲取本機所有字體和顏色的方法
- 01-10C#獲取動態(tài)生成的CheckBox值
- 01-10C#獲取網頁源代碼的方法


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現txt定位指定行完整實例
- 01-10WinForm實現仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11ajax實現頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法


