Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Showing and Hiding Multiple forms (http://www.programmingforums.org/showthread.php?t=11305)

crawforddavid2006 Sep 13th, 2006 2:29 PM

Showing and Hiding Multiple forms
 
I have been try this for a while now (about a week) and did some research and eventually came up with the following code.

[php]
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace multiple_windows
{
public partial class MainForm
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}

public MainForm()
{
InitializeComponent();
}

void MainFormLoad(object sender, System.EventArgs e)
{

}

void Button1Click(object sender, System.EventArgs e)
{
multiple_windows.MainForm.ActiveForm.Hide();
multiple_windows.Form1.ActiveForm.Show();
}
}
}

[/php]
when i compile it... it works find but if i click the button it gives me this error.
:

Exception System.NullReferenceException was thrown in debuggee:
Object reference not set to an instance of an object.

Button1Click() - c:\Documents and Settings\David\My Documents\SharpDevelop Projects\multiple windows\multiple windows\MainForm.cs:31,4
OnClick()
OnClick()
OnMouseUp()
WmMouseUp()
WndProc()
WndProc()
WndProc()
OnMessage()
WndProc()
DebuggableCallback()
System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop()
RunMessageLoopInner()
RunMessageLoop()
Run()
Main() - c:\Documents and Settings\David\My Documents\SharpDevelop Projects\multiple windows\multiple windows\MainForm.cs:15,4


I know i'm missing something but i just don't see it. Is anyone able to help me find out whats missing?

Anonymous Coward Sep 13th, 2006 2:45 PM

How about showing the other half of the partial class MainForm. Then we might have a better chance of finding and fixing the problem :)

crawforddavid2006 Sep 13th, 2006 2:56 PM

Sorry. What I posted for MainForm is everything that is in it. I have another form 'Form1' that has its own code... is that what you mean?

_James_ Sep 13th, 2006 3:12 PM

What he means is the class MainForm is split between two files. That is why the keyword partial is used.

What the forms designer does in VS is split the form class in two. One half is for event handlers etc and t'other is for the forms fields(controls etc) and the InitializeComponent() method which is called by the constructor. All the properties you set on your forms and controls would be in this method and being able to see it will help work out what is going wrong. Sadly I have not used Visual C# in ages and cant recal how to bring up the other half of the class. Maybe someone else can explain how to get to it but i dont think its too hard to bring up.

Samuaijack Sep 13th, 2006 5:24 PM

well i don't know if understand it right,here wht i understand :
when you press this button you want the main form to hide and form1 to show.
if so then

:

void Button1Click(object sender, System.EventArgs e)
{
    Form1 frm1 = new Form1();
    this.Hide();
    frm1.Show();
}


if it's not what you want then goodluck figuring it out


All times are GMT -5. The time now is 12:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC