Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

c# - passing values between forms (winforms)

Wierd behaviour when passing values to and from second form.

ParameterForm pf = new ParameterForm(testString);

works

ParameterForm pf = new ParameterForm();
pf.testString="test";

doesn't (testString defined as public string)

maybe i'm missing something? Anyway I'd like to make 2nd variant work properly, as for now - it returns null object reference error.

Thanks for help.

Posting more code here:

calling

Button ParametersButton = new Button();
ParametersButton.Click += delegate
                {
                    ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
                    pf.ShowDialog(this);
                    pf.test = "test";
                    pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
                };

definition and use

   public partial class ParameterForm : Form
    {
        public string test;
        public XmlElement node;
        public delegate void ParameterSubmitResult(object sender, XmlElement e);
        public event ParameterSubmitResult Submit;

        public void SubmitButton_Click(object sender, EventArgs e)
        {
            Submit(this,this.node);
            Debug.WriteLine(test);
        }
     }

result: Submit - null object reference test - null object reference

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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()
    }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...