0

我有一个方法是这样的:

    public void Report(Form form, string[] textboxes, string[] patientdetails)
    {
        try
        {
            int i = 0;
            foreach (string textbox in textboxes)
            {
                form.Controls.OfType<TextBox>().FirstOrDefault(n => n.Name == textbox).Text = patientdetails[i];
                i++;
            }

            form.ShowDialog();
        }
        catch (Exception ex)
        {
        }
    }

通过将参数传递给这些未设置为实例对象的返回对象引用:

 string[] textboxes = new string[] { "txtPatientName", "txtAge", "txtGender","txtTestType","txtDate" }; 
 string[] patientDetails = new string[]{"Ammar Bashir", "19", "Male", "White Blood Cell Test", "12 March , 2013"};

 //Test a winform which contain textboxes.
  Report(Test, textboxes, patientDetails);
4

2 回答 2

0

I got it , Actually all the textboxes were in first splitterPanel of splitContainer, I traversse through them with their 'Controls' property and changed the Text property of TextBoxes...Guys thanks for your support.

于 2013-03-13T19:22:06.103 回答
0

在没有看到其余代码或确切知道异常是由哪一行引发的情况下,我猜您Test在将其传递给方法之前没有进行初始化。

要么,要么

 form.Controls.OfType<TextBox>().FirstOrDefault(n => n.Name == textbox)

找不到匹配项,因此返回null. 然后你调用whichTextnull抛出异常。

于 2013-03-13T03:10:29.137 回答