简体   繁体   中英

How to Change the Opacity of a form from another form in VB.net

a simple way to change the opacity of a form another form

In the main form you want to change its opacity (which i named it MainForm) make a textbox, call it ChangeSettingsTextBox and in the form that you want to use it to change the other form opacity create a TrackBar and name it OpacityTrackBar (you can use a textbox or something else...) and add this code to the MainForm

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles ChangeSettingsTextBox.TextChanged
    Me.Opacity = ChangeSettingsTextBox.Text
End Sub

When you want to open the form (which i named it Settings) that will change the main form opacity

Settings.Show()

in the changer form add this code:

Private Sub OpacityTrackBar_Scroll(sender As Object, e As EventArgs) Handles OpacityTrackBar.Scroll
    MainForm.ChangeSettingsTextBox.Text = OpacityTrackBar.Value / 100
End Sub

I think that you can not change the opacity of a form from another form because it is like an inside property that must be changed from inside the class but still can change the value of the textbox from outside the form because it is not part of the form object special properties...and while you are changing that textbox value...you are changing it from inside the main form and therefore you can at this moment change the form property as the program now is working inside the structure of the main form

First of all, Opacity is a double. Doing Me.Opacity = ChangeSettingsTextBox.Text is setting a double property equal to a string. You should convert to a double ie

Me.Opacity = Double.Parse(ChangeSettingsTextBox.Text)

Putting Option Strict On at the top of your code fill will help you see all your type mismatches.


Let's assume you have two forms: Form1 and Form2. Form 2 will have an instance of Form1 (MainForm) and will set the opacity in the OpacityTrackBar.Scroll handler

Public Class Form2
    Private MainForm As New Form1()
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MainForm.Show()
        OpacityTrackBar.Minimum = 0
        OpacityTrackBar.Maximum = 100
    End Sub
    Private Sub OpacityTrackBar_Scroll(sender As Object, e As EventArgs) Handles OpacityTrackBar.Scroll
        MainForm.Opacity = OpacityTrackBar.Value / 100
    End Sub
End Class

Putting it in a TextBox and handling the TextBox.TextChanged event seems overly complex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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