Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

This article gives an example how to code if you wants to handle two or more forms in a session with a thread for each session. I have a working, but dirty solution. Mabye someone have a better solution

 

I have a form named frmMain, which can open one or more "sessions". For each session, frmMain starts a new thread, and this thread opens a new window (frmSessionFormA) with showDialog. I use showDialog instead of show, because I want to handle errors occurred in a session with a try-catch in frmMain the code look like this:

 

Try
  frmSessionFormA.ShowDialog()
Catch ex As Exception
  'errorhandling
End Try

 

The frmSessionFormA have a "Next"-button, which will hide frmSessionFormA and show frmSessionFormB instead. Here is the problem: If I use frmSessionFormA.hide or frmSessionFormA.visible = false, the frmSessionFormA closes! (the whole session ends). Even if I try to cancel the closing in frmSessionFormA's Sub OnClosing, the form closes anyway.

 

However I have a solution, a very dirty one. Instead of frmSessionFormA.visible = false, I use

frmSessionFormA.opacity = 0

frmSessionFormA.showInTaskBar = False

 

When showInTaskBar changes value, frmSessionFormA tries to close, but then, the prevention in Sub OnClosing works. When I wants to close frmSessionFormB, and open frmSessionFormA again, I use the same procedure and add frmSessionFormA.bringToFront

 

Have anyone else encountered similar problems? Is there a better way of solving it?

 

Code to hide frmSessionFormA and open frmSessionFormB:

(me = frmSessionFormA)

frmSessionFormB.Show()
preventClosing = 2 
Me.ShowInTaskbar = False
Me.Opacity = 0

 

Code to prevent frmSessionFormA from closing

    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
       If preventClosing > 0 Then
           e.Cancel = True
           If preventClosing = 1 Then
               Me.BringToFront()
           End If
           preventClosing -= 1
       End If
   End Sub

 

Code to close frmSessionFormB, and open frmSessionFormA again:

Me.Dispose()
frmSessionFormA.ShowInTaskbar = True
frmSessionFormA.Opacity = 100

// Hugo

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...