在C#中传递表单之间的信息

首先,我知道有类似的问答。 我似乎没有找到我想要的答案,但我可能错过了它 。 其次我是C#语言的新手,因为我主要使用C ++,所以如果这是一个愚蠢的问题,请原谅我。

现在我想要完成的一些背景知识。 我正在制作一个Paint应用程序。 我称之为Form1的第一个表单是所有UI都适用于我的应用程序以及用户将绘制的位置。 我想允许用户选择不同的画笔类型和尺寸。 在Form1上,我有一个按钮,用户将单击该按钮来更改这些选项。 单击此按钮时,它将启动我称之为Form2的内容。 Form2将有刷子类型和大小的选项,当用户选择它们并点击OK按钮时,应该传回尺寸和刷子类型。 我只是使用两个int变量来保持画笔类型和画笔大小以保持简单,因为Form1需要知道这一点,而不是Form2。

我发现的所有信息都是用于将信息从Form1传递到Form2,当我真的想将信息从Form2传递给Form1时。 有一个简单的方法吗? 我会将这样的信息传递给其他几个按钮,所以我希望不要过于复杂化。

非常感谢您的宝贵时间!!! 🙂

这是在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() ; } } 

通常这种程序有一个包含各种工具的工具调色板。
调色板是非模态的,这意味着当您显示调色板时,不会阻止以第一种forms执行代码。 它停留在某个角落,然后单击调色板中的图标以更改您的行为。

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

所以,在Form2代码中你有

 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

  // 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模式,那么代码很容易

 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 ; } } 

不要过度思考这个问题。 表单只是一个对象。 在Form1中,您将创建一个Form2类型的新对象。 Form2可以具有任何属性(在您的情况下为两个整数),可以根据需要设置。 假设您使用的是WinForms,您可能希望通过ShowDialog()显示Form2,这是一个阻塞调用。 当ShowDialog()返回时,您可以询问Form2(您仍然拥有Form1中对象的句柄)的任何属性。

如果这还不足以让你入门,我相信其他人会发布一个完整的编码解决方案。