|
-
Mar 17th, 2007, 08:29 PM
#1
Thread Starter
Fanatic Member
Form_Paint: Invalid parameter used.
I am trying to do a Microsoft tutorial, and incorporate it into my software. I decided to start out just trying to get their tutorial to work, however it is not working.
http://www.microsoft.com/seminar/sha...est.xml&rate=1
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Invalid parameter used.
Code:
Private Sub frmStudy_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim canvas As Graphics = e.Graphics
Dim expandingBox As New Rectangle(0, _
0, _
e.ClipRectangle.Width / 2, _
e.ClipRectangle.Height / 2)
Dim paintBrush As New LinearGradientBrush(expandingBox, _
Color.White, Color.Blue, LinearGradientMode.ForwardDiagonal)
canvas.FillRectangle(paintBrush, expandingBox)
paintBrush.Dispose()
canvas.Dispose()
End Sub
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Mar 17th, 2007, 08:58 PM
#2
Re: Form_Paint: Invalid parameter used.
It's always a good idea to specify WHERE the exception is thrown. My guess it's this line:
vb Code:
Dim expandingBox As New Rectangle(0, _
0, _
e.ClipRectangle.Width / 2, _
e.ClipRectangle.Height / 2)
When you divide one Integer by another Integer the result is a Double. The Rectangle constructor has ONLY Integer arguments, so your Doubles will have to be implicitly converted to Integers. If the first number is even then that's OK because the result will have no decimal portion anyway, but if the first number is odd then it won't be possible. BOOM! Exception.
If you want the result of a division to be an Integer then you have to use integer division, which is a "\" instead of a "/". Integer division returns the whole portion of the result only, so any decimal portion will just be removed.
This is also a good example of why you should have Option Strict turned On. That would have told you that you were trying to force a Double value into an Integer argument and alerted you that there was a potential problem.
Finally, do NOT dispose a Graphics object in a Paint event handler. You ONLY dispose a Graphics object if you created it. If it was created elsewhere then it may be needed elsewhere so your disposing it may cause an exception in another part of the code. What if that same Paint event is handled elsewhere and you've disposed the Graphics object?
-
Mar 17th, 2007, 09:11 PM
#3
Thread Starter
Fanatic Member
Re: Form_Paint: Invalid parameter used.
I tried the / and \ but neither worked. Also, I checked the Microsoft movie, and they were doing it the same. Also, they told me to do the dispose.
And I have no idea, but this is where my error is coming from:
Friend Class frmStudy
I will try to get the Option Strict turned on, but I have a lot of stuff that needs modifying to be up to par, and some of it will be very hard but I will work at it some more. My other forms I use the strict mostly, but this one is difficult to do everything 
I understand about 70 of the messages, but here are a few I do not know what to do:
C:\Reports\Jeff\VB\study\StudyX.NET\frmStudy.vb(1192): Option Strict On disallows late binding.
How do I do an explicit declaration?
C:\Reports\Jeff\VB\study\StudyX.NET\frmStudy.vb(875): Option Strict On disallows implicit conversions from 'Integer' to 'Short'.
questionCount = questionCount + 1
 Originally Posted by jmcilhinney
