passing values between forms (winforms)

前端 未结 3 1469
终归单人心
终归单人心 2020-11-28 16:16

Wierd behaviour when passing values to and from second form.

ParameterForm pf = new ParameterForm(testString);

works

Parame         


        
相关标签:
3条回答
  • 2020-11-28 16:31
    • pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.

    • Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);

    • You should change ``pf.ShowDialog(this);topf.Show(this);` so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)


    I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.

    Button ParametersButton = new Button();
    ParametersButton.Click += delegate
        {
            ParameterForm pf = new ParameterForm(testString);
            pf.ShowDialog(this); // Blocks until user submits
            // Do whatever pf_Submit did here.
        };
    
    public partial class ParameterForm : Form
    {
        public string test;     // Generally, encapsulate these
        public XmlElement node; // in properties
    
        public void SubmitButton_Click(object sender, EventArgs e)
        {
            Debug.WriteLine(test);
            this.Close(); // Returns from ShowDialog()
        }
     }
    
    0 讨论(0)
  • 2020-11-28 16:31

    When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).

    EDIT (a bit more precise):

    You could write:

    pf.getString(testString);
    

    , if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.

    0 讨论(0)
  • 2020-11-28 16:55

    the thing was in line order :)

    pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
    

    and

    pf.Test = "test";
    

    should have been set before

       pf.ShowDialog(this);
    

    my mistake thingking that parameter can be passed after 2nd form was displayed

    thnx for answers

    0 讨论(0)
提交回复
热议问题