C# 从子窗体导航到其同级窗体
在父窗体a中,有以下代码可调用子窗体“B”: 在相同的表单中:调用子表单“C”的代码如下: 现在我想在表单B中创建一个按钮,这样,如果我单击该按钮,它就会自动导航到表单C 如果可能的话,应避免在形式B中使用形式C的引用对象,如答案中所示。这是因为有十多种形式,如B,C。。。他们每个人都必须能够导航到另一个。在一个表单中有10个表单引用对象是不好的C# 从子窗体导航到其同级窗体,c#,winforms,xaml,C#,Winforms,Xaml,在父窗体a中,有以下代码可调用子窗体“B”: 在相同的表单中:调用子表单“C”的代码如下: 现在我想在表单B中创建一个按钮,这样,如果我单击该按钮,它就会自动导航到表单C 如果可能的话,应避免在形式B中使用形式C的引用对象,如答案中所示。这是因为有十多种形式,如B,C。。。他们每个人都必须能够导航到另一个。在一个表单中有10个表单引用对象是不好的 我想一定有办法达到这个效果。有人知道吗?尝试在frmChildB中创建一个事件,并在父级订阅它。然后,无需在frmChildB中引用frmChildC
我想一定有办法达到这个效果。有人知道吗?尝试在frmChildB中创建一个事件,并在父级订阅它。然后,无需在frmChildB中引用frmChildC即可执行所需操作
看看这个MSDN 这是非常粗糙的,但应该给你一个想法 在子窗体中创建事件
public delegate void SwapEventHandler(object sender, EventArgs e);
public partial class Form2 : Form
{
public event SwapEventHandler Swap;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Swap(sender, e);
}
}
在父窗体中使用它
private void button1_Click(object sender, EventArgs e)
{
frmChildB = new Form2();
frmChildB.Name = "frmChildB";
frmChildB.Swap += new SwapEventHandler(frmChildB_Swap);
frmChildB.ShowDialog();
}
private void frmChildB_Swap(object sender, EventArgs e)
{
frmChildB.Swap -= new SwapEventHandler(frmChildB_Swap);
frmChildB.Close();
frmChildB.Dispose();
frmChildB = null;
frmChildC = new Form2();
frmChildC.Name = "frmChildC";
frmChildC.Swap += new SwapEventHandler(frmChildC_Swap);
frmChildC.ShowDialog();
}
void frmChildC_Swap(object sender, EventArgs e)
{
frmChildC.Swap -= new SwapEventHandler(frmChildC_Swap);
frmChildC.Close();
frmChildC.Dispose();
frmChildC = null;
frmChildB = new Form2();
frmChildB.Name = "frmChildC";
frmChildB.Swap += new SwapEventHandler(frmChildB_Swap);
frmChildB.ShowDialog();
}
如果我正确理解了您的问题,您希望每个表单都有一个实例,并在它们之间来回导航 如果这是您想要的,您可以实现一个静态FormManager类,该类创建表单实例并根据需要显示它们。您甚至可以使用枚举来进一步降低复杂性 下面是这门课的一个例子(它需要一些额外的工作,但应该会给你一个好主意): 这可以从子窗体调用,如下所示:
FormManager.ShowForm(FormManager.FormId.FormB);
在原始级别上,使用标准的“向导”模式似乎比为每个问题使用单独的表单更有益。例外情况是,你不应该只使用典型的“下一步”和“上一步”按钮,而应该使用跳转到任何问题的按钮。这将引导您完成创建向导的正常步骤。
表单。ShowDialog
显示一个模式对话框,您发布的代码中似乎存在问题。如果FormB
是以模式显示的,那么在显示FormC
之前,您需要先将其停用,那么为什么不使用Form。Show
?@M.Babcock:这是因为应用程序不允许用户在不关闭子窗体的情况下返回主窗体。换句话说,用户一次只能处理一个表单。那个么为什么你们要使用不同的表单呢?框架或用户控件不是更合适吗?@M.Babcock:我对用户控件和框架的组合不太了解。本周早些时候有一个问题基本上也是这样问的。不幸的是,我现在找不到它的参考文献。听起来你要找的更像是一个“向导”(想想InstallShield),它有步骤和工作流。我接近你想要的了吗?如果这是一个新手问题,我很抱歉,但我还不太明白你的答案。你介意给我指一些参考资料吗,比如教程或示例源代码?这种方式很好。但当我想关闭一个子窗体,然后重新打开它时,它会大叫:“无法设置可见性或在窗口关闭后调用Show或ShowDialog”我不太明白为什么这会导致错误,但我需要关闭子窗体以返回主窗口。
private void button1_Click(object sender, EventArgs e)
{
frmChildB = new Form2();
frmChildB.Name = "frmChildB";
frmChildB.Swap += new SwapEventHandler(frmChildB_Swap);
frmChildB.ShowDialog();
}
private void frmChildB_Swap(object sender, EventArgs e)
{
frmChildB.Swap -= new SwapEventHandler(frmChildB_Swap);
frmChildB.Close();
frmChildB.Dispose();
frmChildB = null;
frmChildC = new Form2();
frmChildC.Name = "frmChildC";
frmChildC.Swap += new SwapEventHandler(frmChildC_Swap);
frmChildC.ShowDialog();
}
void frmChildC_Swap(object sender, EventArgs e)
{
frmChildC.Swap -= new SwapEventHandler(frmChildC_Swap);
frmChildC.Close();
frmChildC.Dispose();
frmChildC = null;
frmChildB = new Form2();
frmChildB.Name = "frmChildC";
frmChildB.Swap += new SwapEventHandler(frmChildB_Swap);
frmChildB.ShowDialog();
}
public class FormManager
{
private static FormB m_FormB;
public static FormB formB
{
get
{
if (m_FormB == null)
{
m_FormB = new FormB();
}
return m_FormB;
}
}
private static FormC m_FormC;
puClic static FormC formC
{
get
{
if (m_FormC == null)
{
m_FormC = new FormC();
}
return m_FormC;
}
}
public enum FormId
{
FormB,
FormC
}
public static Form ShowForm(FormId whichForm)
{
Form oForm;
switch (whichForm)
{
case FormId.FormB:
oForm = FormManager.formB;
break;
case FormId.FormC:
oForm = FormManager.formC;
break;
default:
oForm = null;
break;
}
if (oForm != null)
{
oForm.ShowDialog();
}
return oForm;
}
}
FormManager.ShowForm(FormManager.FormId.FormB);