Form.Show() method not present

  • Thread starter Thread starter alex
  • Start date Start date
A

alex

Hi I created a simple windows app with two forms, my goal is to open a from
from the main form. The problem is that I can't do Form2.Show(); because the
Show() method is not been shown by the Intellisense. Any ideas??

Thanks,
Alex
 
Hi I created a simple windows app with two forms, my goal is to open a
from from the main form. The problem is that I can't do Form2.Show();
because the Show() method is not been shown by the Intellisense. Any
ideas??

Did you used to use VB.NET before switching to C#?

The usual reason people post this question is that they are trying to call
"Show()" on the class name, rather than creating an instance of the class
and calling it on that. No C, C++, or Java programmer would make that
mistake, but VB.NET does a lot of hand-holding, including creating default
instances of classes for you, and so VB.NET programmers often get confused
when presented with the C-derived syntax.

If that's your problem, then just fix your code by instantiating the
object properly and calling the method on the instance.

If that's not your problem, you need to show your code, preferably as a
concise-but-complete code example that reliably shows the problem.

Pete
 
* alex wrote, On 17-9-2009 2:03:
Hi I created a simple windows app with two forms, my goal is to open a
from from the main form. The problem is that I can't do Form2.Show();
because the Show() method is not been shown by the Intellisense. Any
ideas??

Thanks,
Alex

You did make an instance of Form2 first didn't you?

Form2 formToShow = new Form2();
formToShow.Show();
 
I am calling the method Show() from the instance of the form that I created
using the visual environment and not from the class. Here is the code for
the forms:


//Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace CrystalReportsPrinting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

Form2.Show(); //===============HERE IS THE PROBLEM
}
}
}

//Form2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Windows.Forms;

namespace CrystalReportsPrinting
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
}
 
I am calling the method Show() from the instance of the form that I
created using the visual environment and not from the class. Here is the
code for the forms:


[...]
Form2.Show(); //===============HERE IS THE PROBLEM

And indeed, you are doing it wrong exactly as I had guessed.
 
yes, you are right, but how can I find the instance declaration created by
the visual studio? I can run the program without creating an instance
myself, so I know it has to be somewhere.


Peter Duniho said:
I am calling the method Show() from the instance of the form that I
created using the visual environment and not from the class. Here is the
code for the forms:


[...]
Form2.Show(); //===============HERE IS THE PROBLEM

And indeed, you are doing it wrong exactly as I had guessed.
 
no I didn't, but the program runs fine so visual studio must have created
the instance, only I can't find it.
 
* alex wrote, On 17-9-2009 3:56:
no I didn't, but the program runs fine so visual studio must have
created the instance, only I can't find it.

Visual studio does not, by default, nor automatically, create an
instance of the second form.

If you open Program.cs you'll see that Visual studio generated the code
to instantiate and run the default form using Application.Run(new Form1());

So in order to show a second form, you need to create it yourself.

So change button1_click to:

private void button1_Click(object sender, EventArgs e)
{
Form2 formToShow = new Form2();
formToShow.Show(); //===============HERE IS THE PROBLEM
}

and it should work.

Jesse

 
I think I know what is happening, the main form is being created by
Application.Run(new Form1()); so the second form I have to create an
instance by myself.


Peter Duniho said:
I am calling the method Show() from the instance of the form that I
created using the visual environment and not from the class. Here is the
code for the forms:


[...]
Form2.Show(); //===============HERE IS THE PROBLEM

And indeed, you are doing it wrong exactly as I had guessed.
 
thats exactly what is happening, thanks a lot!

Jesse Houwing said:
* alex wrote, On 17-9-2009 3:56:
no I didn't, but the program runs fine so visual studio must have
created the instance, only I can't find it.

Visual studio does not, by default, nor automatically, create an instance
of the second form.

If you open Program.cs you'll see that Visual studio generated the code to
instantiate and run the default form using Application.Run(new Form1());

So in order to show a second form, you need to create it yourself.

So change button1_click to:

private void button1_Click(object sender, EventArgs e)
{
Form2 formToShow = new Form2();
formToShow.Show(); //===============HERE IS THE PROBLEM
}

and it should work.

Jesse
 
I think I know what is happening, the main form is being created by
Application.Run(new Form1()); so the second form I have to create an
instance by myself.

Bingo.
 
In vb.net open Form2 without creating the new Instance like

Form2.Show()

but in C Sharp
Form2.show() // not present

help me because I need it.
 
Back
Top