以子格式获取主窗体对象c# [英] getting main form objects in child form c#

查看:132
本文介绍了以子格式获取主窗体对象c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

i有一个主窗体有一个picturbox {name:picturBox1}当我打开调整大小形式{子窗体}无法访问主窗体中存在的picturBox1我怎么能活动主儿童形式的形式对象

帮助我

tnx

hello there,
i have a main form there is a picturbox {name: picturBox1} when i open resize form {child form} CAN NOT access picturBox1 that exists in main form how can i active main form objects in child form
help me
tnx

推荐答案

我强烈反对放置一个将主表单本身引用为辅助表单,并且我相信在任何表单上进行任何控制都具有使用公共修饰符定义的访问权限是一个严重的错误。这两种编程策略/选择,即imho,意味着主表单和辅助表单之间存在紧密耦合。



实现松散耦合的目标对象之间以及关注点分离和模块化的价值主张是现代OO编程不可或缺的一部分。



在实际的基础上,我发现在现实世界的情况下,它有问题:对象的任何实例最小信息量(或对内部状态的访问权)是什么? '需要知道'关于另一个对象的实例?



在这种情况下,辅助表格需要知道(有权访问)PictureBox以某种方式在主表单上:我们不知道,在这种情况下,如果辅助表单只需要访问到主数据(图像位)表单PictureBox,或者如果辅助表单不仅需要访问,而且还需要更改(转换)内容在主窗体上的PictureBox。



0.在两种情况下:假设主窗体创建辅助窗体实例,并保留对它的引用:
I disagree strongly with the idea of putting a reference to the Main Form itself into a secondary Form, and I believe that making any Control, on any Form, have its access permission defined with the 'Public modifier is a serious mistake. Both of these programming strategies/choices, imho, mean there is "tight coupling" between the Main Form and the secondary Form.

The goal of achieving "loose coupling" between objects, and the value-propositions of "separation of concerns," and "modularity," are integral to modern OO programming.

On a practical basis, I find it valuable in real-world situations to ask the question: "what is the minimum amount of information (or access to the internal state of) that any one instance of an object 'needs to know' about another object's instance ?"

In this case the secondary Form needs to know (have access to) a PictureBox on a Main Form in some way: we don't know, in this case, if the secondary Form just needs access to the data (the image bits) in the Main Form PictureBox, or if the secondary Form not only needs access, but, also, needs to change (transform) the contents of the PictureBox on the Main Form.

0. in both cases: assume the Main Form creates the secondary Form instance, and keeps a reference to it:
private SecondaryForm theSecondaryForm = new SecondaryForm();

1 。辅助表单只需要访问主表单上PictureBox的图像内容:策略:(对象之间的单向通信)



a。在位图的辅助表单中定义公共属性:

1. the secondary Form only needs access to the image contents of the PictureBox on the Main Form: Strategy: (one-way communication between objects)

a. define a Public Property in the secondary Form for a Bitmap:

public Bitmap mainFormBitmap { set; get; }

湾必要时,在主窗体中将对PictureBox(图像)的内容(位图)的引用注入到辅助窗体中:

b. when necessary, in the main Form inject the reference to the contents (Bitmap) of the PictureBox (the Image) into the secondary Form:

// somewhere in the Main Form, in a method, or EventHandler
theSecondaryForm.mainFormBitmap = new Bitmap(this.YourPictureBoxName.Image);

2。辅助表格需要访问主表单上PictureBox的图像内容,并将修改主表单上PictureBox的内容:策略:(对象之间的双向通信)



a。在PictureBox的辅助表单中定义公共属性

2. the secondary Form needs access to the image contents of the PictureBox on the Main Form, and will modify the contents of the PictureBox on the Main Form: Strategy: (two-way communication between objects)

a. define a Public Property in the secondary Form for a PictureBox

public PictureBox mainFormPictureBox { set; get; }

湾必要时,在主窗体中将PictureBox的引用注入到辅助窗体中:

b. when necessary, in the main Form inject the reference to the PictureBox into the secondary Form:

// somewhere in the Main Form, in a method, or EventHandler
theSecondaryForm.mainFormPictureBox = this.YourPictureBoxName;

