【发布时间】:2013-11-11 07:12:25
【问题描述】:
现在,在有人将此问题标记为重复问题之前,我想说我的问题与其他问题不同。我正在尝试从另一个表单打开现有表单,但我遇到了一些问题,因为我已经将一些表单设置为“托管”其他表单(在它们之间传输变量)。这就是我的意思:
public partial class Schedule_Tasks : Form
{
readonly Schedules schedules;
public Schedule_Tasks(Schedules host)
{
this.schedules = host;
InitializeComponent();
}
因此,在这段代码的 sn-p 中,我试图从 Schedules 表单中获取一些变量的值,并将其放入 Schedule_Tasks 表单中。所以我使用了“主机”系统。到目前为止,这种方法工作正常,但是当我尝试从另一个不是“托管”的表单打开特定表单时,就会出现问题。例如使用:
new Schedules().Show();
所以很明显,当我声明这一点时,我会在 Schedules 之后的括号中添加类似“this”的内容,但如果在“主机”表单之外调用表单,则这不起作用。我只是想现在有什么我缺少或可以改变的吗?如果任何部分不清楚,请告诉我,这有点难以解释。任何帮助表示赞赏,干杯。
编辑
这是我现在正在使用的代码:
public partial class Schedual_Tasks : Form
{
readonly Scheduals scheduals;
public string selectedDevice;
public string getPath;
public string totalPath;
public Schedual_Tasks(Scheduals host)
{
this.scheduals = host;
InitializeComponent();
selectedDevice = scheduals.itemSelected;
}
private void Schedual_Tasks_Load(object sender, EventArgs e)
{
}
private void changeDirectory_Click(object sender, EventArgs e)
{
new Folder_Browser(this).Show(); //Error Occurs here
}
}
这里是 Folder_Browser 的构造函数,这是我试图调用的表单:
readonly Back_up_Options backOptions;
public string deviceSel;
public Folder_Browser(Back_up_Options host)
{
InitializeComponent();
this.backOptions = host;
deviceSel = backOptions.deviceSel;
}
【问题讨论】:
-
我认为你不能
new Schedules().Show();你可以在实例化之后调用对象方法说myForm = new Schedules();myForm.Show();但我想不知道你说的"Form, from another that isn't hosting"是什么意思?最好将大部分(如果不是全部)代码放在这里,以便我们可以看到全貌。 -
Schedules 也是
Form,对吧?当您谈到发生的问题时,问题是什么?编译时错误还是运行时异常? -
schedules.Show()不起作用? -
请您多放一些代码,以便我们了解实际问题是什么..
-
@Edper 我已经添加了我正在使用的代码。我的意思是我刚刚在“托管”的表单中获取了另一种表单的元素,我不知道有更好的方式来放置它。