• 0

[.NET] Multi-Panel Application?


Question

Hi,

I've made a simple application in VB.NET that switches between two different panels, but I'm not sure if it's the most efficient way to do it. Please see the below code and compile the file and let me know if there's any better way to do it. Thanks!

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Panel2 As System.Windows.Forms.Panel
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Label1 = New System.Windows.Forms.Label
        Me.Panel2 = New System.Windows.Forms.Panel
        Me.Label2 = New System.Windows.Forms.Label
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.MenuItem3 = New System.Windows.Forms.MenuItem
        Me.Panel1.SuspendLayout()
        Me.Panel2.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.SystemColors.ActiveCaption
        Me.Panel1.Controls.Add(Me.Label1)
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel1.Location = New System.Drawing.Point(0, 0)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(292, 271)
        Me.Panel1.TabIndex = 0
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Verdana25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(96, 88)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "This Is Panel 1"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Panel2
        '
        Me.Panel2.BackColor = System.Drawing.SystemColors.Info
        Me.Panel2.Controls.Add(Me.Label2)
        Me.Panel2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel2.Location = New System.Drawing.Point(0, 0)
        Me.Panel2.Name = "Panel2"
        Me.Panel2.Size = New System.Drawing.Size(292, 271)
        Me.Panel2.TabIndex = 1
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Verdana25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(32, 48)
        Me.Label2.Name = "Label2"
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "This Is Panel 2"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3})
        Me.MenuItem1.Text = "Change Panel"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 0
        Me.MenuItem2.Text = "Panel 1"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 1
        Me.MenuItem3.Text = "Panel 2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 271)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.Panel2)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.Panel1.ResumeLayout(False)
        Me.Panel2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        Panel1.Visible = True
        Panel2.Visible = False
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        Panel1.Visible = False
        Panel2.Visible = True
    End Sub
End Class

Once again, thanks in advance!

Link to comment
https://www.neowin.net/forum/topic/379148-net-multi-panel-application/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

If the panels appear one over the other (that is, they have the same size and location) then you should consider changing their zorder... like this:

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
  Panel1.BringToFront()
End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
  Panel2.BringToFront()
End Sub

  • 0
  bangbang023 said:
You could simply do a panel1.bringtofront() command. That should make things a little easier and cleaner if you start having many panels. I believe that command should still work.

586600388[/snapback]

Yeah either of those should work fine.

  • 0
  syko86 said:
Thanks for the replies =D. Is there any way to optimize it? I've an application that has 3 different panels and it takes up like 15MB of memory while running =(.

586600853[/snapback]

That's simply how .net works and while users will bitch and moan, it's nothing to worry about. The framework reserves memory for itself so it's readily accessible when needed, however, if another application requests any of that unused memory, the .net framework will immediately release it and let the other process use it.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Impressive Microsoft has done this without saving for Windows 12. Pretty sure this the first time they’ve done an in-version start menu revamp (Windows 8 tinkering aside)? 🤔
    • I use the start menu the same way but what it comes down to is that wasn’t what was intended, it works like that and I appreciate it, but there is no need for all the unnecessary cruft that comes along with it for that. Look how simple Spotlight is on macOS to perform the same function. If I could kill off the start menu from running altogether, I’d be perfectly content because I could use Powertoys Run. Then it comes down to that not everyone uses it the same way you and I do.
    • No major Siri upgrade announced at WWDC 2025 as Apple confirms delay until next year by Sagar Naresh Bhavsar Apple has wrapped up this year's Worldwide Developers Conference (WWDC) 2025 keynote. The company announced a bunch of new updates for its operating systems, services, and software. We also got Apple skipping the number "19" across its software version and instead jumping to "26" for a unified naming. So, now Apple software is known as iOS 26, tvOS 26, watchOS 26, macOS 26, and iPadOS 26. iOS 26 received a major makeover, which Apple touts as the biggest redesign since iOS 7 in 2013. iOS 26 now brings a "glass-like" design called "Liquid Glass," most noticeable on the lock screen and home screen. Then there is a much-talked-about "Call Screening" feature, which shows information about the caller and lets users decide if they want to pick up calls or not. Amidst all the changes to iOS 26 and other operating systems, Apple also touched upon the highly anticipated Siri upgrades announced during last year's WWDC. Back in WWDC 2024, Apple announced that the new AI-powered Siri upgrade that would understand personal context would arrive by fall 2024. However, the roll out was delayed and was suggested to arrive in spring of 2025, with the beta expected to arrive in January 2025. But that also did not happen. Now, at WWDC 2025, Apple said that details about the AI-powered Siri upgrade will be shared in the coming year. Here's what Apple's software engineering chief Craig Federighi said: This only means that the Siri upgrade isn't ready for launch, and won't be a part ofthe iOS 26, iPadOS 26, and macOS 26 developer betas. Also, the phrase "coming year" doesn't offer a concrete timeline, but it does suggest that we will likely hear more about the Siri upgrade in 2026.
    • Here's which iPhone models support iOS 26
    • ive got the orginal nothing by cmf and i cant fault the phone except for no nfc on the model i bought. still zippy, updated regulary good solid phone
  • Recent Achievements

    • Explorer
      MusicLover2112 went up a rank
      Explorer
    • Dedicated
      MadMung0 earned a badge
      Dedicated
    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
  • Popular Contributors

    1. 1
      +primortal
      501
    2. 2
      ATLien_0
      268
    3. 3
      +FloatingFatMan
      256
    4. 4
      +Edouard
      202
    5. 5
      snowy owl
      169
  • Tell a friend

    Love Neowin? Tell a friend!