时间:2021-05-20
本文实例讲述了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#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#递归遍历窗体所有textbox控件并设置textbox事件的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下://///////
本文实例讲述了C#通过属性名字符串获取、设置对象属性值操作.分享给大家供大家参考,具体如下:#通过反射获取对象属性值并设置属性值0、定义一个类publiccla
本文实例讲述了C#根据反射和特性实现ORM映射的方法。分享给大家供大家参考。具体如下:(一)关于反射什么是反射?反射就是在运行时,动态获取对象信息的方法。比如:
获取Url的方法有两种,通过后台获得或通过前面js获得,如下:1、通过C#获取当前页面的URLstringurl=Request.Url.AbsoluteUri
网上找了很久关于C#检测移动硬盘并获取盘符的代码但没能找到,所以只能自己解决了C#获取所有硬盘vararr=DriveInfo.GetDrives();得出的所