Click here to Skip to main content
Sign Up to vote bad
good
See more: C#4.0
how to restrict a form to not open more then one at time.
.showDialogue method i know but tell me another way.
 
my code behind the button
form_2search sw=new form_2search();
sw.show();
sw.ismdiparent=this;
Posted 2 Jan '13 - 3:05
Edited 2 Jan '13 - 3:46


2 solutions

There are many approaches you could use. Singleton form is only one of them, but an elegant one: http://hashfactor.wordpress.com/2009/03/31/c-winforms-create-a-single-instance-form/[^]
 
[update]
 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
 
namespace SingletonForm
{
	public class SingletonForm : Form
	{
		#region Singleton pattern specific
		private static SingletonForm SingleInstance;
				
		public static SingletonForm Instance
		{
		    get 
		    {
		    	if(SingleInstance == null)
		    	{
		    		SingleInstance = new SingletonForm();
		    		SingleInstance.FormClosing += new FormClosingEventHandler(Singleton_FormClosing);
		    	}
		    	return SingleInstance;
			}
		}
		
		private static void Singleton_FormClosing(object sender, FormClosingEventArgs e)
		{
		    e.Cancel = true;
		    SingleInstance.Hide();
		}
		
		
		public new void Show()
		{
		    if (base.Visible)
		        base.Select();
		    else
		    {
		        base.Show();
		    }
		}
		
		#endregion
		
		#region Generated
		private System.ComponentModel.IContainer components = null;
		
		public SingletonForm()
		{
			InitializeComponent();
		}	
		
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		#endregion		
		
		private void InitializeComponent()
		{
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Text = "Child";
			this.Name = "Child";
		}
	}
	
	public class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		
		private System.ComponentModel.IContainer components = null;
 
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(59, 33);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(190, 47);
			this.button1.TabIndex = 0;
			this.button1.Text = "Start the other form";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.Button1Click);
			// 
			// MainForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(303, 116);
			this.Controls.Add(this.button1);
			this.Name = "MainForm";
			this.Text = "Main Form";
			this.ResumeLayout(false);
		}
		private System.Windows.Forms.Button button1;
		
		void Button1Click(object sender, System.EventArgs e)
		{
			SingletonForm.Instance.Show();
		}
	}
	
	public class Program
	{
		[STAThread]
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}		
	}
}
  Permalink  
Comments
Muhamad Faizan Khan - 16 Jan '13 - 23:23
such a big article it is. i wasw guessing that problem code will be consist on 3 or 4 lines but;-(
Zoltán Zörgő - 17 Jan '13 - 2:33
Big?! The article itself is short, but there are several comments. Try to read it before you complain :)
Muhamad Faizan Khan - 17 Jan '13 - 2:42
little bit complexity i am new to C#. just before 4 or 6 month i start to learn it. i am struggling;-(
Zoltán Zörgő - 17 Jan '13 - 5:53
See sample code in update.
Zoltán Zörgő - 17 Jan '13 - 16:24
Any progress?
Muhamad Faizan Khan - 19 Jan '13 - 23:37
seems tough...;-(
Zoltán Zörgő - 20 Jan '13 - 2:21
Don't understand. Sample code is working. What's the problem?
Hi,
 
Try this:
 
Form myform = null;
form_2search myform=null;
List<Form> frms = Application.OpenForms.OfType<Form>().ToList();
    foreach (Form f in frms)
    {
         string type = f.Name.ToString();
         if (type == "FormWebCam")
         {
 
              myform = (FormWebCam)f;
         }
    }
    if(myform!=null)
    {
        form_2search sw=new form_2search();
        sw.show();
        sw.ismdiparent=this;
    }
;
  Permalink  
Comments
Muhamad Faizan Khan - 21 Jan '13 - 1:46
i didnt understand your code;-( behind tool strip this code you tell me according to this;-) please Form2_Search_Word SearchWord = new Form2_Search_Word(); SearchWord.Show(); SearchWord.MdiParent = this;
milenalukic - 21 Jan '13 - 5:36
You need to use this code from where you have the code you submitted. Behind tool strip sounds good yes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 286
1 Mohammed Hameed 208
2 Sergey Alexandrovich Kryukov 188
3 Santhosh G_ 108
4 Mayur_Panchal 98
0 Sergey Alexandrovich Kryukov 8,216
1 OriginalGriff 6,271
2 CPallini 3,528
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid