13 Replies - 925 Views - Last Post: 08 July 2009 - 07:44 AM Rate Topic: -----

#1 theGman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-July 09

opening a window

Post icon  Posted 07 July 2009 - 01:39 AM

Hey guys, a fairly simple question here... I'm writing a hangman game, my main window contains a start button (Start) to start the game which is on another window called 'StartGame' and I'm a bit lost on what to add in the code of the main window (below) in order to link to the 'StartGame' window where the game is. Could anyone please help me?

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

		}




Is This A Good Question/Topic? 0
  • +

Replies To: opening a window

#2 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: opening a window

Posted 07 July 2009 - 01:59 AM

For example like so:

StartGame Main = new StartGame();//instanciates a new StartGame() with name main
Main.Show();


Main is just a name I used, just pick what you like.

This post has been edited by Renagado: 07 July 2009 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 theGman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-July 09

Re: opening a window

Posted 07 July 2009 - 02:07 AM

View PostRenagado, on 7 Jul, 2009 - 12:59 AM, said:

For example like so:

StartGame Main = new StartGame();//instanciates a new StartGame() with name main
Main.Show();


Main is just a name I used, just pick what you like.


Thanks alot for the quick reply! I appreciate it :)
Was This Post Helpful? 0
  • +
  • -

#4 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: opening a window

Posted 07 July 2009 - 02:19 AM

Glad to help, good luck with your program:)
Was This Post Helpful? 0
  • +
  • -

#5 theGman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-July 09

Re: opening a window

Posted 07 July 2009 - 03:52 AM

View PostRenagado, on 7 Jul, 2009 - 01:19 AM, said:

Glad to help, good luck with your program:)



Thanks, I tried the code and it gave me the following errors :

error CS0118: 'StartGame' denotes a 'namespace' where 'class' was expected.
error CS0119: 'HangManGame.MainForm.Main(string[]) denotes a 'method' which is not valid in the given context

Any idea what's wrong?

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: opening a window

Posted 07 July 2009 - 04:46 AM

try
StartGame sg = new StartGame();
sg.Show();


Was This Post Helpful? 0
  • +
  • -

#7 frostyraver  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 195
  • Joined: 21-April 09

Re: opening a window

Posted 07 July 2009 - 06:54 AM

View Postb.ihde, on 7 Jul, 2009 - 03:46 AM, said:

try
StartGame sg = new StartGame();
sg.Show();


You'll find that crating it when you click the button will just keep creating new windows with the sg in memory, I know with my programs this was annoying, First what you should do is at the top of your program, along with your 'global' varibles you want to use you should have StartGame sg = new StartGame();, so that the program keeps this form in memory, then all you need to do is sg.Show(); to show it, and sg.Hide(); to hide it, when you click your button the form will show the current one in memory BECAUSE we're not creating a new form object, you're using your old one, thus retaining the information on it.

If you want a new form do it the wat b.ide said but when you 'close' a form use .dispose, so you clean it out of memory.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: opening a window

Posted 07 July 2009 - 07:18 AM

also, if a form is already open but is hidden, you can use this code to get it's instance to reshow it instead of creating a new instance...

Form2 oldForm2 = (Form2)Application.OpenForms["Form2"];


Was This Post Helpful? 1

#9 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: opening a window

Posted 07 July 2009 - 08:31 AM

Hmm the code of your form StartGame, does it look something like this:

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 WindowsFormsApplication1
{
	public partial class StartGame : Form
	{
		public StartGame()
		{
			InitializeComponent();
		}
	}
}


The namespace should be the same as your other form and program.cs, if not you need to import that namespace. Can you post some code here if you can't figure it out?
Was This Post Helpful? 0
  • +
  • -

#10 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: opening a window

Posted 07 July 2009 - 10:25 AM

View Posteclipsed4utoo, on 7 Jul, 2009 - 02:18 PM, said:

also, if a form is already open but is hidden, you can use this code to get it's instance to reshow it instead of creating a new instance...

Form2 oldForm2 = (Form2)Application.OpenForms["Form2"];



Just by the way, i would always use .ShowDialog() to call another form..(if you do not have to use the mainForm when the Form per ShowDialog opened is open)

As we know, if a user has the chance to open as many forms as he like to open, he probably will do!
Per example he minimize a opened form, and forget it, he will open a new one..
This may cause errors..

Also you can write a code, to don t let the user create a new Form, which is already open, but this should just be an
advice.

regards

Ben
Was This Post Helpful? 0
  • +
  • -

#11 theGman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-July 09

Re: opening a window

Posted 08 July 2009 - 01:39 AM

View PostRenagado, on 7 Jul, 2009 - 07:31 AM, said:

Hmm the code of your form StartGame, does it look something like this:

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 WindowsFormsApplication1
{
	public partial class StartGame : Form
	{
		public StartGame()
		{
			InitializeComponent();
		}
	}
}


