I am totally confused about forms in c#.
If someone can explain the following simply please.

I have a main form - called Front.
under the section partial class Front : Form
I have a Form newF= new Form();

More than once in the program I call the newF form with different button layouts etc. I close it each time so that the display changes as required.

Sometimes I get an exception error because the form doesn't exist.
As I understand it I need to check if newF exists, before calling am instance.

The problem is if I try code to check if newF exists, the program says I can't refer to it when it doesn't exist.

How do I overcome this please?

In C# forms is a class. You can use the forms same like other reference types.

Sometimes I get an exception error because the form doesn't exist.
As I understand it I need to check if newF exists, before calling am instance.

I didn't get what exactly you are trying to do? Could please provide some code sample?

The null reference exception is thrown, when the reference type variable is not pointing to any object in the heap.

put the code where u call the form into a try catch block and post the exception that u get.

You have to create a object Form as global variable - so it can be accessed from every place in the class, and there where you want to open it, simple create a new instance of the Form object and open it:

public partial class Form1 : Form
    {
        //This is a global variable:
        Form2 form2;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //create a new instance of the variable:
            form2 = new Form2();
            form2.Show(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //same here:
            //create a new instance of the variable:
            form2 = new Form2();
            form2.Show(this);
        }
    }

Thanks to all,
Mitja,
Your example shows where I was going wrong. I was calling a new form globally rather than creating a global variable. If I understand your point, button1 creates a new form shows it and when the form is closed, button2 creates the next instance etc.

No I'm afraid I spoke too soon.
I still have the problem.
Snippets are difficult as they are taken out of context but I'll try.

As a global definition

Form newF;

later in code called - which is the first time the form is called
Form newF = new Form();

All goes well - then when the form is accessed again in a different method (without being shown yet)

newF.Text="Edit or enter text for " + lv.Columns[si].Text;
newF.Height = coOrds.Count * 30 + 150;

accessing newF crashes.
The exception is Object reference not set to an instance of an object.
Null reference exception is unhandled.

I notice my Global definition is different, and I suspect this is the issue, but I am uncertain what my definition should be like.

1st of all, you wrote:
As a global definition

Form newF;

later in code called - which is the first time the form is called
Form newF = new Form();
WRONG!
Why you have again Create a new variable?
Take a look into my exampe, and do:

As a global definition

Form newF;

later in code called - which is the first time the form is called
newF = new Form();

ONE MORE THING: Why is the name of your form "Form"? This is impossible, becase Form is the Reserved word! It can be FormX, but never just a form. If you look on the top of the code edior in the namespace, there is written:

public partial class Form1 : Form //OK!
{
      //Form is a reserved word!!
}

//and you can NOT write this way:
public partial class Form : Form //this is WRONG!
{      
}

-----------------------------------------------------------------------
2nd: It never good to do this kind of coding over forms. This code you can do on the constructor of the "newF", not from main form. For changing the form. you can pass values you need to pass from Main to NewForm. you can do:

//main form:
newF = new Form(lv.Columns[si].Text, coOrds.Count);
newF.Show(this);

//newF:
//constructor
public Form()
{
    InitializeComponent(string item, int value);
    this.Text="Edit or enter text for " + item;
    this.Height = value * 30 + 150;
}

Hope this helps explaining the issue abour forms and shows how to pass data from one form to another and open it.
Mitja

Mitja,

Thank you, this is the type of straightforward direction I needed.
I knew I was totally confused. I will work with your examples and see how I go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.