问题
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.
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.
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!!! :)
This is in Form1 which calls 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();
}
This is 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() ;
}
}
回答1:
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 ;
}
}
回答2:
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.
来源:https://stackoverflow.com/questions/25316230/passing-info-between-forms-in-c-sharp