The namespace should be the same as your other form and program.cs, if not you need to import that namespace. Can you post some code here if you can't figure it out?



Hey man thanks for your reply... here's all my basic code from both forms right now so you and everyone else can take a look, I'm really frustrated so your help is appreciated!


using System;
using System.Drawing;
using System.Windows.Forms;

namespace hang1
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.button2 = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// button2
			// 
			this.button2.Font = new System.Drawing.Font("Gill Sans Ultra Bold Condensed", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button2.Location = new System.Drawing.Point(168, 120);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(88, 48);
			this.button2.TabIndex = 2;
			this.button2.Text = "Quit";
			this.button2.Click += new System.EventHandler(this.Quit);
			// 
			// button1
			// 
			this.button1.Font = new System.Drawing.Font("Impact", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button1.Location = new System.Drawing.Point(32, 120);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(96, 48);
			this.button1.TabIndex = 1;
			this.button1.Text = "Start";
			this.button1.Click += new System.EventHandler(this.Start);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Showcard Gothic", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(72, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(144, 40);
			this.label1.TabIndex = 0;
			this.label1.Text = "Hang Man";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label1);
			this.Name = "MainForm";
			this.Text = "MainForm";
			this.ResumeLayout(false);
		}
		#endregion
		void Start(object sender, System.EventArgs e)
		{
			
		}
		
		void Quit(object sender, System.EventArgs e)
		{
			
		}
		
	}
}




And here's the code for the game window that I wanna open up


using System;
using System.Drawing;
using System.Windows.Forms;

namespace hang2
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(64, 168);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(160, 20);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "textBox1";
			this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.textBox1.TextChanged += new System.EventHandler(this.TextBox1TextChanged);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Showcard Gothic", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Image = ((System.Drawing.Image)(resources.GetObject("label1.Image")));
			this.label1.Location = new System.Drawing.Point(64, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(152, 112);
			this.label1.TabIndex = 1;
			this.label1.Text = "Hang Man";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 244);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Name = "MainForm";
			this.Text = "Hang Man";
			this.ResumeLayout(false);
		}
		#endregion
		void TextBox1TextChanged(object sender, System.EventArgs e)
		{
			
		}
		
	}
}




Thanks! :)

Thanks everyone for adding your suggestions! :)
Was This Post Helpful? 0
  • +
  • -

#12 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: opening a window

Posted 08 July 2009 - 06:42 AM

Ah, what I suspected is true, you've got the namespaces mixed up. You're using
namespace hang1

and
namespace hang2

And that's why you get an error when you refer to a method or class from the other namespace. There are several ways to fix this, I think the easiest now is to add:
using hang2

to the code of your first form. so it becomes like so:
using System;
using System.Drawing;
using System.Windows.Forms;
using hang2;

namespace hang1
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	

You could also rightclick on hang2, select Refactor, then Rename(to hang1), but I thought that was only in VS, not the express version. So it depends what you use. Or change it all by hand.

I also suggest you look a bit into the meaning of namespaces, and how they relate to the classes and methods in them, but feel free to ask here if you're stuck :)

This post has been edited by Renagado: 08 July 2009 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

#13 theGman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-July 09

Re: opening a window

Posted 08 July 2009 - 07:35 AM

View PostRenagado, on 8 Jul, 2009 - 05:42 AM, said:

Ah, what I suspected is true, you've got the namespaces mixed up. You're using
namespace hang1

and
namespace hang2

And that's why you get an error when you refer to a method or class from the other namespace. There are several ways to fix this, I think the easiest now is to add:
using hang2

to the code of your first form. so it becomes like so:
using System;
using System.Drawing;
using System.Windows.Forms;
using hang2;

namespace hang1
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	

You could also rightclick on hang2, select Refactor, then Rename(to hang1), but I thought that was only in VS, not the express version. So it depends what you use. Or change it all by hand.

I also suggest you look a bit into the meaning of namespaces, and how they relate to the classes and methods in them, but feel free to ask here if you're stuck :)


Thanks man, that helps... I guessed it had something to do with my namespaces.
But I get another error now, it says that I have more than one entry point defined in both 'hang1' and 'hang2', you can see it in the code I posted.
So I'm guessing I remove the entry point from hang2 in some way but I just get more errors when I do that. I think I might have 2 entry points because I created both forms seperately, I don't know... What am I doing wrong?...Thanks for your help :)
Was This Post Helpful? 0
  • +
  • -

#14 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: opening a window

Posted 08 July 2009 - 07:44 AM

Ah, they both have a method Main. Each program is only allowed to have 1 method Main, since that's where it all starts. So I suggest you remove it from the code from your second form, and then add the code we suggested earlier to an event handler of one of the buttons of the first form. Because the 1st form was supposed to open the 2nd form right?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1