Solved

SWITCH FORMS AT PANEL

Posted on 2011-03-19
9
443 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

Expert Comment

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

    Me.pnlForm.Controls.Clear()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 86

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 86

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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