Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
form_2search sw=new form_2search();
sw.show();
sw.ismdiparent=this;
Posted
Updated 2-Jan-13 3:46am
v2

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]

C#
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());
		}		
	}
}
 
Share this answer
 
v2
Comments
Muhamad Faizan Khan 16-Jan-13 23:23pm    
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:33am    
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:42am    
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:53am    
See sample code in update.
Zoltán Zörgő 17-Jan-13 16:24pm    
Any progress?
Hi,

Try this:

C#
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;
    }
;
 
Share this answer
 
Comments
Muhamad Faizan Khan 21-Jan-13 1:46am    
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:36am    
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900