如何在不创建新的表单实例的情况下在表单之间切换
本文关键字:表单 情况下 之间 实例 创建 | 更新日期: 2023-09-27 17:50:02
在我的应用程序中,我有四个表单 form1 form2 form3 form4 .每个表单都有两个按钮,即下一个和上一个按钮在表单之间切换.我的问题是如何在不创建新的表单实例的情况下在表单之间切换? 下面是我的代码
在窗体 1 中:
public Form1()
{
InitializeComponents();
}
private void Next_Click(object sender, EventArgs e)
{
this.Hide()
Form2 form2 = new Form2();
form2.Show();
}
在窗体 2 中:
public Form2()
{
InitializeComponents();
}
private void Previous_Click(object sender, EventArgs e)
{
this.Hide();
Form1 form1 = new Form1();
form1.Show();
}
private void Next_Click(object sender, EventArgs e)
{
this.Hide();
Form3 form3 = new Form3();
form3.Show();
}
在窗体 3 中:
public Form3()
{
InitializeComponents();
}
private void Previous_Click(object sender, EventArgs e)
{
this.Hide();
Form2 form2 = new Form2();
form2.Show();
}
private void Next_Click(object sender, EventArgs e)
{
this.Hide();
Form4 form4 = new Form4();
form4.Show();
}
在窗体 4 中:
public Form4()
{
InitializeComponents();
}
private void Previous_Click(object sender, EventArgs e)
{
this.Hide();
Form3 form3 = new Form3();
form3.Show();
}
在主要:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
在上面的代码中,我每次都在创建表单的新实例..,如何避免这种情况以及如何在不创建新的表单实例的情况下在表单之间切换.... 请帮助我
由于您是按顺序访问表单的,因此请确保使用 Show 方法将所有者分配给创建的表单,并在创建表单后将其分配给类级别变量。这样的东西应该适合你。
表格1
public partial class Form1 : Form
{
Form2 frm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (frm2 == null)
{
frm2 = new Form2(); //Create form if not created
frm2.FormClosed += frm2_FormClosed; //Add eventhandler to cleanup after form closes
}
frm2.Show(this); //Show Form assigning this form as the forms owner
Hide();
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
frm2 = null; //If form is closed make sure reference is set to null
Show();
}
}
表格2
public partial class Form2 : Form
{
Form3 frm3;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Owner.Show(); //Show the previous form
Hide();
}
private void button2_Click(object sender, EventArgs e)
{
if (frm3 == null)
{
frm3 = new Form3();
frm3.FormClosed += frm3_FormClosed;
}
frm3.Show(this);
Hide();
}
void frm3_FormClosed(object sender, FormClosedEventArgs e)
{
frm3 = null;
Show();
}
}
表格3
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Owner.Show();
Hide();
}
}
试试这个:
Form1 myForm =(Form1) Application.OpenForms["Form1"];
myForm.Show();
可能是一个简单的解决方案。您可以创建一个包含所需所有形式的静态对象的类。因此,您将能够从您选择的任何形式访问所有这些表单,好消息是它们被初始化一次。
public class formList
{
private static Form1 _form1 = new Form1();
public static Form1 form1 { get {return _form1;}
.................
............
}
您可以检查感兴趣的表单是否存在,如果没有,请创建它:
public static T OpenOrCreateForm<T>()
where T: Form, new() {
T result;
// Test if form exists
foreach(Form form in Application.OpenForms) {
result = form as T;
if (!Object.ReferenceEquals(null, result)) {
// Form found; and this is the right place
// to restore form size,
// bring form to front etc.
if (result.WindowState == FormWindowState.Minimized)
result.WindowState = FormWindowState.Normal;
result.BringToFront();
return result;
}
}
// Form doesn't exist, let's create it
result = new T();
// Probably, you want to show the created form
result.Show();
resturn result;
}
...
private void Previous_Click(object sender, EventArgs e)
{
Hide();
OpenOrCreateForm<Form1>();
}
private void Next_Click(object sender, EventArgs e)
{
Hide();
OpenOrCreateForm<Form3>();
}
public bool IsFormAlreadyOpen(Type FormType)
{
foreach (Form OpenForm in Application.OpenForms)
{
if (OpenForm.GetType() == FormType)
return true;
}
return false;
}
此函数可用于确定表单是否已打开
如果返回 true,则调用 IsFormAlreadyOpen(Form4)
,这意味着 Form4 已打开
在您的情况下
在 every forms constructor()
创建下一个和上一个表单对象
在按钮中单击调用IsFormAlreadyOpen((来找出表单是否已打开,如果已经打开,只需将该表单放在前面其他明智的显示表单使用 obj.show(( 方法进行,然后隐藏或关闭父窗体
看起来您正在尝试实现类似向导的功能。我建议使用单一表格。然后,向其添加自定义选项卡控件。将按钮添加到移动上一个和下一个的窗体。
若要自定义选项卡控件,您需要执行以下操作:
public class CustomWizard : TabControl
{
/// <summary>
/// This method traps windows message and hides other tabs in the tab control.
/// Message trapped: TCM_ADJUSTRECT
/// </summary>
/// <param name="m">Reference to Windows message</param>
protected override void WndProc(ref Message m)
{
// Second condition is to keep tab pages visible in design mode
if (m.Msg == 0x1328 && !DesignMode)
{
m.Result = (IntPtr)1;
}
else
{
base.WndProc(ref m);
}
}
/// <summary>
/// This method traps the press to stop users from selecting tab page via keyboard
/// </summary>
/// <param name="ke">Event details</param>
protected override void OnKeyDown(KeyEventArgs ke)
{
if (ke.Control && ke.KeyCode == Keys.Tab)
return;
base.OnKeyDown(ke);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}
此选项卡控件一次仅显示一个选项卡。虽然,在设计时,您可以看到它们。将此按钮和按钮添加到表单中。单击按钮时,只需设置此选项卡控件的 SelectedIndex
属性。
我遇到了同样的问题。我需要的应用程序,它有很多表单,我需要在表单之间向前和向后切换而不会丢失数据。我想出了以下解决方案,它对我有用。
在主(程序.cs(文件中,编写以下两个类:
static class Variables
{
public static DataSet Configurations = new DataSet();
public static float ExchangeRate = 0;
public static Dictionary<string, int> ItemsCategories = new Dictionary<string, int>();
public static Dictionary<string, float> WeightUnits = new Dictionary<string, float>();
}
static class Forms
{
public static Form2 F_Form2 = new Form2();
public static Form3 F_Form3 = new Form3();
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
第一个类用于可跨表单使用的全局变量。您可以通过以下方式访问任何变量:
Variables.ExchangeRate = 7.2; //(for ex).
第二个类是定义所有表单的新实例的地方。您可以通过以下方式隐藏 n 显示它们在所有表单中的任何位置:
Forms.F_Form3.Show();
或
Forms.F_Form2.Hide();
这对我来说很顺利,很完美。试试吧。
只需删除第一种形式的this.hide()
和第二种形式的[formNameHere].show();
即可。
喜欢这个:
表格 1:
public Form1()
{
InitializeComponents();
}
private void Next_Click(object sender, EventArgs e)
{
this.Hide()
Form2 form2 = new Form2();
form2.Show();
}
表格 2:
public Form2()
{
InitializeComponents();
}
private void Previous_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
this.Hide();
}
private void Next_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
this.Hide();
}
不擅长解释,真的是C#。但这应该有效。
//In Form1
private static Form1 i_Instance = null;
public static Form1 Instance
{
get
{
if (Form1.i_Instance == null) Form1.i_Instance = new Form1();
return Form1.i_Instance;
}
}
// And instantiation in other forms shall look like following
Form1 F1 = Form1.Instance;
F1.Show();