سؤال

I have a method which is something like this:

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

by passing arguments to these return object refrence not set to an instance object:

 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);
هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

Without seeing the rest of your code or knowing exactly what line the exception was thrown by, I'm guessing that you didn't initialize Test before passing it to the method.

Either that, or

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

Is not finding a match and therefore returning null. You then call Text on null which would throw the exception.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top