本文介绍了形式之间传递值(的WinForms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传递值和从第二个窗体时,古怪的行为。

Wierd behaviour when passing values to and from second form.

ParameterForm pf = new ParameterForm(testString);

工作

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

没有(定义为公共字符串的TestString)

doesn't (testString defined as public string)

也许我失去了一些东西?无论如何,我想提出第二个变种正常工作,因为现在 - 它返回null对象引用错误

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

感谢您的帮助。

发布多个code在这里:

Posting more code here:

要求

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);
                };

定义和使用

   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 referencetest - null object reference

推荐答案

  • pf.ShowDialog(本); 是一个阻塞调用,所以 pf.Submit + =新ParameterForm.ParameterSubmitResult(pf_Submit) ; 永远也无法达到:切换顺序

  • pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.

    提交(这一点,this.node); 抛出一个空对象引用,因为没有事件被分配给它(见上文)。通常情况下,你应该总是先检查:如果(!提交= NULL)提交(这一点,this.node);

    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);

    您应该修改`pf.ShowDialog(本); pf.Show(本); 所以,当你的对话框打开,如果这就是你想要的东西,或者使用型号下方的主要形式是不会被禁用(典型的对话框。)

    You should change `pf.ShowDialog(this); to pf.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.)

    我不知道是什么 pf_Submit 是应该做的,所以这可能不是去了解它在你的应用程序的最佳方式,但它是如何通用继续?是/否问题的工作。

    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()
        }
     }
    

    这篇关于形式之间传递值(的WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 08:50