UserControls and Panels

S

Søren Dreijer

Hi,

I realize this is a very common problem, but even though I've scoured the
Internet for resources, I can't seem to solve this issue :|

I currently have a very simple application. It contains a a single main
dialog in which all user interaction occurs. The dialog consists of "views",
that is panels, which contain various controls. Only one panel is shown at a
time. When the user has finished working on one panel, the next should be
shown.

As you probably know, designing all the panels in the main dialog is a pain
as they lie on top of each other.
That's why I instead decided to use UserControls and use a single panel on
the main form as a placeholder.

When the application starts up, the following code is executed and the first
view is shown:

// 'mainPanel' is the placeholder on the main form
// 'View1' is the UserControl
mainPanel.Controls.Clear();
mainPanel.Controls.Add(new View1());

Now, when the user wishes to proceed to the next view, he presses, say, a
button on the view's form (not on the main form).

My question is, how does the view update the main form's panel? I know there
are properties to access the parent form from the UserControl, but so far
I've had no luck accessing the main form's panel :|

Thanks!
 
G

Guest

One of the easiest ways to solve this is to pass an instance of the main form
to the UserControl in its constructor , e.g.

MyView vw = new MyView(this);


......//control:
private Form _mainForm;
Public MyView (Form frm)
{
this._mainForm=frm;
}


Now you have an instance of the parent form right inside your control. Fun,
heh?
Peter
 
I

Ian Semmel

This may not be of help, but you can use MDI frame as your main form and then
normal forms for the rest.

To go from one form to the next, close the current form, open the new form and
display it maximized.

Something like

private void OpenNewForm()
{
CloseCurrentForm();
newForm = new NewForm();
newForm.WindowState = FormWindowState.Maximized;
newForm.Show();
}
 
S

Søren Dreijer

Yeah, I know, but the point was not to have the buttons on the main form (as
in a wizard) but on the sub-forms instead.
 
S

Søren Dreijer

Ah, why didn't I think of that! Easy and straightforward :|

Thanks a bunch :]
 
S

Søren Dreijer

Short follow-up question:
What is the preferred way to let the children forms gain access to the
placeholder panel on the main form? Should I allow direct access to the
variable (public scope) or should I instead make a couple of accessor
functions (excuse the C++ term) ?



Søren Dreijer said:
Ah, why didn't I think of that! Easy and straightforward :|

Thanks a bunch :]

Peter Bromberg said:
One of the easiest ways to solve this is to pass an instance of the main
form
to the UserControl in its constructor , e.g.

MyView vw = new MyView(this);


.....//control:
private Form _mainForm;
Public MyView (Form frm)
{
this._mainForm=frm;
}


Now you have an instance of the parent form right inside your control.
Fun,
heh?
Peter


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
S

Søren Dreijer

Yeah, I thought of that, but I wasn't sure how one does that when dealing
with, say, a panel. It's not like you do any assignment operations or
similar. You just use one of its methods or properties.

Thanks,

Otis Mukinfus said:
Short follow-up question:
What is the preferred way to let the children forms gain access to the
placeholder panel on the main form? Should I allow direct access to the
variable (public scope) or should I instead make a couple of accessor
functions (excuse the C++ term) ?
[snip]

Use Accessors (Properties) ;o)

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 

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

Top