开发者

Return result between Windows Forms in C#

I have two Windows Forms (MyApp, Generator), and I need to call Generator from MyApp

Form gen = new Generator();
gen.Show();
string result = gen.IDontKnowWhatToDoHere();

My Generator.cs Form has three TextBox and a Button Ok, so when the user type some text in the three TextBoxes an click Ok I want to get the the Text typed in those three TextBoxes.

D开发者_StackOverflowo you have any ideas how I can achieve this.

Thanks.


I generally use this pattern:

TResult result = Generator.AskResult();

class Generator : Form
{
    // set this result appropriately
    private TResult Result { get; set; }
    public static TResult AskResult()
    {
        var form = new Generator();
        form.ShowDialog();
        return form.Result; // or fetch a control's value directly
    }
}

This makes it work in a single statement, similar to a MessageBox, and does not require making any controls public. You can also include any additional data as parameters to the static method and pass them to the form's constructor or set properties appropriately.

Another benefits includes the ability to, if the need arises, reuse instances of the form.


class Generator : Form
{
    public string Text1 { get; private set; }

    void ok_Click (object sender, EventArgs)
    {
        this.Text1 = txt1.Text;
        ...
        this.Close();
    }
}

Form gen = new Generator();
gen.ShowDialog();
string text1 = gen.Text1;
...

class TextInfo
{
    public string Text1 { get; set; }
    ...
}

class Generator : Form
{
    public TextInfo Textinfo { get; private set; }

    public Generator(TextInfo info)
    {
        this.TextInfo = info;
    }

    void ok_Click (object sender, EventArgs)
    {
        this.TextInfo.Text1 = txt1.Text;
        ...
        this.Close();
    }
}

TextInfo info = new TextInfo();
Form gen = new Generator(info);
gen.ShowDialog();
string text1 = info.Text1;


If you need data from a no modal form you should use events or other notification pattern.

class OkPressedEventArgs : EventArgs
{
   public OkPressedEventArgs(string text1, string text2, string text3)
   {
      Text1 = text1;
      Text2 = text2;
      Text3 = text3;
   }

   public string Text1 {get;private set;}
   public string Text2 {get;private set;}
   public string Text3 {get;private set;}
}

class SourceForm : Form
{
   public event EventHandler<OkPressedEventArgs> OkPressed;
   private void OnOkPressed()
   {
      if(OkPressed != null)
      {
          OkPressed(this, new OkPressedEventArgs(textBox1.Text, textBox2.Text, textBox2.Text);
      } 
   }

   private void okButton_Click(object source, EventArgs e)
   {
       OnOkPressed();
   }
}

class TargetClass
{
   void ShowFormMethod()
   {
      var form = new SourceForm();
      form.OkPressed += OkPressedHandler;
      form.Show();
   }

   private void OkPressedHandler(object source, OkPressedEventArgs e)
   {
      // process form data here!
   }
}


Form gen = new Generator();
gen.ShowDialog();
string result = gen.IDontKnowWhatToDoHere();

But i think is more effectives been create delegate.


IF Generator is a form so it inherits from Form, change the line to:

Generator gen = new Generator();
gen.Show();
string result = gen.SomePublicVariableOrProperty;


I think you can try by passing the Current Form instance as parameter to the constructor of the Generator form.

Once you have filled in the Textbox on click of the event Assign it to the Property in Main Form with the help of the instance which was passed in the constructor.

Form gen = new Generator(this);
gen.Show();

public Generator(Form F1)
{
    InitializeComponent();
    form1 = F1 as ParentForm; // now you have parent reference
}

Button Click of the Generator form

form1.Somproperty assign the values

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