我有两种形式。在第一个表单中,我有按钮将我转发到第二种表单并使用 this.Hide(); 隐藏第一个表单。
看起来像这样:
Form1Streamer f1 = new Form1Streamer();
f1.Left = this.Left;
f1.Top = this.Top;
f1.Size = this.Size;
f1.Show();
this.Hide();
checkBox1.Checked = false;
它也占据了它的位置,但它与它无关。 在第二种形式中,我有一个按钮,单击后应该返回到隐藏形式并使其再次可见,但我找不到如何访问其属性的解决方案。 我有一些想法,但真的不知道如何标记它。任何帮助表示赞赏。
最佳答案
您需要将第一个表单的引用传递给第二个表单,以便调用第一个表单的任何方法。这是一个简单的示例来演示。
下面是我的第一个表单类
using System;
using System.Windows.Forms;
namespace Test_Desktop
{
public partial class FirstForm : Form
{
public FirstForm()
{
InitializeComponent();
}
private void showSecondFormButton_Click(object sender, EventArgs e)
{
SecondForm secondform = new SecondForm(this); //Passing the reference of current form i.e. first form
secondform.Show();
this.Hide();
}
}
}
这是我的第二级类(class)
using System;
using System.Windows.Forms;
namespace Test_Desktop
{
public partial class SecondForm : Form
{
private FirstForm firstForm = null;
public SecondForm()
{
InitializeComponent();
}
///
/// Overriding constructor
///
public SecondForm(FirstForm firstForm)
{
InitializeComponent();
this.firstForm = firstForm;
}
private void showFirstFormButton_Click(object sender, EventArgs e)
{
if(firstForm!=null)
{
firstForm.Show();
//
//Do some processing
//
this.Dispose();
}
}
}
}
关于C# 在两个窗口之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694684/