473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need Help with Multi Form handling

Since I am being challenged with learning c# I figured I could pass
some of the pain on to you guys :-)
I have another question(this one is important for me to fix before I
can get my app to Beta)

My app (an image viewer) opens with a Main form with a file explorer if
you open the program with the exe, but opens with the ImageView form if
you double click an image file, if you want to see the Main form once
the ImageView is open the menu has a Show Main option, and that works,
but if I click it again from the ImageView window it opens another
instance of Main form, and the same with when I open an image from the
file explorer every time I click another file it opens another
ImageView form instead of reusing the same one.

Can someone help point me in the right direction.
I appreciate all the help I have recieved, you guys rock.

Thanks,
Tim Geiges

Nov 16 '05 #1
8 2297
Hi Tim

I have been looking for an elegant solution to this for a bit... but I
really can't say I've found one.

However, I had to solve the same problem with an application of mine that
had MDI children. The way I tackled this was to go through all of the MDI
children of my form... check their names to see if I had allready open an
instance of a form with a specific name. If there was, I would use the
focus method of that form since we get a reference of the object from the
form's MDI children collection. If there wasn't, I create a new instance of
the required form.

I don't have any code off-hand, but if you are really interested I could go
and look at my old codes. I have not have a need for such a thing for a
while so my memory is a bit rusty.

hope that help!

"Tim Geiges" <ke***********@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Since I am being challenged with learning c# I figured I could pass
some of the pain on to you guys :-)
I have another question(this one is important for me to fix before I
can get my app to Beta)

My app (an image viewer) opens with a Main form with a file explorer if
you open the program with the exe, but opens with the ImageView form if
you double click an image file, if you want to see the Main form once
the ImageView is open the menu has a Show Main option, and that works,
but if I click it again from the ImageView window it opens another
instance of Main form, and the same with when I open an image from the
file explorer every time I click another file it opens another
ImageView form instead of reusing the same one.

Can someone help point me in the right direction.
I appreciate all the help I have recieved, you guys rock.

Thanks,
Tim Geiges

Nov 16 '05 #2
Does anyone have an example of how to check if a form is already open,
and not open a new one, and refresh the contents of the existing form,
so I can update the picture in the picturebox, instead of opening a new
window

Nov 16 '05 #3
Read up on the Singleton pattern. You want to do something similar,
although you can't use it precisely as it is, because Visual Studio
needs to instantiate a copy of your form in order to be able to show it
in the design window. So, you can use a "cheating" version of the
Singleton pattern in each applicable form:

public class ImageView : Windows.Forms.Form
{
private static ImageView singleton = null;

public static ImageView GetImageView()
{
if (ImageView.singleton == null)
{
ImageView.singleton = new ImageView();
}
return ImageView.singleton;
}

public void Dispose()
{
ImageView.singleton = null;
}

... rest of your form code...
}

Then you never say "new ImageView()" anywhere in your code... you just
call ImageView.GetImageView() whenever you want it. If it doesn't
already exist it will be created; if it does, you will be given the one
that already exists.

When you dispose of the form, it clears the "singleton" pointer so that
next time you call GetImageView() it will create a new ImageView form.

I'm not 100% certain about the Dispose code... whether it will be
invoked at the appropriate time. However, this is the basic idea.

You can do this in every form that you require to have only one
instance.

Nov 16 '05 #4
Bruce, Thank you, this works the way I want except the Dispose() part
does not get invoked when I close the current ImageView Window so I
can't open another image yet

anyone know how I can tell a form was closed so I can set
ImageView.singleton back to null so I can open the form again

Nov 16 '05 #5
Cool I fixed it by adding these to the ImageView Form.
now when I close an image I can open a new one.
Thanks again for all the help.
using System.ComponentModel;

protected override void OnClosing(CancelEventArgs e)
{
ImageView.singleton = null;
}

Nov 16 '05 #6
By the way, you can make your form a real singleton by making the
constructor private... I just realized that Visual Studio will still be
able to instantiate the form in design mode, even with a private
constructor. Then you'll be sure that no other form in your application
is bypassing the static Get... method.

Nov 16 '05 #7
Also is it bad form to maybe create an invisible listview in my second
form and have it populate itself, then use the data from that list
instead of just trying to read from the Main form?

My guess is that yes this is bad form, but this is the only way I can
think of that I know would solve my issue, but I'll wait to hear from
you guys, since I always get great help from this group

thanks again,
Tim

Nov 16 '05 #8
I"ve been thinking about this all weekend. :)

What you need is an implementation of the Model-View-Controller
architecture for your user interface. Don't panic: you don't need to go
whole hog and implement something tremendously sophisticated. Here is
what you shoud do, in a nutshell.

