简体   繁体   English

使用第二种形式更改一种形式的不透明度

[英]Changing opacity of one form using a second form

So I want my form2's Trackbar to change the opacity of my form1 but it doesn't seem to get the job done? 所以我想让我的form2的Trackbar更改我的form1的不透明度,但似乎无法完成工作吗?

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

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        Form1 frm1 = new Form1();

        frm1.Opacity = trackBar1.Value * 000.1d;

    }
}

You are not changing the opacity of your Form1, you are changing the opacity of a new Form1. 你是不是改变你的 Form1的透明度,要更改一个新的 Form1的不透明度。 You need to make sure you change the opacity of the form instance you want to change: 您需要确保更改了要更改的表单实例的不透明度:

public partial class Form2 : Form
{
    private Form1 form;

    public Form2(Form1 form)
    {
        InitializeComponent();
        this.form = form;
    }

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        form.Opacity = trackBar1.Value * 000.1d;
    }
  }
}

Then when you create Form2, pass the instance of Form1 you want to change. 然后在创建Form2时,传递要更改的Form1实例。 For example from a button in your Form1: 例如,从Form1中的按钮:

public void opacityChangeButton_Click(object sender , EventArgs e)
{
    Form2 opacityChangeForm = new Form2(this);
    opacityChangeForm.ShowDialog();
}

Instead of creating a new form everytime use the id of the existing form: 不必每次都使用现有表单的ID来创建新表单:

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

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        // Do not create a new form: Form1 frm1 = new Form1();

        // Use name of original form
        whateverVariableyourCreateedForYourForm.Opacity = trackBar1.Value * 000.1d;

    }
}

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

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