Running a a form from a form

And second: (I cut out the Form designer part to fit it here)

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.RichTextBox richTextBox1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.PictureBox pictureBox3;
		private System.Windows.Forms.PictureBox pictureBox4;
		private System.Windows.Forms.PictureBox pictureBox5;
		private System.Windows.Forms.TextBox x;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1() //constructor
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		
		static void Main() 
		{
			Form3 frm3=new Form3();
			frm3.Show();
			Application.Run();
		}
		
		private void Form1_Closed(object sender, System.EventArgs e)
		{
			Application.Exit();
		}
		
		public float cube(float s){return s*s*s;}

		private void pictureBox1_Click(object sender, System.EventArgs e)
		{
			float side; side=float.Parse(x.Text);
			MessageBox.Show("Volume of cube with side: "+ side+ " is: "+ cube(side));

			x.Text="YOU GOT IT!!!!";
		}
		
		private void richTextBox1_TextChanged(object sender, System.EventArgs e)
		{

		}
		
		private void Form1_Load(object sender, System.EventArgs e)
		{

		}

		private void x_TextChanged(object sender, System.EventArgs e)
		{
		
		}

	}
}
 
Well shoot - you have to tell us it still wasn't working. I thought you had posted what you had after you got it working :)

Anyway, it looks like your Form1's Closed event isn't hooked up.

In Form1 in the InitializeComponent sub (the one you didn't include :)), you need the line:
C#:
this.Closed += new System.EventHandler(this.Form1_Closed);

This is what actually hooks up the event to your function.

-nerseus
 
I will give it a try and be SURE to post back if it does not work.

Thank you so much for your help. I know I will need it in the future. Probably near future.
 
static void Main()
{
Application.Run();
Form3 frm3=new Form3();
frm3.Show();
}

Why doesn't that work? The program runs but nothing shows up.
 
It worked! I have another question.

Does Application.Run(); only work in Main?
anywhere else I put it the program just immediately shut down.
 
Yes; Main is the only sub that executes on its own (on the starting
of the app). The only way to get any other subs to run is to call them
(directly or indirectly) through the Sub Main (In C#... in VB.NET, you
don't even need an Application.Run statement for the program to
start).
 
I tried removing Application.Run here but the program immediately closed.

static void Main()
{
Form3 frm3=new Form3();
frm3.Show();
Application.Run();
}
 
Is that sub in the same form that is set as your startup object?
Right click on the project in the solution explorer and click Properties.
There will be a "Startup Object" setting. Make sure it is set to the
same form that the Sub Main is located in.
 
Yes, but there should be two selections available:
"WindowsApplication2.Form1" and "WindowsApplication2.Form2"

If Sub Main is in Form1, you want the startup object to be the first one,
otherwise you want it to be the second one.
 
No, you're looking at the solution properties. You want the project properties. Right-click on your project node, not the solution node, and click properties.
 
I found it and changed the starting object to form1 (it was my only choice.) but it still does the same thing when I remove Run.
 
Oh, when you remove the Run command? You need the Run
command in the Sub Main of your startup object; sorry, I misread your
original statement.

If you take out the Run statement, then your program is never really
executing; it's simply starting the program, and closing it immediately
because there's nothing to do. When you use the Run command,
it starts a standard Windows message loop for the window, meaning
that the window becomes active, and recieves events and various
window messages that are sent to it. Without a message loop and
a thread to run it on, there is no program.
 
VolteFace said:
(In C#... in VB.NET, you
don't even need an Application.Run statement for the program to
start).

I tried removing that and also just writing Run(); but it never works without

Application.Run();
Is that the way it should be?

Also, I noticed in the properties window something about having more than one Main(). Is that possible?
 
I meant that in C# you do need it, but in VB.NET you don't.
Yes, Application.Run(); is correct. Sorry for being unclear.
 
Back
Top