℃。当辅助表单需要更新/更改主表单上PictureBox的(图像)内容时,它可以简单地执行:

c. when the secondary Form needs to update/change the contents of (the Image) of the PictureBox on the Main Form, it can simply do:

// assume you have created a transformed Image from the source Image
mainFormPictureBox.Image = transFormedImage;

示例:让我们说你正在使用这种技术(#2),并且您已经定义了一个函数来反转图像:如果您在辅助表单上有一个触发了它的按钮,那么您的代码可能如下所示:

Example: let's say you are using this technique (#2), and you have defined a function to invert an Image: if you had a Button on the secondary Form that triggered this, your code might look like this:

private void InvertImage_Click(object sender, EventArgs e)
{
    mainFormPictureBox.Image = Invert((Bitmap) mainFormPictureBox.Image);
}

private Bitmap Invert(Bitmap bitmapImage)
{
    // code to invert Bitmap here ...

    return bitmapImage;
}

参见[ ^ ]用于示例图像转换代码。



还有其他几种方法可以创建对象之间的交互(在本例中,主要和次要表格),Maciej Los在回复中提供的链接可以教你如何使用这些技巧,如果你研究它们。



取决于在这种情况下,主要和次要表格之间所需的行为关系,我会考虑让二级表格提出一个主要表格可以订阅的事件:提出此事件将告诉主表格可能的变更是建议其PictureBox的内容。通过在辅助表单中使用事件定义的自定义EventArgs类,可以将转换后的图像数据传递到主窗体。



如果要查看示例这个方法,请问,我会发布一些代码。

See [^] for sample image transform code.

There are several other ways to go about creating interaction between the objects (in this case, a Main and a secondary Form), and the links supplied in the reply here by Maciej Los can teach you how to use those techniques, if you study them.

Depending on the behavioral relationship desired between the Main and secondary Form in this case, I would consider having the secondary Form raise an Event, which the Main Form can subscribe to: the raising of this Event would "tell" the Main Form that a possible change is proposed to the contents of its PictureBox. The transformed image data could be passed to the Main Form by using a custom EventArgs class for the Event definition in the secondary Form.

If you want to see an example of this approach, just ask, and I'll post some code.


这是非常糟糕的设计。



没有表格应该知道关于任何其他形式或控制的任何事情。表单代码只应该关注自己的控件,这就是全部。



如果你有数据需要传回父表单,你的数据模型如果需要的话,应该把这个数据保存到另一个表格中。
That's really bad design.

No form should know anything about any other form or the controls on them. Form code should only be concerned with the control on its own form, that's all.

If you have data you need to pass back to a parent form, your data model should hold this data for another form to pickup if it needs to.


你好,



最简单的解决办法是设置pictureBox1修饰符公共(在属性窗口中)。这将通过调用MainForm.pictureBox1从外部窗体访问pictureBox1控件。然后你需要将对MainForm的引用传递给ResizeForm。



1.将私有字段添加到ResizeForm并修改ResizeForm的构造函数:



Hello,

Simplest solution is to set pictureBox1 modifiers to Public (in Properties window). This will give you access to pictureBox1 control from outside form by calling MainForm.pictureBox1. Then you need to pass reference to MainForm to the ResizeForm.

1. Add private field to ResizeForm and modify ResizeForm's constructor:

public partial class ResizeForm : Form
{
    private Form _mainFormReferrence;

    public ResizeForm(Form mainForm)
    {
        InitializeComponent();

        _mainFormReferrence = mainForm;
    }
}







然后从ResizeForm里面你可以访问pictureBox置于MainForm中:




Then from inside of ResizeForm you can access pictureBox placed in of MainForm:

_mainFormReferrence.pictureBox1





2.假设你从MainForm创建ResizeForm,修改你的代码:





2. Assuming that you creating ResizeForm from MainForm, modify your code to something like that:

var resizeForm = new ResizeForm(this);
resizeForm.Show();





完成此任务的其他方法是使用接口。



我希望我能帮到你。



Other way to acomplish this task is to use interfaces.

I hope i helped you.


这篇关于以子格式获取主窗体对象c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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