本文介绍了限制表单不能打开多个时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将表单限制为不能多次打开。
.showDialogue方法我知道但告诉我另一种方式。
我的代码在按钮后面
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;
推荐答案
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());
}
}
}
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;
}
;
这篇关于限制表单不能打开多个时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!