Results 1 to 4 of 4

Thread: [RESOLVED] Gradient Button [2005]

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Resolved [RESOLVED] Gradient Button [2005]

    How do I get this code to work from from the getgo? It works If I put it in the mouse events but not the controls paint event. I have tried adapting different methods from several threads but I cannot seem to get any of them to work.....

    VB Code:
    1. Private Sub DrawGradientButton(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
    2.         Try
    3.             Dim g As Graphics = Button1.CreateGraphics
    4.             Dim rect As New Rectangle(0, 0, Button1.Width, Button1.Height)
    5.             Dim br As New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White, Color.SlateGray, Drawing2D.LinearGradientMode.Horizontal)
    6.             Dim x As Single = CSng(Button1.Width / 2)
    7.             Dim y As Single = CSng(Button1.Height / 4)
    8.             Dim sBrush As New System.Drawing.SolidBrush(Color.WhiteSmoke)
    9.  
    10.             Dim Font As System.Drawing.Font = Button1.Font
    11.             Dim txt As String = Button1.Text
    12.  
    13.             g.FillRectangle(br, rect)
    14.             g.DrawString(txt, Font, sBrush, x, y)
    15.  
    16.             br.Dispose()
    17.             g.Dispose()
    18.  
    19.         Catch ex As Exception
    20.             MsgBox(ex.Message)
    21.         End Try
    22.     End Sub

    Thank you!!

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  2. #2
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Gradient Button [2005]

    Try changing the way you get the graphics object to:

    VB Code:
    1. Dim g as Graphics = e.Graphics

    Oh and you'll need to delete the line..

    VB Code:
    1. g.dispose()
    Last edited by Tinbeard; Jan 25th, 2007 at 04:37 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Gradient Button [2005]

    It gives me this error:

    VB Code:
    1. System.ArgumentException was unhandled
    2.   Message="Parameter is not valid."
    3.   Source="System.Drawing"
    4.   StackTrace:
    5.        at System.Drawing.Graphics.GetHdc()
    6.        at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
    7.        at System.Drawing.BufferedGraphics.Render()
    8.        at System.Windows.Forms.Control.WmPaint(Message& m)
    9.        at System.Windows.Forms.Control.WndProc(Message& m)
    10.        at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    11.        at System.Windows.Forms.Button.WndProc(Message& m)
    12.        at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    13.        at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    14.        at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    15.        at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    16.        at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
    17.        at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    18.        at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    19.        at System.Windows.Forms.Application.Run(ApplicationContext context)
    20.        at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    21.        at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    22.        at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    23.        at MyApp.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
    24.        at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
    25.        at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    26.        at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    27.        at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    28.        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    29.        at System.Threading.ThreadHelper.ThreadStart()

    I am working on what and where, but i wanted to post that up here quickly.

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  4. #4
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Gradient Button [2005]

    see the edi to my post above:

    delete the g.dispose() line

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width