StartUp Form, still no handle

HI!

If I do this frmMain.Show from a tcpsocket class I get this

The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone)'

If I do this.

    Public Sub ShowForm()
        Dim wh As New MethodInvoker(AddressOf test)
        Me.Invoke(wh)
   End Sub

  Private Sub test()
        If Me.InvokeRequired Then
            Me.Invoke(New ShowServerConsoleDelegate(AddressOf test))
        Else
            Me.Opacity = 100
            Me.ShowInTaskbar = True
            Me.BringToFront()
            Me.Refresh()
        End If
    End Sub

I get

Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

So when having the frmMain as the "Startup form" does not the "framework" create the "handle" so I can use it without creating the form manually?
LVL 1
AWestEngAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
In your latter pseudo code you are accessing frmMain directly from the TcpSocket class which kinda defeats the purpose of the Invoke() calls.

I assume that since frmMain is the startup form...it is creating the instances of TcpSocket?  If so, make TcpSocket raise a custom event that frmMain subscribes to.  From the event handler, use your Invoke() code:

    Public Class TcpSocket

        Public Event ShowForm()

        Public Sub ProcessCommands()
            Select case cmd
                Case "OpenForm"
                    RaiseEvent ShowForm()
            End Select
        End Sub

    End Class

Then in frmMain:

    Public Delegate Sub ShowFormDlg()

    Public Sub ShowForm() Handles ts.ShowForm ' <--- or wire it up with AddHandler()
        If Me.InvokeRequired Then
            Me.Invoke(New ShowFormDlg(AddressOf ShowForm), New Object() {})
        Else
            Me.Opacity = 1
            Me.ShowInTaskbar = True
            Me.BringToFront()
            Me.Refresh()
        End If
    End Sub
0
 
KCFORSYTHECommented:
I had what I think is a similar issue trying to Invoke from within a service.  I had to DIM a form object and do the Invoke on that form object.   What I realized is that the handle does not get assigned unless I set the form.visible=true.    You might want to try doing a Me.Visible=True just before you do the Me.Invoke(wh).   It just seems to me as the same root cause that I ran into.

Good luck,

Kent
0
 
KCFORSYTHECommented:
On other thing.....in my case, I did a Me.Visible=True and immediately followed it with a Me.Visible=False since I really did not want to see the form.   This forced the handle to be assigned though.

Kent
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You are using "Me"...but you haven't indicated WHERE this code is occuring.  Is it from inside frmMain?...or from an instance of your tcpsocket class?

If the latter, you need to raise an event from tcpsocket that is trapped in frmMain.  Then, from the main from (in that event), you can use your code with "Me".

On a side note, your code to set Opacity is wrong.  At DESIGN-TIME (in the IDE) this value ranges from 0 to 100....but at RUN-TIME this property is actually a single value that ranges from 0 to 1 (for instance, .5 would be 50% opacity).

0
 
AWestEngAuthor Commented:
The code above is in the frmMain.
Some more pseudo code
 When calling frmMain.Showform it will crash on invoke

Public Class TcpSocket
 
Public Sub ProcessCommands()
    -------
   Select case cmd
       Case "OpenForm"
        frmMain.ShowForm
  End Select
  -----
End Sub
 
  -------
End Class
 
 
Public Class frmMain
 
   Public Sub ShowForm
      If Me.InvokeRequired Then
          Me.Invoke(New ShowServerConsoleDelegate(AddressOf test))
      Else
            Me.Opacity = 100
            Me.ShowInTaskbar = True
            Me.BringToFront()
            Me.Refresh()
       End If
    End Sub
 
End Class

Open in new window

0
 
AWestEngAuthor Commented:
thx for some inputs
Ok then I can do the .show stuff, but it feels there should be another "more correct" way to do this?
0
 
AWestEngAuthor Commented:
one other problem is that if I raise an event in the Socketclass and trap in frmMain I also get an exception, if this method has a invoke in it. and it feels it also has something to do with the handle stuff.
I will try the anser you posted in my other PAQ Idle_Mind
  Me.Invoke(New UpdateListviewDelegate(AddressOf UpdateListview), New Object() {message, type, showdate})

http://www.experts-exchange.com/Programming/Languages/.NET/Q_24145802.html
 
0
 
AWestEngAuthor Commented:
You did it again,, :)  thx Idle_Mind ... A perfect solution..
0
 
AWestEngAuthor Commented:
PERFECT!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.