Windows窗体继承问题 [英] Windows Form Inheritance Problem

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

问题描述

大家好.

我有一个问题.

我有一个主要的Windows窗体和另一个窗体.

另一种形式需要访问主形式的控件(按钮,图片框等).
当我从主表单继承另一种表单时,初始化后的表单的外观与主表单完全一样.

我该如何克服这个问题并同时访问主窗体的控件.

我希望其他设计不变!

最好的问候...

Hey everyone.

I have a little question.

I have a main windows form and another form.

Another form needs access the controls(buttons, picturebox, etc) of the main form.
When I inherit the other form from the main form, the appearance of the inhereited one becomes exactly like main form.

How can I overcome this and access the main form''s controls at the same time.

I want another design not to change!

My best regards...

推荐答案

...编辑#1 ...,其中Form2不是从Form1派生的...

Un_NaMed写道:"
... edit #1 ... in which Form2 is not derived from Form1 ...

Un_NaMed wrote: "
报价:

在表格2(派生表格)中,用户将单击一个按钮,
将会在Form 1的图片框中绘制一张地图.

In form 2(derived form), the user will click on a button,
a map will be drawn in Form 1''s picturebox.

好吧,假设Form2是在Form1中创建的:

0.在Form1的代码中:

Okay, let''s assume that Form2 is created in Form1:

0. in Form1''s code:

// we create Form2 in a button click in Form1
private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show();

    // now we can assign an Event Handler in Form1 to Form2's button click
    // until the Form is shown the Button's not created yet ...
    f2.f2Button.Click += new EventHandler(f2Button_Click);
}

public void f2Button_Click(object sender, EventArgs e)
{
    // this is where you would need to draw you Map, or whatever on Form1
    MessageBox.Show("button clicked in Form2 handled in Form1");
}

1.在Form2的代码中:

1. in Form2''s code:

// public property of type Button
public Button f2Button { get; set; }

private void Form2_Load(object sender, EventArgs e)
{
   // set the public property when Form2 is created
   f2Button = this.button1;
   // assign a click handler
   f2Button.Click +=new EventHandler(f2Button_Click);
}

private void f2Button_Click(object sender, EventArgs e)
{
   MessageBox.Show("button click handled in Form2");
}

如果编译并运行此示例,则在一个项目中,在该项目中创建了一个主窗体Form1,并添加了另一个窗体Form2,并在每个Form上添加了一个按钮,然后应注意,运行:两个窗体都出现,并且在Form2中单击按钮将触发两个事件:它将首先在Form2上执行按钮单击处理程序,然后执行在Form1上触发的按钮单击处理程序.

...结束编辑#1 ...

讨论:您在这里混淆了两个非常不同的问题:

1.继承:如果创建从Form1继承的Form2实例:您将获得Form1的精确副本:每个Form都有其自己的一组相同控件,并且它们彼此完全独立.
2.控件/对象之间的表单之间的通信:我相信这是您所要提出的问题,这是QA上最常被问到和回答的问题之一.

这里重要的是定义所需通信的性质:

1.您是否要在Form2上引发引发Form1订阅的事件,并在该事件中传递供Form1使用的信息?

2.是否要实现绑定:",例如在Form2的某个TextBox中出现的任何键入内容(例如,在Form2的某个TextBox中出现)?

3.或者,是否需要对Form2上的某些控件进行访问才能对其进行操作:例如,您可能希望更改Form2上ComboBox下拉列表中的选择,以对Form1中的控件产生影响.

4.在Form2上的每个控件都需要访问Form1上的每个控件的情况下:出现了另一种情况:涉及在创建Form2实例时将对Form1实例的引用注入到Form2中,并通过公共属性公开访问Form1上的控件.这可以简称为注射".

第一步是准确定义Form2和Form1上控件之间动态链接的性质.

您需要像杰西·利伯蒂(Jesse Liberty)一样获得一本关于C#的好书,并研究继承,事件,属性等.

If you compile and run this example, in a project where you have created a Main Form, Form1, and added another form, Form2, and added a button on each Form, then you should observe that when the project is run: both Forms appear, and that clicking on the button in Form2 will trigger two events: it will execute the button-click handler on Form2 first and then execute the button-click handler fired on Form1.

... end edit #1 ...

Discussion: You are confusing here two very different issues:

1. inheritance: if you create an instance of Form2 that inherits from Form1: you get an exact copy of Form1: each Form has its own set of identical controls, and they are completely independent of each other.

2. communication across Forms between Controls/Objects: I believe this is what you are asking about, and this is one of the most frequently asked and answered questions here on QA.

The important thing here is to define the nature of the communication required:

1. do you want something on Form2 to raise an Event that Form1 will subscribe to, and pass information in that Event that Form1 can then use ?

2. do you want to achieve "binding:" where whatever is typed, for example, in a certain TextBox in Form2 appears in a TextBox in Form1 ?

3. Or, does some control on Form2 need access to some control on Form1 in order to manipulate it: for example, you might wish a change in selection in a ComboBox drop-down on Form2 to have some effect on a control in Form1.

4. In the case that every control on Form2 requires access to every control on Form1: a different scenario arises: involving injecting a reference to the instance of Form1 into Form2 when it is created, and making controls on Form1 publicly accessible through public properties. This may be loosely called "injection."

The first step is to define exactly the nature of the dynamic links between the controls on Form2 and Form1.

You need to get a good book on C#, like one from Jesse Liberty, and study up on inheritance, events, properties, etc.


从Properties Panel进行控制修饰符的公开.然后将主窗体实例传递给子窗体.此外,您还可以为该控件编写包装器以对其进行修改.


Make that controls modifier public from Properties Panel .Then just pass main form instance to child form.Also wou can write wrapper for that controlsto modify them.


 public partial class mainForm : Form
{
    public mainForm(){
      new childForm(this).setMyPictureLeft(0);
   }

}  
  public partial class childForm : Form
{

     mainForm form;
    public childForm(mainForm main){
   
       form=main;

    }
   public setMyPictureLeft(int left){

      form.pictureBox1.left=0;

    }


}



这是最糟糕的事情.相反,请在mainForm中编写setMyPictureLeft,而无需将modifer设置为public.



This is worst you can do.Instead write setMyPictureLeft in mainForm and no need to set modifer public.


另一种形式需要访问主控件的控件(按钮,图片框等)表格.
当我从主表单继承另一种表单时,被初始化的表单的外观与主表单完全一样.

您的问题变得模棱两可,如上面的粗体和下划线所示,因此请先更正您的问题是您真正想要的.
Another form needs access the controls(buttons, picturebox, etc) of the main form.
When I inherit the other form from the main form, the appearance of the inhereited one becomes exactly like main form.

Your question getting ambiguous as shown above bold and underline so first correct your question what exactly you want.


这篇关于Windows窗体继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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