문제

Now before anyone goes and marks this question as a duplicate, I'd like to say that my problem differs from the other ones. I'm trying to open an existing Form from another, but I'm having problems in the sense that I've set some Forms to 'host' others (To transfer variables between them). Here's what I mean:

 public partial class Schedule_Tasks : Form
{

    readonly Schedules schedules;

    public Schedule_Tasks(Schedules host)
    {
        this.schedules = host;
        InitializeComponent();
    }

So in this snippet of code, I'm trying to get the value of some variables from the Schedules form, into the Schedule_Tasks Form. So I've used the 'host' system. SO far this method works fine, but my problem occurs when I try to open a specific Form, from another that isn't 'hosting'. For example using:

new Schedules().Show();

So obviously when I'm declaring this, I'd put something like 'this' in the brackets after Schedules, but that doesn't work if the Form is being called outside of the 'host' Form. I'd just like to now is there something I'm missing or can change? Please let me know if any part isn't clear, it's a little difficult to explain. Any help is appreciated, Cheers.

EDIT

Here's the code that I'm working with now:

 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
    }
}

And here is the constructor for Folder_Browser, which is the Form I'm trying to call:

 readonly Back_up_Options backOptions;
    public string deviceSel;

    public Folder_Browser(Back_up_Options host)
    {
        InitializeComponent();
        this.backOptions = host;
        deviceSel = backOptions.deviceSel;

    }
도움이 되었습니까?

해결책

Your (Folder_Browser) Form's constructor is declared as

public Folder_Browser(Back_up_Options host)

That means you cannot pass a Schedual_Tasks instance as the host parameter because there is no way to convert from a Schedual_Tasks object into a Back_up_Options object. The compiler detects this and creates an error message.

If you cannot pass the host parameter, you can pass null instead:

new Folder_Browser(null).Show();

But then you need to make sure that you check the backOptions member for null reference each time you use it. For example:

if(backOptions != null)
{
    deviceSel = backOptions.deviceSel;
}
else 
{
    deviceSel = null;
}

That in turn means that you will need to check deviceSel for null each time you use it and so on.

And of course, using your Form without a "host" needs to be possible at all. If you have code that requires a "host", it will fail.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top