Windows Forms Question


Scott Stark


I'm *just* delving into Windows forms-based programming without the benefit
of any books, etc. I have a background in light ASP.NET work, so forgive me
if this is a really basic question related to Windows GUI.

I've created a "Form1.vb" file in my project, added a menustrip, etc. When a
user clicks on a menu item, how do I get it to show a new form in the same

I'm basically asking how to create a Windows forms application with more
than 1 form. :) All the examples on the internet of this type of programming
typically show one form.

I'm trying to understand how to create a standard UI (i.e. window size,
title, menustrip, etc) and then change the content of the form based on what
the user wants to do.

Thanks. :)




James Hahn

There's a good discussion here:
Working with Multiple Forms in Visual Basic .NET: Upgrading to .NET

However, your last paragraph seems to contradict the approach described
there - opening different forms (with different content) is one way of
allowing your user to perform different tasks. Changing the content of the
current form is a different way of allowing your user to perform different
tasks. Of course, you can use both procedures within the one application,
but they are different programming techniques.

If you are talking about using a form template so that you are using
different forms but with a consistency in a number of basic controls on the
form, and you don't want to be separately adding and tweaking those repeated
controls, then you are talking about subclassing the form. See
Windows Forms Inheritance

Family Tree Mike

At first I thought you were asking how to make a form similar to MS Access,
where many forms are initated inside the main form. Later it seems to me
that you are describing something more like MS Outlook, which changes
its contents based on what the user wants (email lists, contacts, calendar).

Which is closer to what you are after?

Scott Stark

Hi Mike,

Thank's for the response. I'm talking about more of an Outlook-style
functionality. If the user chooses something from the left-hand menu, the
"content" pane changes to that particular form.


Scott Stark

Hi James,

Sub-classing seems to be more along the lines of what I'm looking for. I'll
look into that, but it seems that then I'll be able to make Form1 and Form2
inherit the base controls from, say "BaseForm" and then add the required
functionality to Form1 and Form2.

However, how do I get Form2 to display when the user is looking at Form1?
Form2.Show() would open a new window? I want to "replace" Form1 with Form2
in the same window, if that makes sense.

James Hahn

Like I said, your second paragraph contadicts your description of what you
are trying to do.

If you subclass a form then you create a new form, with its own design and
functionality, and open and close that form as needed. This can be done as a
selection by the user from a controlling form (dashboard-style) or it can be
in a rotating format, where the user moves from one form to the the next in
a sequence, returning to the start after having completed the task. Because
the forms are subclassed they can be set up to have some predefined
components that are identical from form to form and are maintained at one
location only - the class for the form. You can use modality to require the
user to work on one form at a time, if appropriate. But each task is carried
out in its own form.

The format you are describing involves using one form only, with some
components that are fixed in place and are always visible and enabled and
some components that come and go according to the current task. This is not
subclassing - there is only one form. You are simply dynamically enabling,
disabling, hiding and moving controls on the form to present a form layout
that is relevant to the current task.

I suppose you could do this by using multiple forms, setting them up to have
some common components, and forcing each form to open with the size and
position of the form from which it was started, hiding the startup form each
time. This would fool the user into thinking there was only one form and the
contents simply changed slightly depending on the task. But it seems a bit
pointless to set the project up as multiple forms and then go to all that
trouble to make it look like there's only one.

Or, perhaps you are talking about an MDI form.
MDI Changes in Visual Basic .NET



Family Tree Mike

Here is code that I am using to control the right panel of a split container
based on an item in a treeview selected in the left panel. The form
contained in the right panel depends on what type of node was selected.
Hopefully this gets you started. The description of "s" should not be too
important other than there is a Type property which the form type needs to
be based upon.

GEDCOM.GEDCOM_55.Section s = (GEDCOM.GEDCOM_55.Section)e.Node.Tag;
if (s == null) return;

if (s.Type == "INDI")
IndividualUI ui = new IndividualUI();
ui.TopLevel = false;
ui.Dock = DockStyle.Fill;
else if (s.Type == "FAM")
FamilyUI ui = new FamilyUI();
ui.TopLevel = false;
ui.Dock = DockStyle.Fill;

Jack Jackson

A form is a window. You can display different content in a form, or
you can create more forms and display them. There is no concept of
replacing one form with another, except for MIDI child windows where
you show one or more child windows in a containing parent window.

The easiest way to display different content in a form is to have two
or more containers (e.g. Panel) in a form, and make one visible at a

Family Tree Mike

Just to clarify, a panel can be a container for a form. This lets you use
the form as both a tradtional form and as something like a user
control or panel within the form. My example in this thread shows
how to dock a form in the panel.

When you dock a form in a panel, you should set the border to none,
and I see I forgot that in my other post.




Hi Scott:
I'm not the smartest in VB.Net, but one way if you want to replace form1
with form2 is when you set the button on form1 to show form2, you can also
set it to close form1 as well. and if you want form1 to show again when
closing form2, you can do the same thing. show form1 and close form2.

Private Sub btnShowForm2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button13.Click
End Sub

hope this helps.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question