开发者

Getting values from another winform and passing it to current!

I have 2 windows forms. The 1st one who gets active during start of the program has a button in which another 2nd windows form appears which has text field in which user type their name and HIT okay.

Now in the 1st form I have a variable name: nameproccessed which takes the name from the second form. But the problem is my button which is in the 1st form has more functions besides taking name, how can I stop the flow of the compiler to take first the name from the second form and then process the rest execution of the function???

Here is the snippet of my code:

//1st form
//Class1
 public string _nameProcessed = "";



private void btnGetSomething_Click(object sender, EventArgs e)
    {
          if (!showdial())
            return;
            string name = _nameProcessed;
         //some more functionalities in this function!!!
         //This PART is NOT executing....

     }



       private bool showdial()
    {
        InputName inm = new InputName();
        if (inm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            return false;
        _nameProcessed = inm.name;

        return true;
    }

//2nd form
//Class2
public string name;

 private void btnCancel开发者_运维技巧_Click(object sender, EventArgs e)
    {
        //MessageBox.Show("Please Enter Your Code! ");
        this.Close();
    }

    private void btnOkay_Click(object sender, EventArgs e)
    {
        //string name;
        name = tbxName.Text;
        Form1 ict = new Form1();

        if (name == "")
        {
            MessageBox.Show("Please Enter Your Name!");
            this.DialogResult = System.Windows.Forms.DialogResult.No;

        }
        else
        {
        ict._nameProcessed = name;
        this.Close();
        }

    }

Now I want to take the 'name' from the second form put it on the _nameprocessed in the 1st form and then execute the rest of the function. How can I do that? Any help!


Use ShowDialog instead of Show.

inm.ShowDialog();

that will stop Form1 code till Form2 is closed. Once the form is closed you can read the name variable from it.

_nameProcessed = inm.name;

The final code would be:

private void showdial()
{
  InputName inm = new InputName();
  inm.ShowDialog();
  _nameProcessed = inm.name;

}

EDIT for second problem: Buttons have a property named "DialogResult". Set the "OK" button to OK and "Cancel" one to Cancel. On the code you just need a small IF and change function to return a bool:

private bool showdial()
{
  InputName inm = new InputName();
  if (inm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
      return false;
  _nameProcessed = inm.name;

  return true;
}

Then at the button click

private void btnGetSomething_Click(object sender, EventArgs e)
{
     if(!showdial())
         return;
     //some more functionalities in this function!!!
 }

EDIT 2 -- Add this line 'this.Close()':

this.DialogResult = System.Windows.Forms.DialogResult.OK;

Making the final code:

private void btnOkay_Click(object sender, EventArgs e)
{
    //string name;
    name = tbxName.Text;
    Form1 ict = new Form1();

    if (name == "")
    {
        MessageBox.Show("Please Enter Your Name!");
    }
    else
    {
    ict._nameProcessed = name;
    this.DialogResult = System.Windows.Forms.DialogResult.OK;
    this.Close();
    }

}


In second form save input in a public field. Show after hiding the second form you can still access public variables. So get the value and dispose second form.

EDIT: To stop code execution in form one. use

secondForm.ShowDialog();

as suggested by Jorge

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