It's always a good idea to specify WHERE the exception is thrown. My guess it's this line:
vb Code:
Dim expandingBox As New Rectangle(0, _
0, _
e.ClipRectangle.Width / 2, _
e.ClipRectangle.Height / 2)
When you divide one Integer by another Integer the result is a Double. The Rectangle constructor has ONLY Integer arguments, so your Doubles will have to be implicitly converted to Integers. If the first number is even then that's OK because the result will have no decimal portion anyway, but if the first number is odd then it won't be possible. BOOM! Exception.
If you want the result of a division to be an Integer then you have to use integer division, which is a "\" instead of a "/". Integer division returns the whole portion of the result only, so any decimal portion will just be removed.
This is also a good example of why you should have Option Strict turned On. That would have told you that you were trying to force a Double value into an Integer argument and alerted you that there was a potential problem.
Finally, do NOT dispose a Graphics object in a Paint event handler. You ONLY dispose a Graphics object if you created it. If it was created elsewhere then it may be needed elsewhere so your disposing it may cause an exception in another part of the code. What if that same Paint event is handled elsewhere and you've disposed the Graphics object?
Last edited by rex64; Mar 17th, 2007 at 09:17 PM.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Mar 17th, 2007, 09:29 PM
#4
Re: Form_Paint: Invalid parameter used.
With regards to Option Strict, Microsoft have chosen to have it turned Off by default to make VB.NET "easier" to use. With it Off you don't have to think about a lot of conversions and the system will simply handle them implicitly for you. It's easier for beginners because they don't have to understand the differences between types and how to convert from one to the other. Ignorance is NOT bliss though. If you don't understand the differences between types and how to convert between them then how can you possibly fix issues related to those things? Your code may contain errors waiting to rear their heads at run time that could have been easily fixed with Option Strict On. Any serious developer should have Option Strict turned On by default and ONLY turn it off in specific files where it is required, like late-binding for Office Automation. There are very few situations where it is required. Having Option Strict On doesn't make anything difficult. It just means that you have to cast or convert many values in order that they are the appropriate type for where you want to put them. Casting and converting is easy. You have to do it a bit to get the hang of it though. If you never do it, as with Option Strict Off, you never get the hang of it and your code will be less efficient and less robust.
As for your issue, what I said was only a guess. Where you're now saying that the error is thrown wasn't even in your first post so that was going to be tough to get. Whenever an exception is thrown you will be given a lot more information than you gave us. You will be given the exact line in your code on which it was thrown, a call stack at that point and possibly more. If we're to diagnose your issue then that's the sort of information we need.
Finally, no matter what your tutorial said, it is wrong to dispose the e.Graphics object in a Paint event handler. In most cases it will not cause an issue because the object won't be used again anyway. Having said that, it doesn't gain you anything because it will be disposed by the object that created it anyway, plus if it does need to be used in another event handler then your disposing it will cause that event handler to throw an exception and at best not draw the form properly. At worst the app would crash.
-
Mar 17th, 2007, 10:02 PM
#5
Thread Starter
Fanatic Member
Re: Form_Paint: Invalid parameter used.
You were right on, sorry I forgot to try that. Now, I just need to find out more about VB .NET 2003 Brushes. I did a little Googleing, but no luck so far. Is there a good book that covers all of VB .NET 2003 and or 2005? Or what is the best resource? I always feel like I do not have good resources to find answers. Here is what I am trying to do. I want an image to be my background and re-sized. I saw another method in another thread, but I saw this and it looked like a cool way to do it.
Here is my code so far, but it does not work, because I do not know what format a "brush" is exactly, and how to convert a bitmap to it.
Code:
Private Sub frmStudy_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim canvas As Graphics = e.Graphics
Dim expandingBox As New Rectangle(0, _
0, _
e.ClipRectangle.Width, _
e.ClipRectangle.Height)
'Dim paintBrush As New LinearGradientBrush(expandingBox, _
' Color.White, Color.Blue, LinearGradientMode.ForwardDiagonal)
Dim paintBrush2 As New Bitmap("c:\resume.jpg")
canvas.FillRectangle(paintBrush2, expandingBox)
paintBrush.Dispose()
'canvas.Dispose()
End Sub
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Mar 17th, 2007, 10:34 PM
#6
Re: Form_Paint: Invalid parameter used.
If all you want is an image then you don't need a Brush at all. Think about different paint brushes and the result of using each to paint on a wall. .NET Brushes are similar. They control things like colour, texture, etc. of surface painting. If you want to put an image on your form then you don't want to FillRectangle with a colour, texture and what not. You just want to DrawImage. Having said that, can you not just set the form's BackgroundImage property?
There is no book that covers all of .NET. If there was it would take an army to carry it. The first resource you should always consult is MSDN. That will give you that basics for pretty much everything and various levels of detail for various specific areas. If you want details on using GDI+ to draw on controls then you should search the Web for "GDI+ tutorial" as there are several.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|