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