I am fairly new to C#.NET, only been writing it for about 6 months.
Have writen VB for around 8 years.
I have a simple application I am making in C#.net and I have 2 forms.
I want to hide both form from Form2.
At the moment I have
private void button1_Click(object sender, EventArgs e)
{
Form1.ShowTaskbar();
this.Visible = false;
Form1.Visible = false;
}
Error 1 An object reference is required for the nonstatic field, method, or property 'System.Windows.Forms.Control.Visible.get'
so this.Visible = false works but Form1.Visible = false doesn't.
Loading
Vimal KandasamyPosted Feb 19, 2009, 11:52 PM
once again you have to create a object for form1 in the form2 ( where ever and when ever u want to use that form ,u have to create object for form1..)
it must like
formname obj=new formname();
obj.methods();
it worked well for me.. plz try this.
Posted Feb 19, 2009, 8:35 PM
Program.MainForm
Though I think theres a better way to do it, I think your missing some fundamental concepts of C# and trying to apply things that worked in VB that wont work in C#. A static class in C# is like a Module in VB. Members inside of a C# static class can only be accessed via the class name, such as MyClass.MyMember. You never have to create an instance of a static class (in fact, the compiler will curse you if you try) to access its members just so long as you have access to the class Type.
MainForm doesnt exist in Form2, it exists in Program and therefore Form2 cannot access it.
What exactly are you trying to do? It may be easier if you tell us what you are trying to do and then we can explain it better...
Brad FinnPosted Feb 19, 2009, 4:34 PM
I kind of understand what you mean, the Form1 is an object type which you are creating a new of.
I tried this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form MainForm = new Form1();
Application.Run(MainForm);
}
Form2
private void button1_Click(object sender, EventArgs e)
{
Form1.ShowTaskbar();
this.Visible = false;
MainForm.Visible = false;
}
Error 1 The name 'MainForm' does not exist in the current context
I assume it needs to be public or something :(
any ideas?
Posted Feb 19, 2009, 1:02 AM
Vimal KandasamyPosted Feb 19, 2009, 12:42 AM
in c# ,you have to create a object for form to use its functions.
ex:
if you want to show/hide a form which is named as menu1.then
menu1 m=new menu1();
m.show();
m.hide();
m.hide() is equivalent to form1.visible=false;
try this.