Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

showing a form

Status
Not open for further replies.

traxxas

Programmer
Dec 11, 2003
15
US
i posted this a couple of days ago and it didnt get much reponses, so how do i go about showing a secdond form in my project, its called form2.h, im using visual C++ .NET 2003 and its a windows program NOT a console program

also what exactly is an instance i heard it around alot but never knew what it was

thanks for your help guys
 
I'm not sure how to make a second form, but have you considered just using a dialog instead of an entirly new form? I guess it depends on what you want to do; I'm just making sure you know about the simple way of doing it.

-Bones
 
no i want this form to have alot of stuff on it like textbox's, images and the works, and i dont need to MAKE a new form i just need to know how to make it show up when i clikc the button on the main form
 
MyForm1* frm = new MyForm1();
frm->Show();

Ion Filipski
1c.bmp
 
compile error

error C2065: 'Form2' : undeclared identifier
error C2065: 'frm' : undeclared identifier
error C2061: syntax error : identifier 'Form2'
error C2227: left of '->Show' must point to class/struct/union

heres the code i put in:
Form2* frm = new Form2();
frm->Show();

the name of the form file is form2(lowercase f), the accessible name is Form2(capital F)

im using Visual C++ .net 2003

all help appreciated, thanks
 
The file names are not case sensitive and the name of the form has nothing to do with the name of the form.
A .cs file could contains many forms defined and more that one classes but it is not recomended.
I think you have problem with the namespace.
I give you here an example like a "pattern" that you could use. Here is an example of a mainform (MainForm.cs file) and a LogonForm (LogonForm.cs) which is called from the main form when a menu item is clicked.
Take a look to:
-what to using ... in each file,
-how is declared the namespece in each file
-how the main form "knows" the second form . Note that is a way for the second form to knows the main form but is not shown in this code.
-how the MainForm class is instantiated in the Main() function .e.g. create an instance (object) of the MainForm class but not assigned to any variable.
-how the LogonForm is INSTANTIATED in the constructor of the main form e.g. create an object of a given type in this case of the LogonForm and store it in the member variable of the main form.
-how the LogonForm is shown using two ways: ShowDialog(), Show()
Code:
// MainForm.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace YourNameSpace

         /// <summary>
	/// Summary description for MDI.
	/// </summary>
public class MainForm : System.Windows.Forms.Form
{

	// other members
	private LogonForm m_LogonForm;
	private System.Windows.Forms.MenuItem m_MnuConnect; //menu connect
        public MainForm()
	{
		InitializeComponent();
		m_LogonForm= new LogonForm();

	}
        private void InitializeComponent()
        {
           // initialization code
        }
        private void m_MnuConnect_Click(object sender, System.EventArgs e)
	{
		//...
		this.m_LogonForm.ShowDialog(); // as modal 
                // or 
                // this.m_LogonForm.Show();
        }

	static void Main() 
       {
         Application.Run(new MainForm());
       }


} 

// LogonForm.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;


namespace YourNameSpace
{
	/// <summary>
	/// Summary description for LogonForm.
	/// </summary>
	public class LogonForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox m_TxtPassword;
		private System.Windows.Forms.Label m_LblPassword;
		private System.Windows.Forms.Label m_LblUserID;
        }
       //...
}

static void Main() 
{
Application.Run(new MainForm());
}
-obislavu-
 
Hi,
Please ignore my previous reply ! I was thinking you are using C#.NET !
The answer of Ion is correct but I think the errors you get are generated by the way you include the .h files.
Do you use precompiled headers ?
-obislavu-

 
what do you mean by pre compiled header files, they are generated when i use the wizard that adds a window form, its called form2.h, do i have to put a #include in? or somthin, im really havin a hard time with this
 
you should #include&quot;form2.h&quot; in the file you use the form2.

Ion Filipski
1c.bmp
 
that works great, i also figured out how to hide the form, but what if i dont want to create a new form every time, what if i want to keep bringing up the existing one with all its user input (text in textboxes, etc...) how would i do that?
 
keep the pointer as a variable member of the first class (ie form). After that you could use it as many times as yiou whish.

Ion Filipski
1c.bmp
 
what exactly is a variable member, im alittle new to C++, migrated over from VB could i get a sample code?, thanks alot!
 
find this piece of code:
Form2* frm = new Form2();

put &quot;Form2* frm&quot; somewhere inside the class declaration ant change the line above to:

frm = new Form2();

you chould put this piece of variable initialization, for example, somewhere in the constructor. After that show or hide the form as many times as you whish.

Ion Filipski
1c.bmp
 
what and were exactly is the constructor and the class declaration so i can find it and implement this code, again thanks for your help

is the class declaration this?:

private: System::Void button11_Click(System::Object * sender, System::EventArgs * e)
{
//code goes here
}

and i have no idea what the constructor is
 
In my opinion you have a little to study C++ before asking such questions.

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top