1

I have (for fun) make a form in C#, with a trackbar. I want to change the Opacity of the form with it, so I wrote this:

private void trackBar1_Scroll(object sender, EventArgs e)
{
    progressBar1.Value = trackBar1.Value;
    System.Windows.Forms.Form.ActiveForm.Opacity = trackBar1.Value;
    label2.Text = trackBar1.Value.ToString();
}

When I start the program, the opacity will be 100% if the trackbar is in value 1 to 100, and if i drag the trackbar to 0, the form becomes fully transparant.

can you only get 100% Opacity or 0% Opacity when a form is started, or is what i want also possible?

Manish Kumar
  • 15,269
  • 5
  • 18
  • 27

4 Answers4

4

Use this:

System.Windows.Forms.Form.ActiveForm.Opacity = ((double)(trackBar1.Value) /100.0)

You can have different degrees of opacity. For example 0.5 will give you 50% opacity.

AlexDev
  • 4,049
  • 31
  • 36
4

The value of System.Windows.Forms.Form.Opacity is between 0.0 and 1.0, to get the percentage of the opacity you can multiply it with 100, so 1 means fully opaque and 0 means fully transparent.

For the trackbar, you should convert its Value to the corresponding value between 0.0 and 1.0, so you should do something like this:

yourForm.Opacity = (double)trackBar1.Value/trackBar1.Maximum;
King King
  • 61,710
  • 16
  • 105
  • 130
3

Divide the number by 100. It should be a double between 0 and 1

((double)trackBar1.Value) / 100
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
-1

private void trackBar1_Scroll(object sender, EventArgs e) { label1.Text =trackBar1.Value.ToString()+ "%";

        Opacity=trackBar1.Value / 100.00;
        
    }
  • 1
    a bit more explanation of your solution would be great, in that case it's simply because opacity is a value between 0 and 1. – TinoZ May 20 '21 at 12:54