Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:05am
Edited 2-Jan-13 3:46am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
Muhamad Faizan Khan at 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ő at 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 at 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ő at 17-Jan-13 5:53am
   
See sample code in update.
Zoltán Zörgő at 17-Jan-13 16:24pm
   
Any progress?
Muhamad Faizan Khan at 19-Jan-13 23:37pm
   
seems tough...;-(
Zoltán Zörgő at 20-Jan-13 2:21am
   
Don't understand. Sample code is working. What's the problem?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 at 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 at 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)



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