【解決方法】同じパネルでform1を閉じてform2を開く方法は?

[ad_1]

When I click the button for the first time, form 1 opens but when I click the second button, form 2 does not open(Form2 opens but form1 doesn't hide so form2 can't be seen). Both Form1 and Form2 are to be opened in the same panel

私が試したこと:

C#
//objectsettingpanel is panel

//ObjectPlacement is form1.

//ManualControl is form2.

 private void Object_Placement_btn_Click(object sender, EventArgs e)
        {
            ObjectPlacement objectPlacement = new ObjectPlacement() { TopLevel = false, TopMost = true };

            objectPlacement.FormBorderStyle = FormBorderStyle.None;

            objectsettingpanel.Controls.Add(objectPlacement);

            objectPlacement.Show();
        }

        private void Manual_Controlbtn_Click(object sender, EventArgs e)
        {
            ManualControl ManualControl = new ManualControl() { TopLevel = false, TopMost = true };

            ManualControl.FormBorderStyle = FormBorderStyle.None;

            objectsettingpanel.Controls.Add(ManualControl);

            ManualControl.Show();
        }

解決策 1

クリック イベントの外にフォーム参照を保存します。

C#
private ObjectPlacement? objectPlacement;
private ManualControl? manualControl;

public Form1()
{
    InitializeComponent();
}

private void Object_Placement_btn_Click(object sender, EventArgs e)
{
    if (manualControl is not null)
    {
        manualControl.Hide();
        manualControl = null;
    }

    if (objectPlacement is not null)
        return;

    objectPlacement = new ObjectPlacement();
    objectPlacement.Show();
}

private void Manual_Control_btn_Click(object sender, EventArgs e)
{
    if (objectPlacement is not null)
    {
        objectPlacement.Hide();
        objectPlacement = null;
    }

    if (manualControl is not null)
        return;

    manualControl = new ManualControl();
    manualControl.Show();
}

[ad_2]

コメント

タイトルとURLをコピーしました