Coder Guru
Coder Guru

Reputation: 513

How to create a fading form on focus lost &got event

I am doing project in vb.net When i click on button open I opened form with no control box(minimize,maximize etc).set borderStyle to FixedToolWindow I want to change the opacity of form on got focus & lost focus event. I also used activated & deactivated event but doesnt working

   Private Sub form_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs)HandlesMyBase.Deactivate
     Me.Opacity =0
      End Sub

   Private Sub form_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles  MyBase.Activated
        Me.Opacity = 1
    End Sub

Upvotes: 1

Views: 1635

Answers (2)

Mostafa Farzán
Mostafa Farzán

Reputation: 1003

Try 0.01 in your 2nd line. You used 0 and it will hide your form.

Because that when you click in form area , the form_Actived don't run.

Upvotes: 1

To do this you need to use a System.Windows.Forms.Timer. the implementation is very simple:

  • Have two variables called _fromOpactity and _toOpacity, and a constant OpacityStep = 0.05
  • on Form Activate or Deactivate set _fromOpacity and _toOpacity and start the timer to fade in/out.
  • In the timer Elapsed event handler, Increment or Decrement OpacityStep (depending on from/to) until the desired value is reached.

For a full example of how to do this, see this article.

Best regards,

Upvotes: 1

Related Questions