gpt4 book ai didi

c# - c# 多窗口窗体切换

转载 作者:可可西里 更新时间:2023-11-01 12:02:05 26 4
gpt4 key购买 nike

我有 10 个表单,每个表单都有下一个和上一个按钮。显示和关闭表单的最佳方法是什么。我不需要表格来留在内存中。有什么想法吗?

最佳答案

我会使用类似于 FormSwitcher 的东西:

public class FormSwitcher
{
private List<Func<Form>> constructors;
private int currentConstructor = 1;

public FormSwitcher(Func<Form> firstForm)
{
constructors = new List<Func<Form>>();
AddForm(firstForm);
}

public void AddForm(Func<Form> constructor)
{
constructors.Add(constructor);
}

public Form GetNextForm()
{
if (constructors.Count > 0 && currentConstructor >= constructors.Count)
{
currentConstructor = 0;
}
if (constructors.Count > currentConstructor)
{
return constructors[currentConstructor++]();
}
return null;
}
}

您的主窗体应如下所示:

public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
new FirstForm().Show();
}
}

旋转形式可能如下所示:

public partial class FirstForm : Form
{
private FormSwitcher switcher;

public FirstForm()
{
InitializeComponent();
switcher = new FormSwitcher(() => new FirstForm());
switcher.AddForm(() => new SecondForm(switcher));
switcher.AddForm(() => new ThirdForm(switcher));
}

private void button1_Click(object sender, EventArgs e)
{
switcher.GetNextForm().Show();
Close();
}
}

public partial class SecondForm : Form
{
private FormSwitcher switcher;

public SecondForm(FormSwitcher switcher)
{
InitializeComponent();
this.switcher = switcher;
}

private void button1_Click(object sender, EventArgs e)
{
switcher.GetNextForm().Show();
Close();
}
}

public partial class ThirdForm : Form
{
private FormSwitcher switcher;

public ThirdForm(FormSwitcher switcher)
{
InitializeComponent();
this.switcher = switcher;
}

private void button1_Click(object sender, EventArgs e)
{
switcher.GetNextForm().Show();
Close();
}
}

关于c# - c# 多窗口窗体切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8839577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com