void Start(object sender, System.EventArgs e)
{
}
opening a window
Page 1 of 113 Replies - 789 Views - Last Post: 08 July 2009 - 07:44 AM
#1
opening a window
Posted 07 July 2009 - 01:39 AM
Replies To: opening a window
#2
Re: opening a window
Posted 07 July 2009 - 01:59 AM
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
#3
Re: opening a window
Posted 07 July 2009 - 02:07 AM
#4
Re: opening a window
Posted 07 July 2009 - 02:19 AM
#5
Re: opening a window
Posted 07 July 2009 - 03:52 AM
Renagado, on 7 Jul, 2009 - 01:19 AM, said:
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
#6
Re: opening a window
Posted 07 July 2009 - 04:46 AM
StartGame sg = new StartGame(); sg.Show();
#7
Re: opening a window
Posted 07 July 2009 - 06:54 AM
b.ihde, on 7 Jul, 2009 - 03:46 AM, said:
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.
#8
Re: opening a window
Posted 07 July 2009 - 07:18 AM
Form2 oldForm2 = (Form2)Application.OpenForms["Form2"];
#9
Re: opening a window
Posted 07 July 2009 - 08:31 AM
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?
#10
Re: opening a window
Posted 07 July 2009 - 10:25 AM
eclipsed4utoo, on 7 Jul, 2009 - 02:18 PM, said:
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
#11
Re: opening a window
Posted 08 July 2009 - 01:39 AM
Renagado, on 7 Jul, 2009 - 07:31 AM, said:
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!
#12
Re: opening a window
Posted 08 July 2009 - 06:42 AM
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
#13
Re: opening a window
Posted 08 July 2009 - 07:35 AM
Renagado, on 8 Jul, 2009 - 05:42 AM, said:
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
#14
Re: opening a window
Posted 08 July 2009 - 07:44 AM
|
|

New Topic/Question
Reply




MultiQuote





|