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. How to create a window handle.

How to create a window handle.

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
7 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.
  • R Offline
    R Offline
    RJGCarey
    wrote on last edited by
    #1

    All of my applications have migrated to VB 2005. They all start from Sub Main in a module. Today I decided to create a new one and set "enable application framework" to true so that I can explore its new features. The program has been run many times as I add new components. There have been no apparent problems. I added code that creates a delegate to display the program's name and a date/time string as Form1's text. It is invoked once a second by a thread spauned by my "Clock" class that runs on a background thread. I encountered this error when it ran. Invoke or BeginInvoke cannot be called on a control until the window handle has been created. How does one create a handle for Form1? Why wasn't it created automatically when the application started? Thanks RCarey

    RCarey

    D 1 Reply Last reply
    0
    • R RJGCarey

      All of my applications have migrated to VB 2005. They all start from Sub Main in a module. Today I decided to create a new one and set "enable application framework" to true so that I can explore its new features. The program has been run many times as I add new components. There have been no apparent problems. I added code that creates a delegate to display the program's name and a date/time string as Form1's text. It is invoked once a second by a thread spauned by my "Clock" class that runs on a background thread. I encountered this error when it ran. Invoke or BeginInvoke cannot be called on a control until the window handle has been created. How does one create a handle for Form1? Why wasn't it created automatically when the application started? Thanks RCarey

      RCarey

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      RJGCarey wrote:

      How does one create a handle for Form1?

      You don't. It's automatically created. The problem is caused by your code not waiting long enough for the window to be completely created. Basically, you're trying to update the window before it exists. You can either delay starting your "Clock" class or, in the update code, check the Form's IsHandleCreated property before you try to update the Form.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      R 1 Reply Last reply
      0
      • D Dave Kreskowiak

        RJGCarey wrote:

        How does one create a handle for Form1?

        You don't. It's automatically created. The problem is caused by your code not waiting long enough for the window to be completely created. Basically, you're trying to update the window before it exists. You can either delay starting your "Clock" class or, in the update code, check the Form's IsHandleCreated property before you try to update the Form.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        R Offline
        R Offline
        RJGCarey
        wrote on last edited by
        #3

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dbgS As String bmpMain = New Bitmap(pbMain.Width, pbMain.Height) AddHandler pbMain.Paint, AddressOf pbMain_Paint bmpBars = New Bitmap(pbBars.Width, pbBars.Height) AddHandler pbBars.Paint, AddressOf pbBars_Paint instDelFormText = New delFormText(AddressOf FormText) Shw.SetUp() 'error handling is now available Try MySql.Open() Thread.Sleep(5000) dbgS = Me.IsHandleCreated Clock.Start() Catch ex As Exception Shw.Err("Form1_Load " & ex.Message) End Try End Sub Put in the sleep. It does sleep. put in dbgS. It Shows true. Same error. Thanks RCarey

        RCarey

        L 1 Reply Last reply
        0
        • R RJGCarey

          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dbgS As String bmpMain = New Bitmap(pbMain.Width, pbMain.Height) AddHandler pbMain.Paint, AddressOf pbMain_Paint bmpBars = New Bitmap(pbBars.Width, pbBars.Height) AddHandler pbBars.Paint, AddressOf pbBars_Paint instDelFormText = New delFormText(AddressOf FormText) Shw.SetUp() 'error handling is now available Try MySql.Open() Thread.Sleep(5000) dbgS = Me.IsHandleCreated Clock.Start() Catch ex As Exception Shw.Err("Form1_Load " & ex.Message) End Try End Sub Put in the sleep. It does sleep. put in dbgS. It Shows true. Same error. Thanks RCarey

          RCarey

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, putting a Sleep in the main thread won't help, it just stops the world for a while, nothing happens any more, no handles would get created in the mean time. Where is it failing? Look at the line numbers in the Exception, and watch the line numbers of your source code.(See my tips!) :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          R 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, putting a Sleep in the main thread won't help, it just stops the world for a while, nothing happens any more, no handles would get created in the mean time. Where is it failing? Look at the line numbers in the Exception, and watch the line numbers of your source code.(See my tips!) :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            R Offline
            R Offline
            RJGCarey
            wrote on last edited by
            #5

            Here's my code. This code is in my 15+ other applications and it works all the time. Form1 Private Sub Form1_Load instDelFormText = New delFormText(AddressOf FormText) End Sub Public instDelFormText As delFormText Public Delegate Sub delFormText(ByVal inText As String) Private Sub FormText(ByVal inText As String) Try Me.Text = inText Catch ex As Exception Shw.Err("frmMain.WriteFormText " & ex.Message) End Try End Sub Module Clock Private Sub DoNewSecond() Try Form1.Invoke(Form1.instDelFormText, CObj("BarHunter 2005" & Space (100) & Now.ToString)) Catch ex As Exception Shw.Err("Clock.DoNewSecond " & ex.Message) End Try End Sub The failure occurs on Form1.Invoke etc Thanks RCarey

            RCarey

            L 1 Reply Last reply
            0
            • R RJGCarey

              Here's my code. This code is in my 15+ other applications and it works all the time. Form1 Private Sub Form1_Load instDelFormText = New delFormText(AddressOf FormText) End Sub Public instDelFormText As delFormText Public Delegate Sub delFormText(ByVal inText As String) Private Sub FormText(ByVal inText As String) Try Me.Text = inText Catch ex As Exception Shw.Err("frmMain.WriteFormText " & ex.Message) End Try End Sub Module Clock Private Sub DoNewSecond() Try Form1.Invoke(Form1.instDelFormText, CObj("BarHunter 2005" & Space (100) & Now.ToString)) Catch ex As Exception Shw.Err("Clock.DoNewSecond " & ex.Message) End Try End Sub The failure occurs on Form1.Invoke etc Thanks RCarey

              RCarey

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, there is less code now than there was before in Form1_Load. The Form1 constructor is missing, the Clock stuff is not complete. Where do you start the clock? I would do it as the last thing in Form1_Load :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              R 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, there is less code now than there was before in Form1_Load. The Form1 constructor is missing, the Clock stuff is not complete. Where do you start the clock? I would do it as the last thing in Form1_Load :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                R Offline
                R Offline
                RJGCarey
                wrote on last edited by
                #7

                My last post was just an abstract. The post prior to that has all the code. Starting the clock is the last thing in Form_Load. In my other applications the form is created in Sub Main. Here is the code. Public Sub Main() frmMain = New Form1 'new form frmMain.Show() 'show it Shw = New clsShow 'new class Shw.SetUp() 'set up the graphics cName = SystemInformation.ComputerName() 'get computer name handleEndWorking = Now.ToString("yyyyMMdd") 'set the handleendworking mySql = New clsMySql 'connect to the database hntB = New BarHunter.clsBase 'new hunting base class hntB.setUp = New BarHunter.clsSetUp 'new set up class hntB.setUp.DoIt() 'do the job hntB.setUp = Nothing 'dispose Application.Run() 'run End Sub Now I just discovered that when I run my Show Procedures from another thread it doesn't work. It modifies a bitmap linked to a picture box on the form and then invalidates that picturebox. Enable application framework sure creates a different critter. Thanks RCarey

                RCarey

                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