程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#經由過程反射獲得以後工程中一切窗體並翻開的辦法

C#經由過程反射獲得以後工程中一切窗體並翻開的辦法

編輯:C#入門知識

C#經由過程反射獲得以後工程中一切窗體並翻開的辦法。本站提示廣大學習愛好者:(C#經由過程反射獲得以後工程中一切窗體並翻開的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved