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

Open last form if application starts

I have WinForms MDI application.
MDI child forms are created by menustrip click enent handler

AppDesktop.FormMgr.Show(new ChildForm1("param1", "param2") );

Every MDI child form "childform1" may have separate class with different
name and different number of parameters.
When application in opened, last MDI child form must be opened
automatically.
I think I must store last opened form name to isolated storage and call

AppDesktop.FormMgr.Show(new LastOpenedForm(lastParam1, lastParam2) );
Reflection should probably used to make this call.

I think I can add some code to form base class constructor for this which
stores child form name in isolated storage.

Switch statement is too big for this and so it is ugly.

I think about the following approach:

1. Retrieve actual calling class name from base class constructor and store
it in isolated storage (application can be run form web browser also).

2. At startup retrieve form name and create mdi child from it using
reflection.
Any idea or sample code how to implement this ?

Apr 16 '07 #1
4 1989
On Apr 16, 8:57 am, "Andrus" <kobrule...@hot.eewrote:
I have WinForms MDI application.
MDI child forms are created by menustrip click enent handler

AppDesktop.FormMgr.Show(new ChildForm1("param1", "param2") );

Every MDI child form "childform1" may have separate class with different
name and different number of parameters.
When application in opened, last MDI child form must be opened
automatically.
I think I must store last opened form name to isolated storage and call

AppDesktop.FormMgr.Show(new LastOpenedForm(lastParam1, lastParam2) );
Reflection should probably used to make this call.

I think I can add some code to form base class constructor for this which
stores child form name in isolated storage.

Switch statement is too big for this and so it is ugly.

I think about the following approach:

1. Retrieve actual calling class name from base class constructor and store
it in isolated storage (application can be run form web browser also).

2. At startup retrieve form name and create mdi child from it using
reflection.

Any idea or sample code how to implement this ?
Here's a different idea.

First, Forms shouldn't have parameters for their constructors. It
makes it very difficult / impossible to design them using Visual
Studio Designer. You're better off making the MDI child forms have
constructors that take no arguments, and then give them settable
properties / setup methods you can call to configure them.

If you do that, then what you have becomes not a construction problem,
but a serialization / deserialization problem. Have each MDI child
form implement ISerializable, and teach it to serialize / deserialize
itself.

In isolated storage, you store the class of the MDI child form and its
serialized state. When your application restarts, it instantiates the
last MDI child form from its class name (using Reflection), and then
hands it the stored serialized state. The child form then deserializes
the information and configures itself appropriately.

Apr 16 '07 #2
First, Forms shouldn't have parameters for their constructors. It
makes it very difficult / impossible to design them using Visual
Studio Designer. You're better off making the MDI child forms have
constructors that take no arguments, and then give them settable
properties / setup methods you can call to configure them.
Thank you.
Currently I have parameterless constructor also for designer only.

I use parametherized constructurs since some forms require mandatory
parameters which are used in form constructor.
Using your recommendation I must change calls from

AppDesktop.FormMgr.Show(new ChildForm1("param1", "param2") );

to

myForm frm = new ChildForm1();
frm.param1 = "param1";
frm.param2 = "param2";
frm.Configure();
AppDesktop.FormMgr.Show(frm);

In this case, somebody may forget to set param1 and param2 properties or
call Configure() method.
This is also 5 lines of code insted of single line for every of my 100 form
call, so 400 lines more code.
Using parametherized constructors forces to pass mandatory parameters
always.

Are parametherized form constructors bad practice?
If so I can remove them.
If you do that, then what you have becomes not a construction problem,
but a serialization / deserialization problem. Have each MDI child
form implement ISerializable, and teach it to serialize / deserialize
itself.
In isolated storage, you store the class of the MDI child form and its
serialized state. When your application restarts, it instantiates the
last MDI child form from its class name (using Reflection), and then
hands it the stored serialized state. The child form then deserializes
the information and configures itself appropriately.
I'm new to C#. Do you have any sample or link about this ?

Currently I'm thinking about storing class name and poperties to isolated
storage and use reflection to invoke the code:

myForm frm = new ChildForm1();
frm.param1 = "param1";
frm.param2 = "param2";
AppDesktop.FormMgr.Show(frm);

but I have no idea how reflection allows to run this code.

Andrus.

Apr 16 '07 #3
I have also one more idea.

Every form has corresponding item in MenuStrip.

How to force MenuStrip to remember last issued form opening command ?
How to re-execeute last command in application startup using SendKeys() or
something similar ?

This avoids using reflection.

Andrus.

Apr 16 '07 #4
On Apr 16, 11:06 am, "Andrus" <kobrule...@hot.eewrote:
First, Forms shouldn't have parameters for their constructors. It
makes it very difficult / impossible to design them using Visual
Studio Designer. You're better off making the MDI child forms have
constructors that take no arguments, and then give them settable
properties / setup methods you can call to configure them.

Thank you.
Currently I have parameterless constructor also for designer only.

I use parametherized constructurs since some forms require mandatory
parameters which are used in form constructor.
Using your recommendation I must change calls from

