如何从MDI应用程序中的另一个子窗体访问子窗体上的控件 [英] How to access controls on a child form from another child form in an mdi application

查看:52
本文介绍了如何从MDI应用程序中的另一个子窗体访问子窗体上的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有三个孩子的mdi表单,比如说form1 form2和form3,可以通过父表单的menustrip访问孩子.我希望每当您通过单击它的toolstripmenuItem并在其中单击一个按钮打开form1时,我想要另一个窗体中的另一个按钮说form2应该启用显示窗体2的功能,除非您在看到更改之前单击form2menustripitem,也就是说,我希望更改直接在窗体上发生,而不是在form2的新实例上发生.请在这个代码上我需要帮助,将非常有帮助.在此先感谢

I have an mdi form with three child say form1 form2 an form3 the childs are accessd through a menustrip from the parent form.i want that whenever u open form1 by the click of its toolstripmenuItem and click a button in it, i want that another button in another form say form2 should b enabled witout showing the form2 unless u click on the form2menustripitem before u will see the change, that is ,i want the change to occure directly on the form and not a new instance of the form2.please i need help on this codes will b very helpful Thank in advance

推荐答案

您好,


我猜您想根据form1中的选择来更改form 2的设置.
如果是这样,您可以保存对form1的选择,并在加载form2时对form2进行设置.

希望对您有所帮助.
Hi,


I guess you want to change the settings of form 2 based on the selection in form1.
If so, you can save the selection of the form1 and make the settings for form2 on load of form2.

Hope this helps.


您好,Ese Ochuko,

您可以通过2种方法解决此问题,

一种方法是使用面板而不是表单..以便所有控件(所有面板的控件)都可以在单个表单中使用..我们仍然可以用作差异表单.
我认为不需要此代码.

第二种方法是,要在form1上对form2进行设置,请使用相同的实例来打开该表单.(通常是通过为其创建对象来打开表单).

即,


Hi Ese Ochuko,

U can resolve this problem in 2 ways,

one approach is to use panels instead of forms..so that all the controls(controls of all panels) can be with in the single form..still we can use as diff forms .
i think code not required for this..

2nd approach is ,to have the effect of settings done on form2 from form1..you should use the same instance for opening that form.(usually form is opened by creating object for it).

i.e ,


form1()
{

 Form2 frm2 =new Form2();

 //controlling Form2 button from From1 menu strip
  void menuStripItem1_click(object sender,EventArgs e)
  {

   if(e.clicked==true)
    frm2.button1.enabled = true;

  }

 //suppose form 2 is opened by clicking menustrip item 2(u call in the same way where you want)
 void menuStripItem2_click(object sender,EventArgs e)
  {
   frm2.show();
  }

}


我创建了一个示例应用程序,该应用程序可能符合您的要求..u可能需要做一些细微的更改

创建4个差异表单

带组合框的登录表单
带有菜单条的父窗体,其他3种窗体作为菜单条项目
带有两个按钮和两个标签的form2和3(将所有4个控件都更改为public)....,其中一个按钮是根据登录表单中的用户类型(1或2)启用的.

i have created a sample application which may suit your requirement..u may have to do minor changes

create 4 diff forms

login form with combobox
parent form with menustrip other 3 forms as menu strip items
form2 and 3 with two buttons and two labels each(change all the 4 controls to public)....in which one button is enabled based on user type (1 or 2)from login form.

 public partial class ParentForm : Form
    {
        public LoginForm lgf;
        public Form2 frm2 = new Form2();
        public Form3 frm3 = new Form3();

        public ParentForm()
        {
            InitializeComponent();
        }

        private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm2.Show();
        }

        private void form3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm3.Show();
        }

        private void loginFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lgf= new LoginForm(this);
            lgf.Show();
        }

    }


//Login Form
public partial class LoginForm : Form
    {
        ParentForm prf;
        public LoginForm(ParentForm prf)
        {
            this.prf = prf;
            InitializeComponent();
        }
       
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "user1")
            {
                prf.frm2.button1.Enabled = false;
            }
            else
                prf.frm3.button4.Enabled = false;
        }
    }


//Form2 with 2 buttons and 2 labels
   public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    }

//Form3 with 2 buttons and 2 labels
 public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
    }


这篇关于如何从MDI应用程序中的另一个子窗体访问子窗体上的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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