Results 1 to 8 of 8

Thread: [RESOLVED] “Invaid Cast Exception” - ??????

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Resolved [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.

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    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
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    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?

  5. #5
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    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
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Last edited by jmcilhinney; Jun 5th, 2007 at 03:15 AM. Reason: Changed "granulation" to "granularity".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: “Invaid Cast Exception” - ??????

    Brilliant, it works just as I wanted it to!

    Many Thanks

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