Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Trying to understand the nature of inherited windows forms....

Trying to understand the nature of inherited windows forms....

Scheduled Pinned Locked Moved Visual Basic
visual-studiocsharpwinformsquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sam Marrocco
    wrote on last edited by
    #1

    I’m working on an application that uses an inherited windows form. I’ve accepted the forms designer bugs that accompany using inherited forms in Visual Studio 2012, but I’m trying to understand the nature of the inherited form itself. My original (parent) form code is relatively simple, but contains a small loop that updates a label control on the form with the current time. This form is then inherited by my other forms so that they will always display the current time. When I look at the parent form in the forms designer, it displays as it should, with whatever default text I’ve placed into the label control. However, when I look at any of my child/descendant forms their label is being updated with the current time, i.e. the code of the original parent form is compiled and running, even while viewing the descendant form in the VS Forms Designer. This seems strange to me—shouldn’t the descendant forms be inheriting the source code *only*, but not actually running it until the project is compiled and run?

    L B 2 Replies Last reply
    0
    • S Sam Marrocco

      I’m working on an application that uses an inherited windows form. I’ve accepted the forms designer bugs that accompany using inherited forms in Visual Studio 2012, but I’m trying to understand the nature of the inherited form itself. My original (parent) form code is relatively simple, but contains a small loop that updates a label control on the form with the current time. This form is then inherited by my other forms so that they will always display the current time. When I look at the parent form in the forms designer, it displays as it should, with whatever default text I’ve placed into the label control. However, when I look at any of my child/descendant forms their label is being updated with the current time, i.e. the code of the original parent form is compiled and running, even while viewing the descendant form in the VS Forms Designer. This seems strange to me—shouldn’t the descendant forms be inheriting the source code *only*, but not actually running it until the project is compiled and run?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      smarrocco wrote:

      houldn’t the descendant forms be inheriting the source code *only*, but not actually running it until the project is compiled and run?

      VS is trying to compile code as you edit; that's the way it underlines stuff that the compiler is missing. What you are seeing in the designer is a preview of the form you are editing, but it will need a compiled version of the form that it is based on.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      1 Reply Last reply
      0
      • S Sam Marrocco

        I’m working on an application that uses an inherited windows form. I’ve accepted the forms designer bugs that accompany using inherited forms in Visual Studio 2012, but I’m trying to understand the nature of the inherited form itself. My original (parent) form code is relatively simple, but contains a small loop that updates a label control on the form with the current time. This form is then inherited by my other forms so that they will always display the current time. When I look at the parent form in the forms designer, it displays as it should, with whatever default text I’ve placed into the label control. However, when I look at any of my child/descendant forms their label is being updated with the current time, i.e. the code of the original parent form is compiled and running, even while viewing the descendant form in the VS Forms Designer. This seems strange to me—shouldn’t the descendant forms be inheriting the source code *only*, but not actually running it until the project is compiled and run?

        B Offline
        B Offline
        bojammis
        wrote on last edited by
        #3

        Remove any functioning code from the constructors.

        Public Class myForm
        Inherits Form
        WithEvents _tmr As System.Timers.Timer
        Dim _lbDateTime As Label

        Public Sub New()
        End Sub
        Public Sub StartUI()
            \_tmr = New System.Timers.Timer
            \_tmr.Interval = 2000
            \_lbDateTime = New Label
            Controls.Add(\_lbDateTime)
            \_lbDateTime.Location = New Point(5, 5)
            \_lbDateTime.Visible = True
            \_lbDateTime.AutoSize = True
            \_tmr.Start()
        End Sub
        Private Sub Tick() Handles \_tmr.Elapsed
            \_tmr.Stop()
            UpdateTimer(Date.Now)
            \_tmr.Start()
        End Sub
        Private Sub UpdateTimer(ByVal time As String)
            If \_lbDateTime.InvokeRequired Then
                \_lbDateTime.Invoke(New Action(Of String)(AddressOf UpdateTimer), time)
            End If
            \_lbDateTime.Text = Date.Now
        End Sub
        

        End Class

        In the Child form's constructor add the call to StartUI()

        Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        StartUI()
        

        End Sub

        regs ron O.

        S 1 Reply Last reply
        0
        • B bojammis

          Remove any functioning code from the constructors.

          Public Class myForm
          Inherits Form
          WithEvents _tmr As System.Timers.Timer
          Dim _lbDateTime As Label

          Public Sub New()
          End Sub
          Public Sub StartUI()
              \_tmr = New System.Timers.Timer
              \_tmr.Interval = 2000
              \_lbDateTime = New Label
              Controls.Add(\_lbDateTime)
              \_lbDateTime.Location = New Point(5, 5)
              \_lbDateTime.Visible = True
              \_lbDateTime.AutoSize = True
              \_tmr.Start()
          End Sub
          Private Sub Tick() Handles \_tmr.Elapsed
              \_tmr.Stop()
              UpdateTimer(Date.Now)
              \_tmr.Start()
          End Sub
          Private Sub UpdateTimer(ByVal time As String)
              If \_lbDateTime.InvokeRequired Then
                  \_lbDateTime.Invoke(New Action(Of String)(AddressOf UpdateTimer), time)
              End If
              \_lbDateTime.Text = Date.Now
          End Sub
          

          End Class

          In the Child form's constructor add the call to StartUI()

          Public Sub New()

          ' This call is required by the designer.
          InitializeComponent()
          ' Add any initialization after the InitializeComponent() call.
          StartUI()
          

          End Sub

          regs ron O.

          S Offline
          S Offline
          Sam Marrocco
          wrote on last edited by
          #4

          While I appreciate the effort bojammis, It's more about understanding the issue and why the parent code compiles, or better yet, if it can be avoided with some IDE setting. There are a number of ways to avoid the problem, including using the DesignerMode flag to determine if code should run. Unfortunately it isn't only constructors that run. Load() and other events are triggered at undesired times as well, complicating the code, when the whole purpose of the inherited form should be for the simplification of the code. Also for clarification, my code does a lot more than build a timer--I simplified my description for this post. There are several independent background tasks which are being launched that now all have to monitor if the IDE is running them, or the designer.

          B 1 Reply Last reply
          0
          • S Sam Marrocco

            While I appreciate the effort bojammis, It's more about understanding the issue and why the parent code compiles, or better yet, if it can be avoided with some IDE setting. There are a number of ways to avoid the problem, including using the DesignerMode flag to determine if code should run. Unfortunately it isn't only constructors that run. Load() and other events are triggered at undesired times as well, complicating the code, when the whole purpose of the inherited form should be for the simplification of the code. Also for clarification, my code does a lot more than build a timer--I simplified my description for this post. There are several independent background tasks which are being launched that now all have to monitor if the IDE is running them, or the designer.

            B Offline
            B Offline
            bojammis
            wrote on last edited by
            #5

            Sorry about that - I should have read your post more closely.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups