Results 1 to 6 of 6

Thread: Can't open a different form in C#

  1. #1
    Join Date
    Jul 2006
    Posts
    191

    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. #2
    Join Date
    Feb 2008
    Posts
    1,852

    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. #3
    Join Date
    Jan 2008
    Posts
    1,521

    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. #4
    Join Date
    May 2008
    Posts
    2,389

    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.

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    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. #6
    Join Date
    May 2008
    Posts
    2,012

    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 :
    • C/C++ is a much better choice when compared to the C#.
    • C# is slower than VB, since it goes through 2 dlls and the C only passes through 1.
    • The latest .NET versions are not supported
    • Different tools between Mono and Visual Studio

    If you are already stuck into C#, then you can continue.

Similar Threads

  1. Replies: 5
    Last Post: 25-05-2011, 10:21 PM
  2. Replies: 2
    Last Post: 02-05-2011, 07:06 AM
  3. How to Auto Populate fields from sub form to form?
    By mich43 in forum Windows Software
    Replies: 6
    Last Post: 09-11-2010, 11:29 PM
  4. Procedure in VB to create form inside another form
    By Jalabala in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 02:14 PM
  5. Problem reloading MDI child form in main form.
    By AFFAN in forum Software Development
    Replies: 3
    Last Post: 30-01-2009, 09:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,179,696.37617 seconds with 17 queries