我有两种形式连接到数据库,LoadDocument
表和Fom1
这是主要形式。在LoadDocument
中,我从我的数据库中获取文档名称,当关闭LoadDocument
时,我将文档ID发送到Form1
,以便我可以在其中检索其内容。我只能从表单调用静态函数到另一个表单吗?
唯一的问题是,如果我在Form1
进行函数,称为public void showContent()
我LoadDocument
不能把它的,因为它不是静态的,如果我让静态的,我得到创建radioButtons
问题。
public partial class Form1 : Form
{
public void showTasks()
{
radioButtons = new RadioButton[numberOfTasks];
for (int i = 0; i < numberOfTasks; ++i)
{
radioButtons[i] = new RadioButton();
radioButtons[i].Text = "Task " + (i+1);
radioButtons[i].Location = new System.Drawing.Point(
10, 10 + i * 20);
groupBox1.Controls.Add(radioButtons[i]);
radioButtons[i].Click += new EventHandler(this.radioButtons_Click);
}
}
}
有没有什么办法可以从LoadDocument
调用这个函数而不使它变成静态的?我是否必须使LoadDocument
成为动态的,在这种情况下如何?
编辑:我想这段代码将是非常相关:
private LoadDocument m_form1;
private bool m_underConstruction = false;
private void ShowLoadDocument()
{
if (m_underConstruction)
{
// We're about to show it anyway
return;
}
m_underConstruction = true;
try
{
if (m_form1 == null)
{
m_form1 = new LoadDocument();
// m_form1.FormClosed += new FormClosedEventHandler(m_form1_FormClosed);
m_form1.Show();
}
}
finally
{
m_underConstruction = false;
}
m_form1.BringToFront();
m_form1.Activate();
}
在代码中最有趣的部分是调用showTasks()的代码。如果从LoadDocument表单调用Form1的-instance-函数,应该不会有问题,但我想您将其称为Form1的静态方法。 – Boaz
从LoadDocument表单上的按钮调用它 private void btnLoad_Click(object sender,EventArgs e) Form1.activeDocumentId = documentId; // Form1.showTasks(); this.Close(); } – Raptor
问题是你的架构。为什么表单无论如何都会互相干扰对方的内容?你应该更新每种形式反映的某种通用模型。您不应*需要*调用其他表单上的任何方法。 – siride