[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Make forms invisible

I have a VB.NET application with a MDI enabled form containing several child forms. At any time several of the child forms may be open with data that must be saved before the forms are closed and set to Nothing.

My question is:
Is it possible to temporally make some of the child forms invisible to the user without losing those forms data or having to save to avoid losing data. When the forms are brought back they and it's data must be in the same state as when they become invisible.

By invisible I mean that the forms can not be seen (unreachable) by the user and can not be brought back by just pressing the min/max buttons up right in the MDI container. The code to set forms (in)visible must be located in it’s own sub.
0
daghoff
Asked:
daghoff
1 Solution
 
elimesikaCommented:
There is a trick to do that by moving the form to unreachable position.

See sample code at
http://www.justvb.net/tips/Forms/Forms_MDI_-_Hiding_MDI_Child_Forms_at_Run_Time.html
0
 
daghoffAuthor Commented:
Nice, but it looks a bit dirty. Can I be certain it will be supported in future versions of .NET?

Is there no better way?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
?...

Why not just set the Visible() property of your MdiChildren to True/False?  
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2
        f2.MdiParent = Me
        f2.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ChangeVisibility(False)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ChangeVisibility(True)
    End Sub

    Private Sub ChangeVisibility(ByVal VisibleState As Boolean)
        For Each frm As Form In Me.MdiChildren
            frm.Visible = VisibleState
        Next

        ' if you only want certain kinds, then do something like:
        For Each frm As Form In Me.MdiChildren
            If TypeOf frm Is Form2 Then
                frm.Visible = VisibleState
            End If
        Next
    End Sub

End Class

Open in new window

Idle-Mind-512471.flv
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
durgalakshmieCommented:
You can try minimizing the Form temporarily, once the control moves out of the Form
0
 
CodeCruiserCommented:
IM has nailed it. You can also use the Hide() method to hide the form and Show() method to show it again :-)

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.hide.aspx
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm guessing VB6 couldn't do this?...I can't remember.
0
 
VipulKadiaCommented:
try this : Yourform.Hide
0
 
daghoffAuthor Commented:
My problem is that I keep forgetting that forms are just another control.

I have now implemented a MDI test app with a lot of child forms. I have two groups of child forms and a button that sets one group invisible and the other visible.

The problem now is that during the switch the child forms flicker alott. I have tried SuspendLayout and ResumeLayout on the MDI, hoping it would control the flickering of the children, but with no luck.

Can this be solved?

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Use SendMessage() and WM_SETREDRAW on the MdiClient of your MdiParent form.

Here's a simplified example:
Public Class Form1

    Private MyMdiClient As MdiClient = Nothing

    Private State As Boolean = False
    Private Group1 As New List(Of Form)
    Private Group2 As New List(Of Form)

    Private Const WM_SETREDRAW As Integer = &HB

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal handle As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is MdiClient Then
                MyMdiClient = ctl
                Exit For
            End If
        Next

        For i As Integer = 1 To 20
            Dim frm As New Form
            frm.MdiParent = Me
            frm.Text = If(i Mod 2, "Group1", "Group2")
            If i Mod 2 Then
                Group1.Add(frm)
            Else
                Group2.Add(frm)
            End If
            frm.Show()
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendMessage(MyMdiClient.Handle, WM_SETREDRAW, False, 0) ' Turn OFF MdiClient Updates

        ' Toggle the state of your MdiChildren somehow:
        State = Not State
        For Each frm As Form In Group1
            frm.Visible = State
        Next
        For Each frm As Form In Group2
            frm.Visible = Not State
        Next

        SendMessage(MyMdiClient.Handle, WM_SETREDRAW, True, 0) ' Turn ON MdiClient Updates
        MyMdiClient.Refresh()
    End Sub

End Class

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now