Showing a Form

How would I go about showing a form FormName.Show() doesn't work, and I don't know what will.


Answer this question

Showing a Form

  • Trigun

    Hi,
    can you show a little example that reproduces your problem


  • Carlos Gonçalves

    See, I have two forms (Form1 and Form2). When an item from a treeview control that is on Form1 is clicked, I want Form2 to show. I already have the function that runs when the treeview control is clicked (private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)), but I don't know how to display Form2.  When I try Form2.Show(), I get the following error:

    Error    1    An object reference is required for the nonstatic field, method, or property 'System.Windows.Forms.Control.Show()'    C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\CSharpTut\CSharpTut\Form1.cs    25    13    CSharpTut


  • Alex_Petrovic

    That's because the object (by its name Form2 i suppose it's a Form2's object) was disposed some how. This happens when you close the form for example but in your case I don't know why it's giving that exception. You should try and run the debugger and see where it crashes and why it crashes.


  • Rick Hoskins

    Thanks, now it builds. But now when I click on the treeview control item that triggers Form2 to show, this error pops up that looks like this:

    Click




  • Santosh Kumar

    Ok, now I understand your problem. Form2 is a class not an object (instance of the class) and you can only call methods in that way (ClassName.Method() -- in your case Form2.Show()) if they're static.

    So, the answer to your problem is:
    1- Create an instance of Form2.
    2- Call the Show() method.

    // create the form2 object
    Form2 form2 = new Form2();
    // show the form
    form2.Show();


  • Showing a Form