|
| |||||||||
| Tags: component class, database, form, net versions, sql server 2005, visual studio, window service |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| Can't open a different form in C#
I am having problem with C# while opening different form. I originally started it as a 'window form' but but somehow it 'converted' itself into another type of form. I have already worked on a C# application that works with a database. Now that form is displaying itself as an icon that could be for a 'Component class' or 'window service' etc. I have added a button in Form1 after creating a new window form, so that user can navigate themselves to Form2 via a button click from the Form1. Also now I can't make a link between them for the users to be able to navigate from one form to another because the two forms seem to be of different 'type'. Unknowingly Form1 had turned into another type and that's why I am not able to open Form2 from Form1. Please help me to sort out this problem..!!
__________________ ASUS P5VD1-X Intel Pentium Dual Core 3.00GHz Maxtor 160GB Corsair 1.5GB PC3200 RAM Nvidia Geforce 6800 GT 256mb Phillips DVD RW Magna 500W PSU XION II Steel Black Gaming Case |
|
#2
| ||||
| ||||
| Re: Can't open a different form in C#
I have tried to provide some tips regarding your problem. I think that the following steps may help you in opening a different form in C#. Have you tried to have the Form1 class which derives from type Form. Then also try to have a Form2 class which derives from type Form. After having the both forms derived from the type Form, create a button on Form1, which on clicking will show you the Form2 form. In the code handler of OnClick, when adding an OnClick event handler to Button1 you should write the code : Form2 newForm2 = new Form2(); newForm2.Show(); //Or newForm2.ShowDialog() It depends on the accomplishment that you are trying to do. Hope that this will help you to open a different form. |
|
#3
| ||||
| ||||
| Re: Can't open a different form in C#
For opening the different form in C#, you will have to create two forms and insert the following codes in their place. The coding is as follows : //to create as variable form1 in class, write private Form2 refF2; //then you will have to call the Constructor public Form1() { InitializeComponent(); refF2 = new Form2 (); refF2.refF1 = this; } [STAThread] static void Main() { Application.Run(new Form1()); } //button click on first form (form1) private void button1_Click(object sender, System.EventArgs e) { this.Hide(); refF2.Show (); } //for creating variable form2 in class public Form1 refF1; //button click on second form (form2) private void button1_Click(object sender, System.EventArgs e) { this.Hide(); refF1.Show(); } By doing this coding you should able to open the another form in C#. |
|
#4
| ||||
| ||||
| Re: Can't open a different form in C#
You have not mentioned where are you coding.!! You can use the version of the "Object Factory" pattern. If you are coding with C# in Visual Studio 2005 for a database application residing on remote MS SQL Server 2005. A static method for creating instances is contained in the Class, which keeps track of whether an instance has already been created which in return the existing instance. I think that you can use Show() : class Form1 : Form { private Form1():base{} private static Form1 instance = null; public static Form1 ShowTheForm() { if (instance==null) instance=new MyForm(); instance.Show(); return instance; } } The only thing is that you will have to invoke the MyForm.ShowTheForm(), every time you want to open it.
__________________ The FIFA Manager 2009 PC Game |
|
#5
| ||||
| ||||
| Re: Can't open a different form in C#
According to me you can apply the Singleton design pattern so that there is only ever one instance of the Form at any given time. The coding for that should be as follows : Code: public class SingletonForm : System.Windows.Forms.Form
{
private static SingletonForm _instance = null;
private SingletonForm()
{
... put your usual constructor stuff here ...
}
public SingletonForm Instance
{
get
{
if (SingletonForm._instance == null)
{
SingletonForm._instance = new SingletonForm();
}
return SingletonForm._instance;
}
}
} |
|
#6
| |||
| |||
| Re: Can't open a different form in C#
I think that the coding suggested by the 'Modifier' will work for your problem. I would like to suggest you to switch over the other language, because of the following reasons :
|
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Can't open a different form in C#" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Google Chrome is opening in basic HTML form instead of the Standard form | Beatrix | Technology & Internet | 5 | 25-05-2011 11:21 PM |
| how to get items in a form to auto-resize with the form using visual basic | deansmylie93 | Software Development | 2 | 02-05-2011 08:06 AM |
| How to Auto Populate fields from sub form to form? | mich43 | Windows Software | 6 | 09-11-2010 11:29 PM |
| Procedure in VB to create form inside another form | Jalabala | Software Development | 3 | 16-11-2009 02:14 PM |
| Problem reloading MDI child form in main form. | AFFAN | Software Development | 3 | 30-01-2009 09:05 PM |