Quest: Problem with a shared panel between to forms...

B

bj7lewis

I am trying to setup a Windows Form app using C# in VS.NET 2003 that will
display 1 or 2 forms but both must shared a set of controls that interact
with shared data 100% the same way...

Here is what it looksed like before...

Main Form(Smaller Size of the 2)
--------------------------------
| | |
| It's | Shared |
| Data | Data |
| Controls | Controls |
--------------------------------

Child Form(But is Maximize)
--------------------------------
| |
| It's |
| Data |
| Controls |
--------------------------------

But this did work good but when I use the app as an user I ended up part way
thur using Child Form and I needed to close Child Form to go back to Main to
edit Shared data controls to update the data before I forget what I need to
change then reopen Child Form to continue where I was... This could also
repeat several time while app is opened so I desided to try this...

Main Form(Smaller Size of the 2)
--------------------------------
| | |
| It's | Shared |
| Data | Data |
| Controls | Controls |
--------------------------------

Child Form(But is Maximize)
--------------------------------
| | |
| It's | Shared |
| Data | Data |
| Controls | Controls |
--------------------------------

I have two forms(Main, Child) and a panel/usercontrol to hold
Shared Data Contols so when app opens main form *_Load() calls
Controls.Add() and assocates Shared Data Contols panel/usercontrol to main
form and Docking setting sets the docking and when child form
is invoke Controls.Remove() is called and Controls.Add() of child form is
call and again docking is fixed to expand to max...

Trying this I can Controls.Add() then Controls.Remove() the shared data
panel on the Main Form all I want but when I call
ChildForm.Controls.Remove() this set the panel visibility to false and below
doesn't change it to true... Note: Nowhere else in project is panel ever
hidden except this adding and removing and when form is closed...

ChildFormObj.Controls.Add(Shared DataContolsPanel.Shared
DataContolsPanelObj);
Shared DataContolsPanel.Shared DataContolsPanel.Visible = true;
MessageBox.Show(Shared DataContolsPanel.Shared
DataContolsPanel.Visible.ToString());

This Shows "False"...

What going on here... Why isn't "....Visible = true;" line working and
giving the wrong value to MessageBox.Show()...

Any help here...
 
C

Charles Crawford

I would begin by creating a user control with the Shared Data Controls.
Then, I would include an instance of this user control on each of the forms
you want them to appear on.

Say you create a user control called 'sharedcontrols.cs'.

You can then drag the control from the Solution Explorer onto the forms you
wish to have the common set of controls.

You can write properties for the controls within the code portion of the
user control as follows:

add private member variables to your code:

Private int _myInt;

and then the properties:

Public int myInt
{
get
{
return _myInt;
}
set
{
_myInt = value;
<insert code to perform whatever action you wish to take
once the value has been set...
}
}

Alternatively, you can change the modifiers on the individual controls you
wish to expose to 'Public' through the properties dialogue in design view.

Hope this helps,

Charlie
 
B

bj7lewis

oh, please ignore my previous post... I misread your post.
Yes that would be my 2nd new option but that way requires data sysc between
the to shared data panel so my 1st is to have shared data form the is a
sibling to Main and Child form...

Any more help here...
 
B

Bruce Wood

bj7lewis said:
I am trying to setup a Windows Form app using C# in VS.NET 2003 that will
display 1 or 2 forms but both must shared a set of controls that interact
with shared data 100% the same way...

<snip/>

I have two forms(Main, Child) and a panel/usercontrol to hold
Shared Data Contols so when app opens main form *_Load() calls
Controls.Add() and assocates Shared Data Contols panel/usercontrol to main
form and Docking setting sets the docking and when child form
is invoke Controls.Remove() is called and Controls.Add() of child form is
call and again docking is fixed to expand to max...

I believe that you're thinking about this problem the wrong way.

Making the shared data panel a UserControl is the right idea. The wrong
idea is that you have a "shared panel". You don't have a shared panel.
You have shared data. More to the point, you have _two_ (or more)
panels that are the same user control but share data.

I have done the same thing many times. Here is how I did it.

The first (and most important point) is that the UserControl the
contains the display that will be duplicated (not shared) _does not
store the data_. You need to create another class to hold the data.
Let's call it the Model.

The Model is really smart. It stores the data, and it also knows when
each field on the user control should appear / disappear in response to
the contents of other fields, etc., which buttons should be enabled,
etc.

The UI UserControl communicates with the Model by calling its methods
and querying its properties.

The Model communicates with the UI UserControl via events.

So, let's say that you have a field on your shared data called
CustomerName. The Model would have these properties / events:

public string CustomerName { ... }

public event System.EventHandler CustomerNameChanged;

public bool CustomerNameRelevant { ... }

public event System.EventHandler CustomerRelevantChanged;

The UI component, then, has a property like this:

public Model Model { ... }

Whenever a new Model is set, the UI queries its properties and sets the
fields on the UI to contain the right stuff. It also subscribes to all
of the Model events in which it's interested.

A lot of work, but now check this out. In MainForm, you just put a copy
of the UserControl on the form in the usual way, and then in the
constructor or the Load event handler you say:

this._sharedDataPanel.Model = MainForm.CommonModel;

and, in the Child form, you do the same:

this._sharedDataPanel.Model = MainForm.CommonModel;

Now you have _one model_ with _one_ set of data shared by two user
controls. Whenever you change a field on the child form, it will update
the shared data on the main form as well. Whenever you change a field
on the main form, it will update the child form.

The Child form doesn't even have to be a dialog of the Main form. You
can have both of them active at once, the user flipping between them,
and the data will update across the forms seamlessly.

You may also want to read up on the Model-View-Controller (MVC) pattern
(of which my design above is a bastardized form).
 
B

bj7lewis

Yes that would be my 2nd new option but that way requires data sysc
between the to shared data panel so my 1st is to have shared data form the
is a sibling to Main and Child form...
Ok I Had some time to back up the failing panel version and try A shared
data form version and this is working great...

Thanks all for help...
 
B

bj7lewis

Thanks Bruce Wood but I been working on a new version of this this time
Shared Data Control are all on it's own form whose parent is the desktop and
this is working great...
I believe that you're thinking about this problem the wrong way.

Making the shared data panel a UserControl is the right idea. The wrong
idea is that you have a "shared panel". You don't have a shared panel.
You have shared data. More to the point, you have _two_ (or more)
panels that are the same user control but share data.
Correct I was using term "panel" loosely/wrong...
The Model is really smart. [Snip]... [and]
Whenever a new Model is set, the UI queries its properties and sets the
fields on the UI to contain the right stuff. It also subscribes to all
of the Model events in which it's interested. [and]
A lot of work, but now check this out. In MainForm, you just put a copy
of the UserControl on the form in the usual way, and then in the
constructor or the Load event handler you say:
Wow a lot of work went into your reply and some above me by the wording(my
bad not your) of reply but since I got the form version working I will stick
to it... Sorry but thanks...

Thanks to all...
 

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