[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:
Private Sub DrawGradientButton(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
Try
Dim g As Graphics = Button1.CreateGraphics
Dim rect As New Rectangle(0, 0, Button1.Width, Button1.Height)
Dim br As New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White, Color.SlateGray, Drawing2D.LinearGradientMode.Horizontal)
Dim x As Single = CSng(Button1.Width / 2)
Dim y As Single = CSng(Button1.Height / 4)
Dim sBrush As New System.Drawing.SolidBrush(Color.WhiteSmoke)
Dim Font As System.Drawing.Font = Button1.Font
Dim txt As String = Button1.Text
g.FillRectangle(br, rect)
g.DrawString(txt, Font, sBrush, x, y)
br.Dispose()
g.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Thank you!!
D
Re: Gradient Button [2005]
Try changing the way you get the graphics object to:
VB Code:
Dim g as Graphics = e.Graphics
Oh and you'll need to delete the line..
Re: Gradient Button [2005]
It gives me this error:
VB Code:
System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Graphics.GetHdc()
at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
at System.Drawing.BufferedGraphics.Render()
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at MyApp.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
I am working on what and where, but i wanted to post that up here quickly.
D
Re: Gradient Button [2005]
see the edi to my post above:
delete the g.dispose() line