在c中的多个窗体窗体之间切换#
我会为每个窗口使用一个单独的引用,如果您不再需要它,您可以将其设置为null。当没有任何引用指向对象时,垃圾回收器会在一段时间后调用它的析构函数。
如果我明白了,你有很多窗口,比如说n个窗口,这些窗口在一段时间内完成了他们的工作,然后不再需要。所以,当你写
Window myWindow_1 = new Windowd();
Window myWindow_2 = new Windowd();
Window myWindow_3 = new Windowd();
// ...
Window myWindow_n = new Windowd();
如果你希望他们留下记忆,你可以简单地做
myWindow1 = null;
myWindow2 = null;
myWindow3 = null;
//...
myWindow4 = null;
如果这些是用来链接到窗口对象,他们将只引用在内存中仍然是不可引用的幽灵,垃圾收集器会在一段时间后将其删除。
为了简单起见,您可以将所有这些引用放在数组中以避免为每个对象命名。对于exapmle:
Window[] myWindows = new Window[n];
for (int i=0; i<n; i++) {
myWindows[i] = new Window();
}
希望这有助于:)
代码示例将真的帮助...谢谢 – McDee 2012-01-12 17:56:02
我会利用类似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();
}
}
好吧将尝试此...名称空间列表
List 来自System.Collections.Generic,Func 来自System – 2012-01-12 18:35:13
不要这样做。 有一个窗体,十个用户控件,最好是实现一个接口的窗体。
从表格中尽可能多的代码,一切都很好... 现在点击抽象按钮,否则你的下一个版本将有形式3检查表单1,看看你是否可以在边缘情况634返回时跳过表格2,如果你从1跳转到1,则可以跳过2,除非他们勾选了框14并在框8中放置了“Fred”。
这不是你想穿的Teeshirt。
我会使用除表格以外的东西。为什么不更改UI以使用多个面板(或类似的东西)使用一个窗体? – webdad3 2012-01-12 17:38:16
然后很难阅读代码。这是一个巨大的项目。 – McDee 2012-01-12 17:40:41
@McDee - 你在这里描述的概念通常被称为'向导',它们有几个很好的教程可用来描述正确的'向导'实现。这里是一个:http://geekswithblogs.net/jannikanker/archive/2005/03/14/55193.aspx – 2012-01-12 17:41:30