Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My program opens image files and creates each time a mdichild to visualize them. The mdichild form "Text" property gets the name of the opened file without path.

If the user decides to open the same file twice, I would like the mdichild "Text" to be unique adding, for example a suffix to the file name (myfile.jpg and myfile(1).jpg). This to let the user know on which copy of the file he is working on.

How could you implement this window title change ?

If you look at this code:

Foreach (Form child in this.MdiChildren)
{
 if (child.Text.Contains(filename) == true
{
count = count + 1;
// here the code to add the suffix (count)
}
}


If the user decides to close this mdichild, you need to update count subtracting 1.

This works of course only with the first file opened. To make it work for each file I should use a "table", for example, with filename and count as fields.

Which kind of data structure would you use for this ? Is it the right way or is there something smarter ?
Posted
Comments
Sergey Alexandrovich Kryukov 19-Mar-13 10:22am    
Why MDI?! And, even with MDI, what's the problem?
—SA

Not only your schema is ugly, it won't really work at all, because it will lead to duplicates, if a user closes not the last file.

You can use the following: increment the number every time, per file name. You only need to have unique identification of file buffers, not really a continues set of numbers. If the user removes some number less then the last one, do the same. For example, you have 1, 2, 3, 4. The user removes 2. You have 1, 3, 4. Then you add one more copy of the same file. Make it 1, 3, 4, 5. Then number 2 will keep missing. In all example, I take into account only the numbers per one full file name, of for all these numbers.

But! On each file closed by the user, do the following: check up if it closes all of the copies of some file. In this case, your counter will be reset to, say 1. Probably you should not show this 1 until you have 2. That solves the problem.

To support the schema of mapping the files and numbers of their copies, use the dictionary like System.Collections.Generic.Dictionary<string, FileCopyDescriptor>, where FileCopyDescriptor can be some data class storing the copy number and reference to the file buffer object or its form. But don't go with forms. Keep reading…

Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA
 
Share this answer
 
v2
I would get the MDI parent to do it.
If opens each of the children, so it has access to each of them, and their Text properties.
It can easily handle the FormClosing event for the children, and update the other child Text properties at that point.
I would probably add a property to the child forms ("ImageFilePath" or similar) to allow the parent to detect if two have the same image file loaded.
No new structures needed, and only the classes that need to know about others are involved in the name manipulation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900