Passing Info Between Forms in C# [duplicate]

陌路散爱 提交于 2019-11-28 14:35:18

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.

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

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

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

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

Don't overthink this. A form is just an object. From within Form1 you will create a new object of type Form2. Form2 can have whatever properties (two ints in your case) that can be set however you wish. Assuming you are using WinForms, you will probably want to show Form2 via ShowDialog() which is a blocking call. When ShowDialog() returns, you can then interrogate Form2 (you still have a handle to the object in Form1) about any of its properties.

If this is not enough to get you started, I'm sure someone else will post an entire coded solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!