简体   繁体   English

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

[英]C# Change Form1 Width with data from Form 2

I've been breaking my head over a problem I have (I just started coding in C#) 我一直在解决我遇到的问题(我刚开始用C#编写代码)

I have a Main Screen (Hereafter reffered to as Form 1) and a Video Options Form (hereafter reffered to as Form 2) (Picture Included). 我有一个主屏幕(以下称为表格1)和一个视频选项表格(以下简称表格2)(包含图片)。 Now, when fe I change the Radiobuttons in Form 2 to " Windowed " and Select a Resolution, I want some options to change in Form 1. 现在,当我将表单2中的Radiobuttons更改为“ Windowed ”并选择一个分辨率时,我想在表单1中更改一些选项。

This is as far as I got so far, it goes to the Form 1 code, but says I can't change anything there. 到目前为止,这是表格1的代码,但是说我不能改变它。

Code Snippet Main Form 代码片段主窗体

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

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

Code Snippet Options Form 代码片段选项表单

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();
    }
}

I've looked on the internet for a bit now, and, I found a few solutions, but, from the few solutions I found, none seemed to work so far. 我现在在互联网上看了一下,并且,我找到了一些解决方案,但是,从我发现的几个解决方案中,到目前为止似乎都没有。

Ty in advance, Ty提前,

Me ^_^ 我^ _ ^

If you use ShowDialog, it will open your options Form as a Modal Dialog, that way once you make changes you can check the DialogResult and read a Public Property from your Option Form in your Main Form, using it to set your size. 如果您使用ShowDialog,它将打开您的选项Form作为模态对话框,这样一旦您进行更改,您可以检查DialogResult并从主窗体中的选项表单中读取公共属性,使用它来设置您的大小。 Otherwise you could subscribe to the Options Form's Closed Event and use that event to set your Forms Size. 否则,您可以订阅选项表单的已结束事件,并使用该事件来设置您的表单大小。

Fist Option: 拳头选项:

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
}

2nd Option using same property: 使用相同属性的第二选项:

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
}

You might want to do this: 你可能想这样做:

options.Show(this); passing the this reference will make "options" form as the child of your mainform. 传递this引用将使“选项”形式成为主窗体的子级。

Then frmMain Main can be set as: 然后frmMain Main可以设置为:

frmMain Main = (frmMain)this.Owner;

After this, you can alter any and all properties of your main form. 在此之后,您可以更改主表单的任何和所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM