School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,089 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,046 people online right now. Registration is fast and FREE... Join Now!




Creating a mouse-dependant fading toolbox

 
Reply to this topicStart new topic

> Creating a mouse-dependant fading toolbox

remorseless
Group Icon



post 26 Aug, 2009 - 03:48 AM
Post #1


Hey guys. Today I'm gonna show you one of the coolest things you can use in your forms. Ever created a toolbox (a form that has tools or lots of buttons, like an on-screen keyboard, for example) but wished it would disappear when you don't need it? You probably don't, but I'm gonna show you anyway.

This is a little something I did in my spare time. It took me a while, but the end code turned out to be relatively simple. I'm only new to VB (only a student still tongue.gif ) and I would eventually like to work into more advanced languages.

So here's what you need:
  • A form (any size, shape, colour..It doesn't matter..It's your choice!)
  • A timer (Enabled, Interval is 10 milliseconds)

That's it. Just to clear things up, I'm using 10 milliseconds because it doesn't lag the fading. You can put it at 1 if you like but for now, I'm just going to use 10.

Anyways, let's start with the code.
CODE

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (MousePosition.X - (Me.Location.X + Me.Width)) > 33 Then
            Me.Opacity -= 0.03
        ElseIf (MousePosition.Y - (Me.Location.Y + Me.Height)) < -(Me.Bounds.Height + 33) Then
            Me.Opacity -= 0.03
        ElseIf (MousePosition.X - (Me.Location.X + Me.Width)) < -(Me.Bounds.Width + 33) Then
            Me.Opacity -= 0.03
        ElseIf (MousePosition.Y - (Me.Location.Y + Me.Height)) > 33 Then
            Me.Opacity -= 0.03
        Else
            Me.Opacity += 0.03
        End If


Ok, let me break it down for you:
CODE

        If (MousePosition.X - (Me.Location.X + Me.Width)) > 33 Then
            Me.Opacity -= 0.03

Basically, this tells us if the user has his mouse too far right from the form, the form will fade away by reducing the opacity by 0.03 (which is equal to 3%) every interval (10ms). This means it takes a grand total of 30 intervals (or 300ms) for the form to fully disappear. You can change this number to suit your design. If you want it to appear/reappear quickly, you can try 0.1 or 0.2 (which equals 10% and 20% respectively) and so on..
CODE

        ElseIf (MousePosition.Y - (Me.Location.Y + Me.Height)) < -(Me.Bounds.Height + 33) Then
            Me.Opacity -= 0.03

This tells us that if the user is too high above the form, the form will fade away.
CODE

       ElseIf (MousePosition.X - (Me.Location.X + Me.Width)) < -(Me.Bounds.Width + 33) Then
            Me.Opacity -= 0.03

Again, this tells us if the user is too far to the left of the form, and the form will fade away.
CODE

        ElseIf (MousePosition.Y - (Me.Location.Y + Me.Height)) > 33 Then
            Me.Opacity -= 0.03

You probably got the hang of this...This tells us if the user is too far below the form, and the form will (of course) fade away.
CODE

        Else
            Me.Opacity += 0.03
        End If

And finally, if all of the above fail (which means the user is within reach of the form), the form will reappear again.

If you notice in my code, the 33's are there because they are the distance from which the user has to be in from the form to actually appear. For example, the user has to be at least 33 pixels away from the top edge of the form for the form to appear. Feel free to change the 33's to any number you like. If you want your user to have to get real close to the form for it to appear, you could put 10 or even 5. But if you want the form to appear from a further distance, you could put 100. It's really up to you.

Here's a little OPTIONAL addition to your program. This code basically says that if you at any point hold down the space bar, the form will appear instantly. This could be handy if your user can't find the form. Again, this part is optional.

CODE

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode() = Keys.Space Then
            Me.Opacity() = 1
            Timer1.Stop()
        Else
            'Do Nothing
        End If
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Timer1.Start()
    End Sub

You can change the key to be anything. Notice that I turned off the timer when the key is pressed and turned it on when it is not, that's because if I didn't it would interact with the form and make it pulse. (fade out then instantly fade in repeatedly)

I hope you learned something new today, and remember me when you are using this technique wink2.gif Comments and suggestions are greatly appreciated.

Thanks and have a great day!
-remorseless
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Roxor
Group Icon



post 14 Sep, 2009 - 05:26 AM
Post #2
Nice Tut, but i got a problem, i want to make too a tutorial, i entered all i need and clicked New Thread, and when to show my tutorial, there is nothing, it redirects me to the tutorials page,i search for my tutorial and is nowhere. Why can't i post the tut? Thnx for future help:)
Go to the top of the page
+Quote Post

pharaon
*



post 20 Sep, 2009 - 11:09 AM
Post #3
it didnt work with me at all ...i put a button in the form and write the code for the timer and run it nothing happen
Go to the top of the page
+Quote Post

remorseless
Group Icon



post 27 Sep, 2009 - 08:06 AM
Post #4
QUOTE(pharaon @ 21 Sep, 2009 - 05:09 AM) *

it didnt work with me at all ...i put a button in the form and write the code for the timer and run it nothing happen


Was your timer enabled?
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 11:23AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month