Why all instances share the same window?

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

Hello, friends,

I created a class library (c#.net 2005) in which it may instanciate multiple
window forms to display images: One window for each image file.

It worked fine except that if more than one instance were created, all
instances will share the same window although you can see multiple icons in
task bar. And here is source code we called to instantiate each window.

public bool ShowImage(string imageFileName, string viewerCaption)
{
ImageWindow newImageWindow = new ImageWindow ();
newImageWindow.Text = viewerCaption;
newImageWindow.ImageLocation = imageFileName;
newImageWindow.Show();
return true;
}

This is really weired. Any ideas?

Thanks a lot
 
Andrew said:
I created a class library (c#.net 2005) in which it may instanciate multiple
window forms to display images: One window for each image file.

It worked fine except that if more than one instance were created, all
instances will share the same window although you can see multiple icons in
task bar. And here is source code we called to instantiate each window.

public bool ShowImage(string imageFileName, string viewerCaption)
{
ImageWindow newImageWindow = new ImageWindow ();
newImageWindow.Text = viewerCaption;
newImageWindow.ImageLocation = imageFileName;
newImageWindow.Show();
return true;
}

This is really weired. Any ideas?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Hi, Jon,

I thought I found the problem.

In the property window of the form declared in this class library, I had a
property binding in ApplicationSetting for Location. If I took out this
binding and put it back to (none), no more "sharing".

Actually, it was not sharing. Simply, when one window was moved, the
ApplicationSettings for Location of the rest windows would change. As a
result, .net moved all windows to the same location.

This ApplicationSettings/ProperyBinding puzzled me before. For instance,

Properties.Settings.Default.Save(); in Form_Closing() event

sometimes saved negative location value and users could not see the window
next time. Any good reference paper on how to use it?

Thanks.
 
Andrew said:
I thought I found the problem.

In the property window of the form declared in this class library, I had a
property binding in ApplicationSetting for Location. If I took out this
binding and put it back to (none), no more "sharing".

Actually, it was not sharing. Simply, when one window was moved, the
ApplicationSettings for Location of the rest windows would change. As a
result, .net moved all windows to the same location.

This ApplicationSettings/ProperyBinding puzzled me before. For instance,

Properties.Settings.Default.Save(); in Form_Closing() event

sometimes saved negative location value and users could not see the window
next time. Any good reference paper on how to use it?

Very odd - no, I'm afraid I don't know any good references :(
 
Andrew wrote :

"Properties.Settings.Default.Save(); in Form_Closing() event sometimes saved
negative location value and users could not see the window next time."

Hi Andrew,

Like (our deeply appreciated) J. Skeet, I'd appreciate seeing a brief, but
more complete, code example. And I would appreciate seeing your code that
demonstrates this move-one-move-all "entraining" effect on run-time created
"parentless" WinForms : just to understand what's going on, and think about
yet another dimension I haven't looked into yet.

Hypothesis : One possible source of the "negative location" value you
mention may be that when you create WinForms at run-time ... even though
they are NOT child forms of the main application window (i.e., have their
'Parent property set to the creating main form) ... minimizing the Main Form
will also minimize the run-time created "parentless" forms.

I've recently needed to over-ride this "side-effect" in order to make
certain "parentless" forms persist on the screen when the main form in which
they were created was minimized. I've been able to do that without getting
into WndProc programming, although (our deeply appreciated) H. Wagner just
pointed me in that direction.

When a Form is minimized, you can examine the Location property X,Y values :
you'll see they are large negative numbers.

best, Bill Woodruff
dotScience
Chiang Mai, Thailand
 
Hello, Bill,

I think you are right. From my experience, in one scenario, the negative
location values could be saved when this form was minimized. (Still, this
should not happen and .net should take care of these invalid values).

As for the code, you may do this in c#.net 2005 to replicate this problem:

(1) Create a solution for WindowsForms application;
(2) Add a new class library project into solution;
(3) Add a new WindowForm in this class library project;
(4) In property window of this new added form, at
ApplicationSettings/PropertyBindings, bind properties Location and ClientSize;
(5) Back to WindowsForms project, and add this class library as its reference;
(6) In the form of WindowsForms project, add a button.
(7) In the button_click() event, add source code like this:

ClassLibrary1.Form1 frm1 = new ClassLibrary1.Form1();
frm1.Text = "First Form";
frm1.Show();

ClassLibrary1.Form1 frm2 = new ClassLibrary1.Form1();
frm2.Text = "Second Form";
frm2.Show();

(8) Run the app.
(9) Click on Button1.

You will see 2 windows overlap each other, and move together....Cool and
weired...
 
Back
Top