1

I create a form that will serve as InputBox

    public partial class InputBox : Form
    {
       public string sample
       {
          get
          { return TextBox1.Text; }
          set
          { TextBox1.Text = value; }
       }

       public InputBox(string title, string question)
       {
          this.Text = title;
          Label1.Text = question;
          InitializeComponent();
       }
    }

In my other form I call this form:

    private void Button1_Click(object sender, EventArgs e)
    {
        InputBox dlg = new InputBox("TITLE", "Sample Question ?");
        dlg.ShowDialog();

        if (dlg.DialogResult == DialogResult.OK)
        {
            TextBox1.Text = dlg.sample;
        }
    }

Why I got NullReferenceException? Object reference not set to an instance of an object.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
Karlx Swanovski
  • 2,869
  • 9
  • 34
  • 67
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine Oct 04 '15 at 09:07

1 Answers1

3

do as below

public InputBox(string title, string question)
{
    InitializeComponent();
    this.Text = title;
    Label1.Text = question;
}

first Initialize the Components, after that you can set properties

Damith
  • 62,401
  • 13
  • 102
  • 153