| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3316 人关注过本帖
标题:求教,C# ,怎么通过button_Click事件跳转到另一个命名空间的form
只看楼主 加入收藏
潇潇骚年
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-11-10
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
求教,C# ,怎么通过button_Click事件跳转到另一个命名空间的form
怎么通过button_Click事件跳转到另一个命名空间的form,求教,怎么实现。
搜索更多相关主题的帖子: 空间 命名 
2012-11-10 21:44
mmxo
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:13
帖 子:189
专家分:1090
注 册:2012-11-7
收藏
得分:5 
不清楚你指的是同一个项目中不同的命名空间还是另一个应用程序的命名空间,这里两种都给出,两个按钮,butJumpNear跳到同一项目的FromTarget窗体;ButJumpFar动态载入你指定的可执行程序集并实例化名字包含“FormMain”的类型,并且调用其中名称为“DoSomething”的方法,当然你指定的命名空间中要有这个方法才可以,在这个例子中就是NamespaceJumpB。
程序代码:
//NamespaceJumpA.FormMain.cs
using System;
using System.Reflection;
using System.Windows.Forms;

namespace NamespaceJumpA
{
    public partial class FormMain : Form
    {
        #region 常量

        private const string Filter = "exe文件|*.exe";

        #endregion

        #region 全局字段

        private FormTarget _formTarget;

        #endregion
      
        #region 构造函数

        public FormMain()
        {
            InitializeComponent();
        }

        #endregion

        #region 控件事件

        private void ButJumFar_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog { Filter = Filter };
            if (ofd.ShowDialog() != DialogResult.OK) return;
            var assembly = Assembly.LoadFile(ofd.FileName);
            var types = assembly.GetTypes();
            foreach (var type in types)
            {
                if(type == null || type.FullName == null)
                    continue;
                if (!type.FullName.Contains("FormMain"))
                    continue;
                var instance = Activator.CreateInstance(type);
                var method = type.GetMethod("DoSomething");
                if (method == null) return;
                method.Invoke(instance, new object[0]);
            }
        }

        private void butJumpNear_Click(object sender, EventArgs e)
        {
            if (_formTarget == null) _formTarget = new FormTarget();
            _formTarget.DoSomehing();
        }

        #endregion
    }
}

//NamespaceJumpA.FormTarget.cs
using System.Windows.Forms;

namespace NamespaceJumpA
{
    public partial class FormTarget : Form
    {
        #region 常量

        private const string Msg = "I am in near target.";

        #endregion

        #region 构造函数

        public FormTarget()
        {
            InitializeComponent();
        }

        #endregion

        #region 公共方法

        public void DoSomehing()
        {
            MessageBox.Show(Msg);
        }

        #endregion
    }
}

//NamespaceJumB.FormMain.cs
using System.Windows.Forms;

namespace NamespaceJumpB
{
    public partial class FormMain : Form
    {
        #region 常量

        private const string Msg = "I am in far target.";

        #endregion

        #region 构造函数

        public FormMain()
        {
            InitializeComponent();
        }

        #endregion

        #region 公共方法

        public void DoSomething()
        {
            MessageBox.Show(Msg);
        }

        #endregion
    }
}



为提高中华编程水平而奋斗
2012-11-11 14:01
w805756054
Rank: 2
等 级:论坛游民
帖 子:10
专家分:21
注 册:2012-10-14
收藏
得分:5 
[code]
[/code]

[ 本帖最后由 w805756054 于 2012-11-11 22:19 编辑 ]
2012-11-11 22:16
smart0721
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:106
专家分:468
注 册:2012-2-10
收藏
得分:5 
先把要调用的那个命名空间的项目生成,然后另一项目调用这一项目生成的DLL,然后就调用这一项目的函数了,就可以实现你那样的跳转。
2012-11-12 00:23
ccn294609235
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2012-11-6
收藏
得分:5 
做成库引用
2012-11-12 07:58
快速回复:求教,C# ,怎么通过button_Click事件跳转到另一个命名空间的form
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013258 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved