[RESOLVED] “Invaid Cast Exception” - ??????
I am trying to get a progressbar to display smoothly here is my code which causes an error “Invaid Cast Exception”
Code:
If ADN > 100 + (Rate) And ADN <= 100 + (Rate * 2) Then
ProgressBar1.Visible = True
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 1000
ProgressBar1.Minimum = 100 + (Rate)
ProgressBar1.Maximum = ((100 + (Rate * 2)))
ProgressBar1.Value = Format((ADN / ProgressBar1.Maximum), "###0%")
Else
End If
The values that cause this error are ADN = 105.49148819330023 and Progressbar1.Maximum =106
Progressbar1.Miniumum = 103
Now as you can see these numbers are very nearly the same which is why I am using the % to spread the values displayed by the progressbar from 0-100 to give some resolution to the display, as the progressbar values by default only step in 1's. :confused:
Re: “Invaid Cast Exception” - ??????
Hi,
the value property of the progress bar will only take an integer - you can't format it like that, i.e. using a % sign
Pete
Re: “Invaid Cast Exception” - ??????
Just use ADN\ProgressBar1.Maximum if ADN is greater than ProgressBar1.Maximum. That will do a simple integer division. Not as precise, but since the value can only handle integers, it makes little difference.
Re: “Invaid Cast Exception” - ??????
I have changed it to
ProgressBar1.Value = ((ADN / ProgressBar1.Maximum) * 100)
And now I get this errror:
System.ArgumentException was unhandled
Message="An error message cannot be displayed because an optional resource assembly containing it cannot be found"
StackTrace:
at Microsoft.AGL.Common.MISC.HandleAr()
at System.Windows.Forms.ProgressBar._SetInfo()
at System.Windows.Forms.ProgressBar.set_Value()
at Mx.Form1.AIR()
at Mx.Form1.VScrollBar3_ValueChanged_3()
at System.Windows.Forms.ScrollBar.OnValueChanged()
at System.Windows.Forms.ScrollBar.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.CTL.ScrollBarSetInfo()
at System.Windows.Forms.ScrollBar._SetInfo()
at System.Windows.Forms.ScrollBar.set_Value()
at Mx.Form1.InitializeComponent()
at Mx.Form1..ctor()
at System.Reflection.RuntimeConstructorInfo.InternalInvoke()
at System.Reflection.RuntimeConstructorInfo.Invoke()
at System.Reflection.ConstructorInfo.Invoke()
at System.Activator.CreateInstance()
at MyForms.Create__Instance__()
at MyForms.get_Form1()
at Mx.Form1.Main()
What on earth is going on?
Re: “Invaid Cast Exception” - ??????
Hi,
why do you set the minimum to 0, the max to 1000, and then immediately change them?
Not saying that explains the error, but...
Put a try catch around it and check the error message
Pete
Re: “Invaid Cast Exception” - ??????
Pete
The reason for this is I was getting an error when setting the max and min with the code so I set at a level that will always be bigger and then just re-set inside those limits. However if I take those away I still get the same error.
Let me try to explain what I am trying to do. My problem is that the Max value is likely to be say 105 and the min value say 103 and the actual value say 103.998765345 Now if I just use those values I get a very choppy display on the progress bar as it only moves in increments of whole 1’s then it will only display 103,104 and 105 values, were as if I calculate the percentage value then I will get (if it worked) a progress bar value from 0-100 as the actual values changed from 103 to 105 instead of what I get now is just 3 big steps :confused:
Re: “Invaid Cast Exception” - ??????
If the actual values are 103 and 105 then you're making your life more difficult by using Maximum and Minimum values of 0 and 100. If you want finer granularity then simply multiply the ACTUAL values by a factor, e.g.
ScaleFactor = 100
Minimum = 103 * ScaleFactor
Maximum = 105 * ScaleFactor
Value = CInt(Math.Floor(ADN * ScaleFactor))
If you want finer granularity again then use a larger scale factor.
Re: “Invaid Cast Exception” - ??????
Brilliant, it works just as I wanted it to!
Many Thanks