AppDesktop.FormMgr.Show(new ChildForm1("param1", "param2") );

to

myForm frm = new ChildForm1();
frm.param1 = "param1";
frm.param2 = "param2";
frm.Configure();
AppDesktop.FormMgr.Show(frm);
No, not necessarily. You could set it up like this:

myForm frm = new ChildForm1();
frm.Configure("param1", "param2");
In this case, somebody may forget to set param1 and param2 properties or
call Configure() method.
Yes, someone could still forget to call the Configure method.
This is also 5 lines of code insted of single line for every of my 100 form
call, so 400 lines more code.
Well, two lines instead of one, which means 200 lines instead of 100
lines, but that's only a significant factor if you can get your
persistence problem solved using parameterized constructors. If you
can't, then those extra 100 lines of code don't look so bad any
more. :-)
If you do that, then what you have becomes not a construction problem,
but a serialization / deserialization problem. Have each MDI child
form implement ISerializable, and teach it to serialize / deserialize
itself.
In isolated storage, you store the class of the MDI child form and its
serialized state. When your application restarts, it instantiates the
last MDI child form from its class name (using Reflection), and then
hands it the stored serialized state. The child form then deserializes
the information and configures itself appropriately.

I'm new to C#. Do you have any sample or link about this ?

Currently I'm thinking about storing class name and poperties to isolated
storage and use reflection to invoke the code:

myForm frm = new ChildForm1();
frm.param1 = "param1";
frm.param2 = "param2";
AppDesktop.FormMgr.Show(frm);

but I have no idea how reflection allows to run this code.
I took a quick look at the documentation for ISerializable:

http://msdn2.microsoft.com/en-us/lib...le(vs.71).aspx

It looks rather complicated to me. It may be worth learning, but if
you want something simple and home-grown, then consider the following.

public interface ISerializeMDIChild
{
string SerializedState { get; set; }
}

Have each form implement this interface. Basically, each child form is
given the job of gathering all of its internal state that it thinks is
worth saving, and turning that into a string somehow. And, conversely,
taking a correctly formatted string and (the form) setting its
internal state based on what the string says.

The string can be anything at all. Its format is really private to the
child form, so it can be very machine-friendly. For example, say that
a child form needs to save the number of rows and columns of some grid
it is showing, and on which column the grid is sorted, and in what
sort order. The form could decide to emit a serialized string like
"10,5,3,ascending": 10 rows, 5 columns, sorted on column 3 ascending.
Then the SerializedState property might look like this:

public string SerializedState
{
get { return String.Format("{0},{1},{2},{3}",
this.grid.Rows.Count, this.grid.Columns.Count, this.gridSortColumn,
this.gridSortAscending ? "ascending" : "descending"); }
set
{
string[] parms = String.Split(",", value);
... write code to parse bits of string and set up form ...
}
}

Now your persistence / serialization problem becomes easier. You just
need to instantiate the form using Reflection (simply call no-argument
constructor), then set the SerializedState property from the string
you saved. You save only two things: the class name, and the
serialized string that came from the form when it was last active.

You can even use your constructors with arguments, so long as you
leave a no-argument constructor for Reflection to call. If you do it
that way, then there are two ways to create your child form:

1. Call the constructor with arguments.
2. Call the constructor with no arguments and set the SerializedState
property.

Apr 16 '07 #5

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

Similar topics

7
by: Martin | last post by:
Again drawing on the groups experience:- 1. For general file opening and file saving, using VB6, are there any issues with using the FileOpen and FileSave Common Dialog Boxes? 2. Is using the...
55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
6
by: Kenneth Courville | last post by:
Hello, I'm looking for assistance with the Access object model. I know this is VB, but I'm building an Office Add-using C# directed at Access 2002. I'm literate in VB, so you can reply in VB... I...
4
by: sara | last post by:
I am learning with a simple application/form. I am ok - enter customer name, list box of customers, select a customer, see the items for that customer. The items form has customer ID and Name...
5
by: JNariss | last post by:
Hello, Within a form I created I have placed links that users can click on to view information. I am trying to write a javascript code so it opens in a sized window but I can't seem to figure...
15
by: John Salerno | last post by:
Hi all. I apologize since this is only remotely Python related, but I hope someone might now the solution. I tried opening my Python chm docs just now, as well as the one for wxPython, and both...
3
by: Ronald S. Cook | last post by:
I know how to close and open a form... Well.. but can you tell me how can I do this..... Example... There are 2 forms...named formA and formB and each of them have their own button named...
7
jmoudy77
by: jmoudy77 | last post by:
I've got some vb code that gives me a "previous operation cancelled" error when I try and open the form as a subform in a tabbed control. When I open the form itself I don't get the error. Does...
5
by: Chuck Anderson | last post by:
I run Apache 2.0.55, and Php (both 4.4.1 and 5.2.5) on my home PC (Windows XP). One of the scripts that I run daily needs to access a secure URL (https://..............). When I am running Php4,...
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...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.