Author Topic: Switching between forms in an exe  (Read 4272 times)

0 Members and 1 Guest are viewing this topic.

jjs

  • Guest
Switching between forms in an exe
« on: November 04, 2008, 04:17:39 PM »
I have tried to follow this, but it does not seem to work in my app.
http://articles.techrepublic.com.com/5100-10878_11-1050650.html

It either says my variable does not exist in the current context, or something else.
Anybody have this licked?

Glenn R

  • Guest
Re: Switching between forms in an exe
« Reply #1 on: November 04, 2008, 04:30:08 PM »
You will get a lot more help with your problem if people do not have to guess what your code does, but rather test/play with the sample you provide.

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Switching between forms in an exe
« Reply #2 on: November 04, 2008, 04:39:05 PM »
Jeremie - did you put the exact code in to start off with or have you modified it to suit yet?

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Switching between forms in an exe
« Reply #3 on: November 04, 2008, 04:41:02 PM »
It...says my variable does not exist...or something else.
That's....uhhh... vague enough to be useless.


I'll second what Glenn said.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

jjs

  • Guest
Re: Switching between forms in an exe
« Reply #4 on: November 04, 2008, 04:47:34 PM »
I want to do exactly what the example I linked to does. I want to have two forms and be able to switch back and forth between the two forms progmatically.
I have FORM1 that has a BUTTON1 that opens FORM2. If the person hits BUTTON1 a second time it should set the focus to FORM2 if it has not been closed. If FORM2 does not exist, it opens a new instance of FORM2. I should never have more than 1 instance of any form open.
I don't have any code that works yet, because I have not figured out how to do it.

All the code I have tried from my books makes a new instance of the form each time. Then I tried this web example and it does not work. When I open a form class, it says public partial class Form1 : Form
The example from the website does not say partial. So I don't know if that was an earlier version of visual studio or what. I am using VS 2005.

 

Glenn R

  • Guest
Re: Switching between forms in an exe
« Reply #5 on: November 04, 2008, 04:48:19 PM »
I like your avatar Matt...spat my drink over the keyboard!!!  :-D

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Switching between forms in an exe
« Reply #6 on: November 04, 2008, 04:52:31 PM »
I like your avatar Matt...spat my drink over the keyboard!!!  :-D
Here... have a copy!
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Glenn R

  • Guest
Re: Switching between forms in an exe
« Reply #7 on: November 04, 2008, 05:08:32 PM »
I don't have any code that works yet, because I have not figured out how to do it.

This is why I said what I did in my original post. If you want to learn in the context of how you interpret examples and write your code, you need to supply a sample for people to:

A: See what you have tried and
B: To offer guidance in what you will comprehend, in relation to what you have already tried

I hope that makes sense.

Glenn R

  • Guest
Re: Switching between forms in an exe
« Reply #8 on: November 04, 2008, 05:09:23 PM »
Ta Matt.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Switching between forms in an exe
« Reply #9 on: November 04, 2008, 09:22:42 PM »
<<<<
The example from the website does not say partial. So I don't know if that was an earlier version of visual studio or what. I am using VS 2005.
 

The partial modifier for classes do not affect the functionality. It was included in VS 2005. The biggest advantage of partial classes is that you can split YOUR code from AutoGenerated code and not have one trample over the other.

... and great for separating business logic from interface

added,
partial methods are available for VS2008, but I'm not sure about availability in net 2.0

« Last Edit: November 04, 2008, 09:30:40 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TonyT

  • Guest
Re: Switching between forms in an exe
« Reply #10 on: November 04, 2008, 09:57:36 PM »
I want to do exactly what the example I linked to does. I want to have two forms and be able to switch back and forth between the two forms progmatically.
I have FORM1 that has a BUTTON1 that opens FORM2. If the person hits BUTTON1 a second time it should set the focus to FORM2 if it has not been closed. If FORM2 does not exist, it opens a new instance of FORM2. I should never have more than 1 instance of any form open.
I don't have any code that works yet, because I have not figured out how to do it.

All the code I have tried from my books makes a new instance of the form each time. Then I tried this web example and it does not work. When I open a form class, it says public partial class Form1 : Form
The example from the website does not say partial. So I don't know if that was an earlier version of visual studio or what. I am using VS 2005.

 

It may help if you goggled and researched the 'Singleton pattern', because that's essentially what you need.

Here's a basic example of a singleton form:

Code: [Select]

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 SingletonFormExample
{
   public partial class MySingletonForm : Form
   {
      // Make constructor 'protected' because the single     
      // instance of this form should only be created internally.

      protected MySingletonForm()
      {
         InitializeComponent();
      }

      // A private static variable to hold the singleton instance:

      private static MySingletonForm instance = null;

      // A public static property that is used to access
      // the single instance of the form. If the instance
      // has not been created yet, this will create it.
      //
      // If the instance was created and subsequently
      // disposed, this will recreate it.

      public static MySingletonForm Instance
      {
         get
         {
            if( instance == null || instance.IsDisposed )
               instance = new MySingletonForm();
            return instance;
         }
      }

      // This is necessary if the form is shown modelessly,
      // to prevent it from disposing itself when it is closed.     

      protected override void OnFormClosing( FormClosingEventArgs e )
      {
         base.OnFormClosing( e );
         if( ! this.Modal )
         {
            e.Cancel = true;
            this.Visible = false;
         }
      }

   }

   // Example usage:

   public class Test
   {
      public static void ShowModelessExample()
      {
         // Show the singleton form modelessly:

         if( ! MySingletonForm.Instance.Visible )
            MySingletonForm.Instance.Show();

      }

      public static void ShowModalExample()
      {
         // Show the singleton form modally:

         MySingletonForm.Instance.ShowDialog();
      }
   }
}


« Last Edit: November 05, 2008, 04:13:05 AM by TonyT »

jjs

  • Guest
Re: Switching between forms in an exe
« Reply #11 on: November 05, 2008, 03:38:41 PM »
Thanks,
I will try the singleton form stuff. I will let you know how it goes.

jjs

  • Guest
Re: Switching between forms in an exe
« Reply #12 on: November 05, 2008, 04:07:37 PM »
Tony,
I tried your example code, but it did not quite work. So I searched for singleton forms and came up with http://www.codeproject.com/KB/architecture/singletonforms.aspx this example. I implemented it into my program. It is working now. Thanks for the help.
I had done searches for single instance of a form, but nothing was coming up. Once I used singleton, then I found lots of good stuff.
Thanks.

jjs

  • Guest
Re: Switching between forms in an exe
« Reply #13 on: November 05, 2008, 04:13:42 PM »
The partial modifier for classes do not affect the functionality. It was included in VS 2005. The biggest advantage of partial classes is that you can split YOUR code from AutoGenerated code and not have one trample over the other.

... and great for separating business logic from interface

added,
partial methods are available for VS2008, but I'm not sure about availability in net 2.0


thanks Kerry.

TonyT

  • Guest
Re: Switching between forms in an exe
« Reply #14 on: November 06, 2008, 02:46:35 AM »
Tony,
I tried your example code, but it did not quite work.

The example I posted is used routinely in my own code, and
works just fine here, so if it didn't work for you, it had to be
a result of a mistake in your implementation.