1. Separate your data—the list of images—from your user interface
handling logic. Make a separate class that holds your list of images
(and, logically, a class that holds information about each image).
Don't be concerned if, at the beginning, these classes seem pretty
thin. As you work through your design you will find various operations
that the classes should offer to your user interfaces (the main form
and the image viewer) that would make their jobs much, much easier. In
the end your "model" classes will probably be the biggest part of your
application.

2. Have your "model" (the list of images and the individual image
class) expose events that happen whenever something interesting happens
inside the model. So, for example, if your user changes the "current
image" in the main form, then it sets the "current" image in the image
list object, and that object then fires a CurrentImageChanged event.

3. Connect the events from the "model" classes to your screen controls.
There are two ways of doing this: simple-minded or sophisticated. For
now, I recomment simple-minded. It's more coding, but it's easier to
understand and easier to get working.

Simple-minded: Create event handlers in your forms that subscribe to
the events from your model. So, in your main form, whenever the model
fires a CurrentImageChanged event, maybe you highlight a different file
name (or maybe you do nothing, because it's already been taken care of
by the UI control). In your ImageViewer, whenever the model fires a
CurrentImageChanged event, change to display the new image. Then make
more event handlers in your forms to respond to user actions on the
screen controls (you probably already have those), and call method in
your "model" classes to take the action that the user wants done.

Sophisticated: You can do all of this with .NET's DataBinding classes,
which means you write much less code. Beware, though: DataBindings are
non-trivial and temperamental. That's why I recommend you start with
your own event handlers and hand-coding, for now.

This may seem like a lot of work, but the beauty of it becomes evident
when you consider this:

User clicks on main view, and clicks on a new image in the image list
in the main view. The event handler for the UI control picks up on the
mouse click, or SelectedIndexChanged, or whatever, and as a result sets
the models CurrentImage property. This causes the model to fire
CurrentImageChanged. Any other views you have open (probably just your
ImageViewer) are listening for this event, and their
...._CurrentImageChanged event handlers run. In the ImageViewer, this
causes the viewer to change the image that is currently showing.

You can expand this to as many views as you wish. From the user's point
of view, one action in one form can result in all of the views
changing, "like magic."
From a philosophical standpoint, what you're saying is this: all of my

forms (the main form and the image viewer) are just different views
into the same data: the list of images. So, I have classes that
represent that list of images, and represent individual images.
Whatever the UI needs to do to that list, it does by calling this
"model". Whatever the UI needs to show, it does by listening to the
"model" and responding to events coming from that "model". The UI
_never_ directly connects a button somewhere to a display changing
somewhere else. Buttons and user-modifiable controls _always_ do their
work by going through the model: user action → event handler →
method call / property set in the model → model event → event
handlers → changes to screen display. That way, all the visual stuff
on the screen is on the same footing, and responds in the same way to
changes in the data.

Often, model-view-controller is overkill for simple screens. However,
for your example, where you have two forms looking at the same data,
it's a natural fit. Read up on it (just search for "model view
controller") and give it a try!

Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jinlin | last post by:
I found a interesting problem in C# and couldn't explain it. The code to reproduce it is very simple: 1. Create a windows application in C#. 2. Listen to the activated event on the default form...
8
by: SAN CAZIANO | last post by:
i have to do in the onkeypress or in onchange the float (real) field validation I try something: function ValidaCampo(nomeCampo,TotInteri,TotDecimali) {...
2
by: RipTide | last post by:
Background: Using an unsupported/abandoned multi-user multi-database program that uses Access 97 and Jet 3.5. Program itself appears to have been built with PowerBuilder 6.5. Databases reside on...
2
by: CSDunn | last post by:
Hello, I need some assistance with error handling in an Access 2003 Project form. The project is has a data source connection to a SQL Server 2000 database. The main form is named...
13
by: Jeff Davis | last post by:
Right now performance isn't a problem, but this question has me curious: Let's say I have a shopping cart system where there is a "products" table that contains all possible products, and an...
5
by: Olly | last post by:
Hello Everyone! Could someone please have a look at my JS Form I posted below....Something wrong there, but I don't understand what's exactly. Many thanks. Olly ...
4
by: GS | last post by:
the procedure when executed from c#, it does update or insert but I got result code of -1 return to c# is not successful execution of stored proc 0 or number rows affected? connectionString...
5
by: Paul Rubin | last post by:
I think I've asked about this before, but is there a way to set up Python to handle uncaught exceptions with pdb? I know about setting sys.except_hook to something that calls pdb, but this is...
14
by: confusedfusion | last post by:
Not sure how many form submissions that have been lost over the years before I started but the company has a contact form that the required fields when validation fails the error message is going...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.