Solved

SWITCH FORMS AT PANEL

Posted on 2011-03-19
9
437 Views
Last Modified: 2012-06-27
Hi All,

I try to show forms at panel.

Here are the controls:

1. mdiform
2. pnlMenu as menu to select Menu use picturebox inside this panel.
3. pnlForm to show form inside it.

The problem is how to switch form among shown form at pnlForm when menu a pnlMenu is click ?

Is there any better idea to show forms inside a control for the same purpose ?

Thank you.
Private Sub Show_Bank()

        Dim frmBank As New frmBank

        frmBank.TopLevel = False
        Me.pnlForm.Controls.Add(frmBank)

        With frmBank
            .WindowState = FormWindowState.Maximized
            .Text = "SAMPLE FORM BANK"
            '.Dock = DockStyle.Fill
            .Show()
        End With

    End Sub

Open in new window

0
Comment
Question by:emi_sastra
  • 5
  • 3
9 Comments
 
LVL 9

Expert Comment

by:Hawkvalley1
ID: 35174114
Maybe instead of a form you use a UserControl and add that to the panel. You can have all the same functionality.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35174118
Hi Hawkvalley1,

I don't get it.

Thank you.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35174200
Just clear the Panel before you add the new form...

    Me.pnlForm.Controls.Clear()
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35174211
Hi Idle_Mind,

Suppose there are 2 forms.

1. Cash
2. Bank

The Bank form is firstly Shown and user already input something but not saved yet.
Then user show form Cash.

Then use switch to form Bank, what will be happened to the data at form Bank that is in the middle of editing ?

Thank you.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35174220
If you need them to keep state then move their declarations out to the CLASS level:
Private frmBank As New frmBank ' <-- add it here instead

    Private Sub Show_Bank()
        ' Dim frmBank As New frmBank <-- get rid of this one

        Me.pnlForm.Controls.Clear()
        With frmBank
            .TopLevel = False
            .WindowState = FormWindowState.Maximized
            .Text = "SAMPLE FORM BANK"
            '.Dock = DockStyle.Fill
            .Show()
        End With
        Me.pnlForm.Controls.Add(frmBank)
    End Sub

Open in new window


...doing a similar thing with the Cash form.

Do the forms have BorderStyle set to None?  If not, and they have the 'X' on them still, then we need to add additional code.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35174226
Interesting, let me try first.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35174239
I've tried it.

There are several problems:

1. The form .WindowState = FormWindowState.Maximized is not work.
2. When I close a form and reopen it then rise error "Cannot access a disposed object. Object name: 'frmKas'."

Thank you.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35175342
Follow this pattern for all of the Forms that need to appear in your Panel:
Public Class Form1

    Private WithEvents frmBank As frmBank = Nothing
    Private WithEvents frmCash As frmCash = Nothing

    Private Sub Show_Bank()
        If IsNothing(Me.frmBank) Then
            Me.frmBank = New frmBank
            Me.frmBank.TopLevel = False
            Me.frmBank.Text = "SAMPLE FORM BANK"
            Me.pnlForm.Controls.Add(Me.frmBank)
        End If

        Me.frmBank.WindowState = FormWindowState.Maximized
        Me.frmBank.Show()
        Me.frmBank.BringToFront()
    End Sub

    Private Sub frmBank_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles frmBank.FormClosed
        Me.frmBank = Nothing
    End Sub

    Private Sub Show_Cash()
        If IsNothing(Me.frmCash) Then
            Me.frmCash = New frmCash
            Me.frmCash.TopLevel = False
            Me.frmCash.Text = "SAMPLE FORM CASH"
            Me.pnlForm.Controls.Add(Me.frmCash)
        End If

        Me.frmCash.WindowState = FormWindowState.Maximized
        Me.frmCash.Show()
        Me.frmCash.BringToFront()
    End Sub

    Private Sub frmCash_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles frmCash.FormClosed
        Me.frmCash = Nothing
    End Sub

End Class

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35177412
Hi Idle_Mind,

It works great.

Just a small change from

Me.pnlForm.Controls.Add(Me.frmCash)
to
frmCash.Parent = pnlForm

Thank you very much for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now