473,385 Members | 1,309 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,385 software developers and data experts.

MDI Help

I am trying to create an MDI Application. I am opening the windows OK
(MDI Children). But I don't think I am doing it correctly.

Basically, I declare the forms on the main form (Parent).

frm1 f1;
frm2 f2;

etc

One for each form.

Then, when I open the form, I just do:

if (f1 != null)
f1 = new frm1;
....
f1.Show();
f1.BringToFront();

However, if I close the form, and try open... it breaks... I have no
code for the close, so it seems to think that even though f1 is
closed... it's not null...

How should I be closing forms? Or, is there a better way to be doing
this?
Dec 30 '07 #1
10 1684
even though f1 is closed... it's not null...

Yes - nothing will automatically clear fields, so it *isn't* null (nor
is it collected, since the field keeps it in-scope).

Two solutions; the first is the hook into the Form's FromCosed event
and set the field to null - but this is a little scrappy since it
makes having multiples of a form tricky. Personally I'd forget about
the fields, and simply enumerate the MdiChildren looking for a likely
form - if you find such, bring it to the front, else spawn a new one
and add it as a child. This way, you don't have to worry about the
fields (there aren't any). And you can probably do the "show existing/
new form<x>" simply with generics - something like (notepad code - not
tested):

void ShowForm<T>() where T : Form, new() {
foreach(Form child in MdiChildren) {
if(child is T) {
child.BringToFront();
return;
}
// not found
T newForm = new T();
newForm.MdiParent = this; // or however... I can't remember...
newForm.Show();
newForm.BringToFront();
}
}

Marc
Dec 30 '07 #2
Forgot to add - then you would just call:

ShowForm<Form1>();

or whatever, where Form1 is the class-name of a type of Form.
Dec 30 '07 #3
On 30 Dec, 15:43, Marc Gravell <marc.grav...@gmail.comwrote:
Forgot to add - then you would just call:

ShowForm<Form1>();

or whatever, where Form1 is the class-name of a type of Form.
Fantastic!
I'll give this a bash. I'm quite a novice at this. Thanks for the
help, Marc.
Dec 30 '07 #4
Darn.. battling...

Should my ShowForm function look something like this?

private void ShowForm(Form form)
{
foreach (Form child in MdiChildren)
{
if (child is form)
{
child.BringToFront();
return;
}
}
// not found
form newForm = new form();
newForm.MdiParent = this;
newForm.Show();
newForm.BringToFront();
}

Dec 30 '07 #5
Not if you intend it to work the way I did... the generics (the <T>
stuff) was important to this solution (let me know if you are using
1.1, since generics don't exist there); you'd need as below (and I've
tested it this time):

Marc

private void ShowForm<T>() where T : Form, new()
{
foreach (Form child in MdiChildren)
{
if (child is T)
{
child.BringToFront();
return;
}
}
// not found
T newForm = new T();
newForm.MdiParent = this;
newForm.Show();
newForm.BringToFront();
}

then call i.e. "ShowForm<Form1>();"
Dec 30 '07 #6
aside: I've just realised that my tested and working code was exactly
what I posted earlier (untested); if you had some problems with
getting this to work, then please let me know.

Marc
Dec 30 '07 #7
On 30 Dec, 21:31, Marc Gravell <marc.grav...@gmail.comwrote:
aside: I've just realised that my tested and working code was exactly
what I posted earlier (untested); if you had some problems with
getting this to work, then please let me know.

Marc
Sorry Marc, didn't realise I must include the 'where T : Form, new()'
part. This is syntax I have never seen before. Was a bit confusing.
Will read up on 'generics' to help me work out what's happening there?

Will try this code you sent.

Thanks kindly.
Dec 30 '07 #8
Will read up on 'generics'
(MSDN: http://msdn2.microsoft.com/en-us/library/512aeb7t.aspx)

Generics are the "big feature" in 2.0, in the same way (but much, much
more so) as LINQ is the "big feature" in 3.5. It is a big (and very
important) area, so I can't attempt to do it much justice, but in
short, the code says:

* ShowForm is a generic method that accepts a single "type parameter"
named (by convention) "T" [this is the <Tbit]
* The caller is only allowed to invoke ShowForm with types that derive
from Form (inclusive) [T : Form()], and which have a public
parameterless constructor [T : new()]

When the method is called, "T" is a template (a Type); broadly
speaking, if you call it with Form1 as the type parameter (by saying
ShowForm<Form1>()), then imagine doing a find-and-replace inside
ShowForm on "T" replaced with "Form1":
.... if(child is Form1) ...
.... Form1 newForm = new Form1();
etc

The big restriction is that you can only use members that *must* exist
for *any* template - i.e. the methods on System.Object, and anything
that are constraints [T : Form, new()] introduce. For instance, we can
only use newForm.MdiParent because the compiler knows that it is (at
minimum) a Form, and Form has an MdiParent.
(this is different to C++ templates, where-by it checks at compile
time for suitable members; .NET generics are actually runtime-based
(not compile-time), so must work for types the compiler doesn't yet
know about).

This also means you can't call the method with unsuitable type
arguments, i.e. ShowForm<StringBuilderwill fail at compile-time
because StringBuilder isn't a Form.

There is quite a lot more depth to generics (compile-time type-
parameter-inference, for example) - but a fascinating area, and one
that you are going to have to face at some point to work with .NET 2
(and above).

Perhaps the first generics example people see is List<T- i.e.
List<intdata = new List<int>();
data.Add(16);
....
data.Add(13);
data.Sort();
[etc; same things, but this time a whole generic type, not just a
generic method]

Marc
Dec 30 '07 #9
It's working, but I haven't quite grasped the workings behind it.
Also, when I close the mdiChild form, is it freed, or do I need code
to make sure it's dead. I ask, as when I close it, and reopen.. the
new form seems to appear in a lower position... as if it was
cascading, even though the other form has been closed. Is this normal?
Jan 1 '08 #10
Also, when I close the mdiChild form, is it freed, or do I need code
to make sure it's dead.
Yes, it should be fully disposed and available for garbage collection.
I ask, as when I close it, and reopen.. the
new form seems to appear in a lower position... as if it was
cascading, even though the other form has been closed. Is this
normal?
This is normal for a new form; if you want it to appear in the same
position as it (or rather: the previous form) was, then you'll either
need to save the location/size somewhere, else simply hide the old
form (proabably by catching the "closing" event, set the cancel flag,
and call Hide() instead)

Marc
Jan 7 '08 #11

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.