在C#中的表单之间传递信息 [英] Passing Info Between Forms in C#

查看:73
本文介绍了在C#中的表单之间传递信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道对此也有类似的问答。我似乎尚未找到所需的答案,但很可能错过了。其次,我是C#语言的新手,因为我大部分时间都在C ++中工作,所以如果这是一个愚蠢的问题,请原谅我。

First off I know there have been similar Q&A for this. I haven't seem to have found the answer I am looking for, but it's possible I missed it. Second I am new to the C# language as I've mostly worked in C++ so please forgive me if this is a stupid question.

现在我有点儿背景了试图完成。我正在制作Paint应用程序。第一种形式叫Form1,我将其称为所有应用程序的用户界面,用户将在其中进行绘图。我想允许用户选择不同的画笔类型和大小。在Form1上,我有一个按钮,用户单击该按钮即可更改这些选项。点击此按钮后,它将启动我称为Form2的内容。 Form2将具有画笔类型和大小的选项,并且当用户选择它们并单击确定按钮时,尺寸和画笔类型应传回。我只是使用两个int变量来保存笔刷类型和笔刷大小,以使事情简单,因为Form1需要知道这一点,而不是Form2。

A little background now on what I am trying to accomplish. I'm in the process of making a Paint application. The first form, Form1 as I'll call it is where all of the UI is for my application and where the user will draw. I want to allow the user to select different brush types and sizes. On Form1 I have a button which the user will click to change these options. When this button is clicked it will initiate what I'll call Form2. Form2 will have the options for brush type and size and when the user selects them and hits the OK button the size and brush type should be passed back. I am just using two int variables to hold the brush type and the brush size to keep things simple since Form1 needs to know this, not Form2.

当我真的想将信息从Form2传递到Form1时,我发现的所有信息都是用于将信息从Form1传递给Form2的。有没有简单的方法可以做到这一点?我还将为其他几个按钮传递这样的信息,所以我希望不要使事情变得过于复杂。

All of the information I have found is for passing information from Form1 to Form2, when I really want to pass information from Form2 to Form1. Is there a simple way to do this? I will be passing information like this for several other buttons as well so I am hoping not to over complicate things.

非常感谢您的宝贵时间!!! :)

Thank you very much for your time!!! :)

这是在Form1中调用Form2的

private void brushBtn_Click(object sender, EventArgs e)
{
    //New form which will ask which brush type and the size 
    Form2 paintInfo = new Form2() ;
    paintInfo.ShowDialog();  
}

这是Form2

public partial class Form2: Form
{
    public Form2()
    {
        InitializeComponent();
    }

    int typeOfBrush;   

    //User picked the circle brush 
    private void circleBrushBtn_Click(object sender, EventArgs e)
    {
        typeOfBrush = 1 ; 
    }

    //User picked the square brush 
    private void squareBrushBtn_Click(object sender, EventArgs e)
    {
        typeOfBrush = 2 ;
    }

    private void okBtn_Click(object sender, EventArgs e)
    {
        //PASS THE BRUSH TYPE & SIZE BACK TO FORM1 WHEN USER HITS OK BUTTON

        this.Close() ;

    }
}


推荐答案

通常,这种程序的工具选项板包含各种工具。

选项板是非模态的,这意味着当您显示调色板。它停留在某个角落,您可以单击调色板中的图标来更改行为。

Usually this kind of program has a tool palette that contains the various tools.
The palette is non modal, meaning that you don't block the execution of code in the first form when you show the palette. It stays in some corner and you click over the icons in the palette to change your behavior.

但是,如果是这种情况,则将信息传递回form1需要委托和事件处理程序

But, if this is the case, then passing back information to form1 requires a delegate and an event handler

因此,在Form2代码中,您拥有

So, inside Form2 code you have

public enum ToolEnum
{
    Draw = 1,
    Rubber = 2,
    Fill = 3,
    ......
}

public class Form2
{
     public delegate void OnToolChanged(ToolEnum newTool);
     public event OnToolChanged ToolChanged;

     ....

     protected override palette_Click(object sender, EventArgs e)
     {
         // Code that retrieves the tool clicked....
         ToolEnum choosenTool = GetTool();
         // If someone is interested to know when the user changes tool 
         // then it has subscribed to the ToolChanged event passing an 
         // appropriate event handler 

         if(ToolChanged != null)
             ToolChanged(choosenTool);
     }
}

form1以这种方式调用form2

The form1 call the form2 in this way

 // Open the form with the palette passing reference to this instance of form1.
 // Important to link the two forms together....
 Form2 f = new Form2(this);
 // Now inform the form2 instance that this instance of form1 
 // wants to know when the user clicks another tool
 f.ToolChanged += myToolChangedHandler;
 ....

 // We receive here the notification of the click event on the palette form
 public void myToolChangedHandler(ToolEnum newTool)
 {
     if(newTool == ToolEnum.Fill)
     {
         ... adapt for the fill operation ...
     }
 }

编辑

但是,如果您希望遵循更简单的方式来显示Form2,那么代码很容易

EDIT
However, if you want the follow the simpler road of showing modally your Form2, then the code is easy

private void brushBtn_Click(object sender, EventArgs e)
{
    //New form which will ask which brush type and the size 
    using(Form2 paintInfo = new Form2())
    {
        paintInfo.ShowDialog();  
        int brushType = paintInfo.BrushType;
    }
}
....    

public partial class Form2: Form
{
    public Form2()
    {
        InitializeComponent();
    }

    int typeOfBrush;   
    public int BrushType 
    {
       get {return typeOfBrush;}
       set {typeOfBrush = value;}
    }
    private void circleBrushBtn_Click(object sender, EventArgs e)
    {
        this.BrushType = 1 ; 
    }
}

这篇关于在C#中的表单之间传递信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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