0

我一直在为我遇到的一个问题而烦恼(我刚开始用 C# 编码)

我有一个主屏幕(以下简称表格 1)和一个视频选项表格(以下简称表格 2)(包括图片)。现在,当我将 Form 2 中的 Radiobuttons 更改为“ Windowed ”并选择一个分辨率时,我希望在 Form 1 中更改一些选项。

到目前为止,这是我所获得的,它转到 Form 1 代码,但说我不能在那里改变任何东西。

代码片段主窗体

public partial class frmMain : Form
{
    public frmMain()
    {
        InitializeComponent();
    }

    private void btnVideo_Click(object sender, EventArgs e)
    {
        Visual_Options options = new Visual_Options();
        options.Show();
    }

代码片段选项表单

public partial class Visual_Options : Form
{
    frmMain Main;

    public Visual_Options()
    {
        InitializeComponent();
    }

    private void Visual_Options_Load(object sender, EventArgs e)
    {
        switch (Main.FormBorderStyle) //Check the Borderstyle of frmMain with Switch to determine current state
        {
            case FormBorderStyle.None: // if BorderStyle of frmMain = "None"
                if (Main.WindowState == FormWindowState.Maximized) //Check if frmMain = Maximizes
                {
                    rbFullscrn.Checked = true;
                }
                else
                {
                    rbBorderless.Checked = true;
                };
                break;
            case FormBorderStyle.Fixed3D:
                rbWindow.Checked = true;
                break;
        }

        switch (Main.Width) //Check Width to determine current value
        {
            case 800:
                rb8x6.Checked = true;
                break;
            case 1024:
                rb10x7.Checked = true;
                break;
            case 1280:
                rb12x7.Checked = true;
                break;
        }
    }

    private void btnAccept_Click(object sender, EventArgs e)
    {
        if (rbFullscrn.Checked == true)
        {
            Main.WindowState = FormWindowState.Maximized;
            Main.FormBorderStyle = FormBorderStyle.None;
        }
        else if (rbBorderless.Checked == true && rb8x6.Checked == true)
        {
            Main.WindowState = FormWindowState.Normal;
            Main.FormBorderStyle = FormBorderStyle.None;
            Main.Height = 600;
            Main.Width = 800;
        }
        else if (rbBorderless.Checked == true && rb10x7.Checked == true)
        {
            Main.WindowState = FormWindowState.Normal;
            Main.FormBorderStyle = FormBorderStyle.None;
            Main.Height = 768;
            Main.Width = 1024;
        }
        else if (rbBorderless.Checked == true && rb12x7.Checked == true)
        {
            Main.WindowState = FormWindowState.Normal;
            Main.FormBorderStyle = FormBorderStyle.None;
            Main.Height = 720;
            Main.Width = 1280;
        }
        else if (rbWindow.Checked == true && rb8x6.Checked == true)
        {
            Main.WindowState = FormWindowState.Normal;
            Main.FormBorderStyle = FormBorderStyle.Fixed3D;
            Main.Height = 600;
            Main.Width = 800;
        }
        else if (rbWindow.Checked == true && rb10x7.Checked == true)
        {
            Main.WindowState = FormWindowState.Normal;
            Main.FormBorderStyle = FormBorderStyle.Fixed3D;
            Main.Height = 768;
            Main.Width = 1024;
        }
        else if (rbWindow.Checked == true && rb12x7.Checked == true)
        {
            Main.WindowState = FormWindowState.Normal;
            Main.FormBorderStyle = FormBorderStyle.Fixed3D;
            Main.Height = 720;
            Main.Width = 1280;
        }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

我现在在互联网上看了一会儿,我找到了一些解决方案,但是,从我找到的几个解决方案中,到目前为止似乎没有一个有效。

提前TY,

我^_^

4

2 回答 2

2

如果您使用 ShowDialog,它将以模态对话框的形式打开您的选项表单,这样一旦您进行更改,您就可以检查DialogResult并从主表单中的选项表单中读取公共属性,并使用它来设置您的大小。否则,您可以订阅选项表单的已关闭事件并使用该事件来设置您的表单大小。

拳头选项:

private void button1_Click(object sender, EventArgs e)
{
    Visual_Options options = new Visual_Options();
    if ( options.ShowDialog() == DialogResult.OK)
        this.Size = options.getFormSize;  //This is a public property returning a size
}

使用相同属性的第二个选项:

private void button1_Click(object sender, EventArgs e)
{
    Visual_Options options = new Visual_Options();
    options.FormClosed+=new FormClosedEventHandler(options_FormClosed);
    options.Show();
}

void options_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Size = ((Visual_Options)sender).getFormSize;
    ((Visual_Options)sender).FormClosed -= new FormClosedEventHandler(options_FormClosed); //Remove handler to prevent leaks
}
于 2013-02-02T04:45:59.047 回答
0

您可能想要这样做:

options.Show(this);传递this引用将使“选项”表单成为主表单的子表单。

那么frmMain Main可以设置为:

frmMain Main = (frmMain)this.Owner;

在此之后,您可以更改主窗体的任何和所有属性。

于 2013-02-02T04:35:35.717 回答