处理多种形式 [英] Working with multiple forms

查看:71
本文介绍了处理多种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种形式,一种是主形式,另两种形式继承它,并且具有相同的内容和一些附加功能.
我希望通过主表单打开第二个表单,并通过第二表单打开第三个表单,同时打开第二个表单.
我以为有一些全局对象,但它给运行时异常"sysgeo.exe中发生了类型为"System.StackOverflowException"的未处理的异常".
如果有任何想法,请提出建议.

I have 3 forms One is master form two other form inherit it and have same contents with some additional functions.
I want through master form Second form should open and through second form third form should open at the same time second form should get closed.
I have thought to have some global object but it give runtime exception "An unhandled exception of type ''System.StackOverflowException'' occurred in sysgeo.exe"
If any body have idea please suggest.

推荐答案

您不需要全局对象.在您的主窗体中实例化Form2的实例并打开它.处理Form2上的Close事件并实例化表单3的实例并打开它.很简单,不是吗?
You don''t need a global object. Instantiate an instance of Form2 in your master form and open it. Handle the Close event on Form2 and instantiate an instance of form 3 and open it. Simple, no?


在这种情况下,主表单"应控制子表单的创建.
第二种形式"应引发一个事件,例如ThirdFormRequested.然后,此事件的处理程序中的主表单"可以关闭第二表单"并打开第三表单".

这样的事情:
In this situation the ''master form'' should control the sub form(s) creation.
The ''second form'' should raise an event such as ThirdFormRequested. The ''master form'' in it''s handler for this event can then close the ''second form'' and open the ''third form''.

Something like this:
using System;
using System.Windows.Forms;

namespace MasterSlave
{
    public partial class FormMaster : Form
    {
        FormSlave formSlave;

        public FormMaster()
        {
            InitializeComponent();
            Shown += new EventHandler(FormMaster_Shown);
        }

        void FormMaster_Shown(object sender, EventArgs e)
        {
            CreateSlave();
        }

        private void CreateSlave()
        {
            formSlave = new FormSlave();
            formSlave.OtherSlaveRequest += new EventHandler(formSlave_OtherSlaveRequest);
            formSlave.Show();
        }

        void formSlave_OtherSlaveRequest(object sender, EventArgs e)
        {
            if (formSlave != null)
                formSlave.Close();
            CreateSlave();
        }
    }
}

using System;
using System.Windows.Forms;
namespace MasterSlave
{
    public partial class FormSlave : Form
    {
        public event EventHandler OtherSlaveRequest;
        private Button button;
        public FormSlave()
        {
            InitializeComponent();
            button = new Button();
            button.Text = "Other &Slave";
            Controls.Add(button);
            button.Click += new EventHandler(button_Click);
        }
        void button_Click(object sender, EventArgs e)
        {
            OnOtherSlaveRequest(EventArgs.Empty);
        }
        protected virtual void OnOtherSlaveRequest(EventArgs e)
        {
            EventHandler eh = OtherSlaveRequest;
            if (eh != null)
                eh(this, e);
        }
    }
}


谢谢大家的建议.我找到了又一个解决方案,这正是我一直在寻找的解决方案.
我更喜欢在这里使用父母和孩子的概念.
这样我可以使用相同的表单主表单,也可以使用其他子表单在父表单中打开
如下所示,我将在表格1中编写.
Thanks for the suggestions guys. I found one more solution which is very much what I was looking for.
I prefer to use Parent and child concept here.
So that I can use same form master form and I can use other child forms to get open in the parent form
like as follows I will write in Form one.
Form2 obj = new Form2();
obj.MdiParent = this;
obj.Show();


干杯,
迪帕克

[由DaveyM69添加]您从未提到要制作MDI应用程序!

如果它是MDI应用程序,那么这很好,因为如果没有父级就不能存在子级,那么Form2Form1的耦合就可以了,并且是可取的.

如果不是,那么这是一种较差的方法,因为如果以后不将表单用作子项,则对this.MdiParent的任何调用都可能会失败.[/Added]


Cheers,
Deepak

[Added by DaveyM69] You never mentioned you were making a MDI application!

If it''s an MDI app then this is fine as the child cannot exist without the parent so the coupling of Form2 to Form1 is OK and is desirable.

If it''s not then it''s a poor approach as any call to this.MdiParent may potentially fail if later the form is used not as a child.[/Added]


这篇关于处理多种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