Hack Forums
[.NET] [TUT]Making a Custom Button easily[TUT] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [TUT]Making a Custom Button easily[TUT] (/showthread.php?tid=1193840)



[TUT]Making a Custom Button easily[TUT] - Vertu - Apr 7, 2011

[IMG]http://www.[censored].net/uploads/1d26029562.gif[/IMG]
ow to do that WITHOUT using any images?

We just need 1 Button (of course), 2 events for calling our function, and our same Function from before (which is here slightly modified).

Here is the Function, I call it this time PaintGradient.
Code
Public Sub PaintGradient(ByVal aControl As Control, ByVal Color1 As Color, ByVal Color2 As Color, _
    Optional ByVal mode As System.Drawing.Drawing2D.LinearGradientMode = Drawing2D.LinearGradientMode.Vertical)

        Dim bmp As New Bitmap(aControl.Width, aControl.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim Rect1 As New RectangleF(0, 0, aControl.Width, aControl.Height)

        Dim lineGBrush As New System.Drawing.Drawing2D.LinearGradientBrush(Rect1, Color1, Color2, mode)
        g.FillRectangle(lineGBrush, Rect1)

        aControl.BackgroundImage = bmp
        g.Dispose()
    End Sub

Now we will use 2 events from our Button, MouseEnter and MouseLeave

Code
Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        PaintGradient(Button1, Color.LightGoldenrodYellow, Color.YellowGreen)
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
        Button1.BackgroundImage = Nothing
    End Sub

That's it, you are done. :lol:


* - Cody8295 - Apr 7, 2011

(Apr 7, 2011 01:49 PM)Vertu Wrote:  ow to do that WITHOUT using any images?

Technically this is using images. Hence the bitmap.


* - no1me - Apr 7, 2011

why not
PHP Code

<?php 
Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
  PaintGradient
(Button1, Color.LightGoldenrodYellow, Color.YellowGreen)
    
End Sub
    
Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
  PaintGradient
(Button1, Color.LightGoldenrodYellow, Color.Green)
    
End Sub
    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  PaintGradient
(Button1, Color.LightGoldenrodYellow, Color.Yellow)
    
End Sub 



* - Vertu - Apr 7, 2011

(Apr 7, 2011 01:52 PM)Cody8295 Wrote:  Technically this is using images. Hence the bitmap.
Ok,just i you dint need to load from resources

@no1me

That works too 🤗


* - Cody8295 - Apr 7, 2011

(Apr 7, 2011 02:20 PM)Vertu Wrote:  Ok,just i you dint need to load from resources

@no1me

That works too 🤗

You don't need to if you use and imagelist with local images.




* - Kōtei - Apr 7, 2011

This is nice, thank you no1me for the code modification, making it simpler and such.


* - Marketplace - Apr 7, 2011

I love this. I hope you make more tutorials like how to make custom background, progress bars, etc.

And can't you do:

Code
Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
  Button1.BackColor = Color.LightGoldenrodYellow
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
  Button1.BackColor = Color.Empty
    End Sub



* - Aeonhack Archivist - Apr 7, 2011

(Apr 7, 2011 04:33 PM)AlphaSquad Wrote:  I love this. I hope you make more tutorials like how to make custom background, progress bars, etc.

And can't you do:

Code
Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
  Button1.BackColor = Color.LightGoldenrodYellow
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
  Button1.BackColor = Color.Empty
    End Sub

All your code is doing is applying a single color. The other codes are filling with a gradient.