我有一个主窗体和一个用户控件(带图形)。我正在动态更新图表。
void foo(int i)
{
MainForm MF = (MainForm)MainForm.ActiveForm;
if (tb != null)
{
tb.userGraph.updatesomething(i);
// usergraph is the user control which has graphs
}
}
问题是当我离开主表单时我无法更新图表。有没有其他方法可以写入用户控件,即使它不是活动表单? 如果我创建一个新实例,图表上的先前更新将会消失。
答案 0 :(得分:1)
我认为最简单的解决方案是在表单和用户控件之间传递usercontrol的引用。在用户控件中创建一个字段,例如;
public Form formExample{get;set;}
当您最初实例化usercontrol时,将引用作为参数传递;
userControl usercontrol = new userControl();
usercontrol.formExample = this;
//passing this from the original form will pass a reference of it.
这样您现在可以从用户控件访问表单上的图形。如果你想反过来这样做,即从表单访问用户控件上的图形,只需反过来传递引用。我自由键入了这个,但我认为语法就在那里!
答案 1 :(得分:1)
我使用Application.OpenForms["Name of the Form"]
方法解决了问题。