C#使用表单2中的数据更改Form1宽度

时间:2013-02-02 04:29:33

标签: c# forms

我一直在解决我遇到的问题(我刚开始用C#编码)

我有一个主屏幕(以下称为表格1)和一个视频选项表格(以下简称表格2)(包含图片)。现在,当f.e.我将表单2中的Radiobuttons更改为“ Windowed ”并选择一个分辨率,我想在表单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,

我^ _ ^

2 个答案:

答案 0 :(得分:2)

如果您使用ShowDialog,它将打开您的选项Form作为模态对话框,这样一旦您进行更改,您可以检查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
}

答案 1 :(得分:0)

您可能想要这样做:

options.Show(this);传递this引用会使“选项”形成为主体的子项。

然后frmMain Main可以设置为:

frmMain Main = (frmMain)this.Owner;

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