none
NumericUpDown.ValueChanged corrupting executon of MyBase.Load?

    Question

  • I'm having a silly problem.

    Upon loading a form, I'm trying to change the value of a NumericUpDown.

    The form is being declared as following as a class-level private variable:

    Dim EMITLC As New EMITLogCreator(My.Computer.FileSystem.ReadAllText("Temp\deviceid.txt"), True)

    The New constructors of the form (EMITLogCreator) are as following (PrepareLoad is currently an empty method, and Time is a custom class):

        Private Log As New ArrayList
        Private TimeUsedTotal As New Time
        Private DeviceNumber As String
        Private CurrentPost As Byte = 1
        Private SaveDevIDOnExit As Boolean


    #Region "Constructors" Public Sub New(Optional SaveDeviceNumberOnExit As Boolean = False) ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Me.DeviceNumber = 0 PrepareLoad() SaveDevIDOnExit = SaveDeviceNumberOnExit End Sub Public Sub New(ByVal DeviceNumber As Integer, Optional SaveDeviceNumberOnExit As Boolean = False) InitializeComponent() Me.DeviceNumber = DeviceNumber.ToString() PrepareLoad() Do Until Me.DeviceNumber.Length >= 6 Me.DeviceNumber = "0" & DeviceNumber Loop SaveDevIDOnExit = SaveDeviceNumberOnExit End Sub Public Sub New(ByVal DeviceNumber As String, Optional SaveDeviceNumberOnExit As Boolean = False) InitializeComponent() Me.DeviceNumber = DeviceNumber PrepareLoad() Do Until Me.DeviceNumber.Length >= 6 Me.DeviceNumber = "0" & DeviceNumber Loop SaveDevIDOnExit = SaveDeviceNumberOnExit End Sub #End Region

    On the form I have a variety of controls, including one called NumericUpDown1. What I'm trying to do is to change it's value to what's specified as Me.DeviceNumber (string, as seen above), for each constructor:

    ' New(Optional SaveDeviceNumberOnExit As Boolean = False)
    NumericUpDown1.Value = 0
    
    ' New(ByVal DeviceNumber As Integer, Optional SaveDeviceNumberOnExit As Boolean = False)
    NumericUpDown1.Value = DeviceNumber
    
    ' New(ByVal DeviceNumber As String, Optional SaveDeviceNumberOnExit As Boolean = False)
    NumericUpDown1.Value = CInt(DeviceNumber)

    The problem is that after that code is called, it quits the constructor (happens in all three of them) and skips the MyBase.Load event handler, so a lot of important code is missed. The code has to be executed or the application will crash.

    I tried placing it in MyBase.Load aswell, but it just quits the event.

    I enclosed the code in a Try..Catch..End Try snippet, of which appears when stepping through the code, that it reaches Try, then the code, and then directly to End Try (so no exception was thrown, and none in Immediate Window either).

    If I don't have the Try block, it quits the code as described...

    Here is a video better demonstrating what's happening:

    http://youtu.be/UYFOUDrS-yA

    As I was typing this, another bug appeared, the application freezing when clicking the button on the main form. Stepping through the code didn't freeze it.

    I have not tried to copy over to a new blank project... if you think that would help, please tell me!

    Sincerely yours,
    - bilde2910

    PS. If you need more info, code, or a ZIP of the project, please tell me ;)


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

    • Edited by bilde2910 Friday, June 08, 2012 7:47 PM More info on the Time class
    Friday, June 08, 2012 7:44 PM

Answers

All replies

  • NumericUpDown.Value is a Decimal type, not an Integer - http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.value(v=vs.71).aspx

    As such, you need to use::

    NumericUpDown1.Value = CDec(DeviceNumber)


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Carmelo La Monica Friday, June 08, 2012 7:59 PM
    • Marked as answer by bilde2910 Friday, June 08, 2012 8:17 PM
    Friday, June 08, 2012 7:50 PM
  • Carmelo - you beat me to marking Reed as the proposed answerer! ;-)

    @Blide--> On this and all of your forms, I'd suggest that you put the following at the very top:

    Option Strict On

    Option Explicit On


    Please call me Frank :)

    • Marked as answer by bilde2910 Friday, June 08, 2012 8:17 PM
    Friday, June 08, 2012 8:02 PM
  • First step would be switching Option Strict On.

    In the video, when you write that form_load has been skipped, it's not true. The code is still executing the code for the constructor. That happens before the Load event fires and the Form will be shown. That means, you're assuming "Log" to be filled in the constructor, but actually it will be filled later, in the load event.

    Side note: Instead of writing duplicate code in the constructors, call one from the other:

        Public Sub New(ByVal DeviceNumber As Integer, Optional SaveDeviceNumberOnExit As Boolean = False)
            MyClass.New(DeviceNumber.ToString, SaveDeviceNumberOnExit)
        End Sub

    I'm using String here because the field DeviceNumber is declared as String. (Should be Integer?)



    Armin

    Friday, June 08, 2012 8:10 PM
  • Can be both Integer and String, infact, as DeviceNumber is initially a string, but only contains numerical letters.

    Thanks for the fast answers! Option Strict On highlighted a lot of errors which were now fixed, and also putting the Log filler in the constructor instead of MyBase.Load made it work!


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

    Friday, June 08, 2012 8:17 PM
  • Can be both Integer and String, infact, as DeviceNumber is initially a string, but only contains numerical letters.

    Thanks for the fast answers! Option Strict On highlighted a lot of errors which were now fixed, and also putting the Log filler in the constructor instead of MyBase.Load made it work!


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

    Glad you got it working!

    The problem with not using those options is that you then allow the compiler to infer and - it's not always right. It also slows the process because it has to do that, but that aside, it pretty much forces you to become a better coder.

    Glad it helped. :)


    Please call me Frank :)

    Friday, June 08, 2012 8:33 PM
  • Hy Frank,

    Carmelo - you beat me to marking Reed as the proposed answerer! ;-)

    Forgive me, but I did not understand because of my difficulty with the English what did you tell me :)

    Bye.


    Friday, June 08, 2012 8:35 PM
  • Forgive me, but I did not understand because of my difficulty with the English what did you tell me :)

    Hi Carmelo,

    I just meant that I signed on to mark Reed as the proposed answerer (and add some comments), and when I logged on, you'd already marked him as the proposed answerer.

    :)


    Please call me Frank :)

    Friday, June 08, 2012 8:37 PM
  • Hy Frank,

    Hi Carmelo,

    I just meant that I signed on to mark Reed as the proposed answerer (and add some comments), and when I logged on, you'd already marked him as the proposed answerer.

    :)


    Please call me Frank :)

    Now I understand :)

    Bye.


    Friday, June 08, 2012 8:40 PM