0

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

现在对我正在努力完成的事情有一点背景。我正在制作一个 Paint 应用程序。第一个表单,我称之为 Form1,是我的应用程序的所有 UI 以及用户将绘制的地方。我想让用户选择不同的画笔类型和大小。在 Form1 上,我有一个按钮,用户可以单击该按钮来更改这些选项。单击此按钮时,它将启动我称之为 Form2 的内容。Form2 将具有画笔类型和大小的选项,当用户选择它们并点击 OK 按钮时,应将大小和画笔类型传回。我只是使用两个 int 变量来保存画笔类型和画笔大小以保持简单,因为 Form1 需要知道这一点,而不是 Form2。

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

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

这是在调用 Form2 的 Form1 中

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() ;

    }
}
4

2 回答 2

1

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

但是,如果是这种情况,那么将信息传递回 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 ; 
    }
}
于 2014-08-14T19:49:53.243 回答
1

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

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

于 2014-08-14T19:42:01.183 回答