How to comment out the main form given a subform?

R

RayLopez99

I have a basic forms question. Your main form, Form1, is the gateway
to other forms. I have yet to figure out how to comment out the main
form easily and leave the subforms. Why? Because let's say you have
a complex program that really is like a Swiss Army knife and has a
bunch of components. Each component has it's own form, all linked by
the main form. You want to seperate these components so each
component is a stand alone program (since none of these components
depend on the other components--they just happen to be linked together
thorough the first main form, typically named Form1--but how do you do
that without losing information? Of course you can start from scratch
and laboriously recreate all menus, buttons, etc, which would take
about two or three hours or more, but I'm wondering if anybody has a
quick way of just commenting out stuff and doing a global search and
replace of text words to do this?

Any tips or even a success story appreciated.

RL
 
G

gerry

Maybe your I am misunderstanding your question , but it sounds like you just
want to modify your Main to invoke a different form :


using System;
using System.Windows.Forms;

namespace Oogity
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Forms.MainForm() ); // <<<----
change this to whatever form you like
}
}
}
 
R

RayLopez99

Maybe your I am misunderstanding your question , but it sounds like you just
want to modify your Main to invoke a different form :

Thanks for replying. I don't think it's this easy. I could not find
STAThread for example. Below is a partial list of the code. What
keywords do I search for?

RL

namespace RaysProjectName1
{
public partial class Form1 : Form
{
///

//from Form1.cs

private void AChildWindowToolStripMenuItem_Click(object sender,
EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}


// What I want to do is show Form2 as "Form1" without having to even
click on Form 1 (i.e., Form 1 disappears--as well as all other forms
besides form Form2).

// is there a generic way of doing this (since in actuality the
project is much more complicated, as it has fifty or so forms besides
the main form, Form 1, and they are all independent of one another)?

RL
 
J

Jeff Johnson

Thanks for replying. I don't think it's this easy. I could not find
STAThread for example. Below is a partial list of the code. What
keywords do I search for?
namespace RaysProjectName1
{
public partial class Form1 : Form
{
///

//from Form1.cs

private void AChildWindowToolStripMenuItem_Click(object sender,
EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}


// What I want to do is show Form2 as "Form1" without having to even
click on Form 1 (i.e., Form 1 disappears--as well as all other forms
besides form Form2).

I've been reading this thread and I STILL have no idea what it is you're
trying to accomplish. Are you trying to eliminate a form FROM YOUR CODE
(i.e., trying to do some sort of refactoring), or are you just trying to
hide it AT RUN TIME?
 
G

gerry

I am not sure I understand your situation.
It sounded like you were asking how to replace you applications main form.
If so , then just find the place where you create and show Form1 and change
it to Form2.

The code i posted is the standard Program.cs created by visual studio when
you create a new windows forms project.
Regardless of how your app was initailly created, somewhere in your
application you have a Main() and that should somehow create and show your
main form.




Maybe your I am misunderstanding your question , but it sounds like you
just
want to modify your Main to invoke a different form :

Thanks for replying. I don't think it's this easy. I could not find
STAThread for example. Below is a partial list of the code. What
keywords do I search for?

RL

namespace RaysProjectName1
{
public partial class Form1 : Form
{
///

//from Form1.cs

private void AChildWindowToolStripMenuItem_Click(object sender,
EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}


// What I want to do is show Form2 as "Form1" without having to even
click on Form 1 (i.e., Form 1 disappears--as well as all other forms
besides form Form2).

// is there a generic way of doing this (since in actuality the
project is much more complicated, as it has fifty or so forms besides
the main form, Form 1, and they are all independent of one another)?

RL
 
F

Family Tree Mike

RayLopez99 said:
I have a basic forms question. Your main form, Form1, is the gateway
to other forms. I have yet to figure out how to comment out the main
form easily and leave the subforms. Why? Because let's say you have
a complex program that really is like a Swiss Army knife and has a
bunch of components. Each component has it's own form, all linked by
the main form. You want to seperate these components so each
component is a stand alone program (since none of these components
depend on the other components--they just happen to be linked together
thorough the first main form, typically named Form1--but how do you do
that without losing information? Of course you can start from scratch
and laboriously recreate all menus, buttons, etc, which would take
about two or three hours or more, but I'm wondering if anybody has a
quick way of just commenting out stuff and doing a global search and
replace of text words to do this?

Any tips or even a success story appreciated.

RL


I'm like the others, and don't have a grasp at what you are after, but here
is a guess:

You have four forms launched from a main app in your current configuration.
Consider these Main, FormA, FormB, FormC, and FormD. In your mind after
creating this, you realize that launching FormA independently of the others
might make sense. This is kind of like having a package arround MS Office,
but now you realize that maybe MS Word makes sense on it's own.

If this is your goal, and where you are at in the development, the
straightforward way might be to create four copies of the project and in
each, make one of the other forms the main form. You could then delete the
other forms from the individual projects.

Lastly, your original form could add references to the other four projects
and call into those executables to create instances from your original
application.

Of course, this is all dependent on whether I am at all close to what you
want. If I am, then the lesson learned is to think about this at the
requirements phase (the fact you want four independent executables).
Starting off with the five independent projects is easier than going there
now.
 
R

RayLopez99

I'm like the others, and don't have a grasp at what you are after, but here
is a guess:

You're pretty good FTM.
You have four forms launched from a main app in your current configuration.
Consider these Main, FormA, FormB, FormC, and FormD.  In your mind after
creating this, you realize that launching FormA independently of the others
might make sense.  This is kind of like having a package arround MS Office,
but now you realize that maybe MS Word makes sense on it's own.

Yes, but with a slight twist. After creating this app, as you
describe it, I realize that I just want one form, FormA, which I can
call "Main". In other words, I want to delete Form B, FormC, and
FormD, but with this additional 'twist'--I do not want a "Main" form
(or rather, since you must have a main form, I want my Form A to be
the main form). Thus, when you fire up the app, the first window you
see (and the main form, and indeed the only form) will be FormA. But
how to do this, if you've already built Forms A through D? It's not
at all easy. Sure you can delete Forms B,C,D, leaving only the main
form and FormA--it is trivial to do this--but you're left with a main
form and form FormA, which again requires "two clicks" to launch FormA
(which really is your app in question) rather than one click. I want
just one click, and to be in my app (the one currently in FormA).
If this is your goal, and where you are at in the development, the
straightforward way might be to create four copies of the project and in
each, make one of the other forms the main form.  You could then deletethe
other forms from the individual projects.

Right, but see the above. You still have two clicks.

Of course, this is all dependent on whether I am at all close to what you
want.  If I am, then the lesson learned is to think about this at the
requirements phase (the fact you want four independent executables).
Starting off with the five independent projects is easier than going there
now.

I think your last paragraph sums it up--unless you plan this from the
get go, there's no easy way to do this, because all your menus will be
screwed up. To be sure, it's not that hard to recreate menus, except
in my case I have a bunch of bells and whistles sized just so, but I
can, with about a couple of hours or more of effort, recreate it in a
"one click" form. But I was thinking somebody may have come across
this problem and solved it with a global search and replace of text,
but it doesn't seem to be that easy to fix.

RL
 
R

RayLopez99

I've been reading this thread and I STILL have no idea what it is you're
trying to accomplish. Are you trying to eliminate a form FROM YOUR CODE
(i.e., trying to do some sort of refactoring), or are you just trying to
hide it AT RUN TIME?

Thanks for replying.

See my answer to FTM in this thread. It would be more like "FROM YOUR
CODE", not hiding it. But really the problem is more like I want just
ONE form, not a bunch of forms, but my current app has a bunch of
forms, and even deleting the bunch save one won't give me the one and
only form (again, see my reply to FTM), but rather, two forms.

RL
 
J

Jeff Johnson

Yes, but with a slight twist. After creating this app, as you
describe it, I realize that I just want one form, FormA, which I can
call "Main". In other words, I want to delete Form B, FormC, and
FormD, but with this additional 'twist'--I do not want a "Main" form
(or rather, since you must have a main form, I want my Form A to be
the main form). Thus, when you fire up the app, the first window you
see (and the main form, and indeed the only form) will be FormA. But
how to do this, if you've already built Forms A through D? It's not
at all easy. Sure you can delete Forms B,C,D, leaving only the main
form and FormA--it is trivial to do this--but you're left with a main
form and form FormA, which again requires "two clicks" to launch FormA
(which really is your app in question) rather than one click. I want
just one click, and to be in my app (the one currently in FormA).

You delete the Main form as well, then you do exactly what gerry suggested
in the first reply to the thread: Find the Main method (assuming it wasn't
in the Main form--but it won't be if you generated your Windows Forms
project through Visual Studio; it'll be in a separate class file named
Program.cs) and then change the Application.Run() call to use a new instance
of FormA instead of Main.
I think your last paragraph sums it up--unless you plan this from the
get go, there's no easy way to do this, because all your menus will be
screwed up. To be sure, it's not that hard to recreate menus, except
in my case I have a bunch of bells and whistles sized just so, but I
can, with about a couple of hours or more of effort, recreate it in a
"one click" form. But I was thinking somebody may have come across
this problem and solved it with a global search and replace of text,
but it doesn't seem to be that easy to fix.

Wait a minute...what menus? Why would they get screwed up?
 
F

Family Tree Mike

Jeff Johnson said:
You delete the Main form as well, then you do exactly what gerry suggested
in the first reply to the thread: Find the Main method (assuming it wasn't
in the Main form--but it won't be if you generated your Windows Forms
project through Visual Studio; it'll be in a separate class file named
Program.cs) and then change the Application.Run() call to use a new instance
of FormA instead of Main.



Perhaps there is no program.cs in his project? If there is not, then the
startup object in the project properties is what needs changing.


Wait a minute...what menus? Why would they get screwed up?

Is it possible that although the forms seem independent, that the main
application contains the other applications in an MDI design? If so, then
the main form would hold the menu items from the child forms. This is the
only guess I can come up with per the description thus far.


Mike
 
R

RayLopez99

You delete the Main form as well, then you do exactly what gerry suggested
in the first reply to the thread: Find the Main method (assuming it wasn't
in the Main form--but it won't be if you generated your Windows Forms
project through Visual Studio; it'll be in a separate class file named
Program.cs) and then change the Application.Run() call to use a new instance
of FormA instead of Main.

Ok, thanks to Gerry and you reminding me, it worked. I had misread
Jerry's post and assumed he was dealing with console mode ("Main") and
somehow I missed that even a Window Forms program has a "Main" in the
Program.cs file. What spurred me to reread Gerry's post was your
mention of "Program.cs" (which I know I have).

So assist to you. Thank you.

RL
 
R

RayLopez99

Perhaps there is no program.cs in his project?  If there is not, then the
startup object in the project properties is what needs changing.

No, I misread Gerry's post--I do have a program.cs and following his
advice it worked. But I'm curious if such a thing exists as you
surmise, so I'll follow this thread in the future.

RL
 
D

Dathan

You're pretty good FTM.


Yes, but with a slight twist.  After creating this app, as you
describe it, I realize that I just want one form, FormA, which I can
call "Main".  In other words, I want to delete Form B, FormC, and
FormD, but with this additional 'twist'--I do not want a "Main" form
(or rather, since you must have a main form, I want my Form A to be
the main form).  Thus, when you fire up the app, the first window you
see (and the main form, and indeed the only form) will be FormA.  But
how to do this, if you've already built Forms A through D?  It's not
at all easy.  Sure you can delete Forms B,C,D, leaving only the main
form and FormA--it is trivial to do this--but you're left with a main
form and form FormA, which again requires "two clicks" to launch FormA
(which really is your app in question) rather than one click.  I want
just one click, and to be in my app (the one currently in FormA).




Right, but see the above.  You still have two clicks.


I think your last paragraph sums it up--unless you plan this from the
get go, there's no easy way to do this, because all your menus will be
screwed up.  To be sure, it's not that hard to recreate menus, except
in my case I have a bunch of bells and whistles sized just so, but I
can, with about a couple of hours or more of effort, recreate it in a
"one click" form.  But I was thinking somebody may have come across
this problem and solved it with a global search and replace of text,
but it doesn't seem to be that easy to fix.

RL

Try this: within Visual Studio (assuming that's the dev environment
you're using), hit Ctrl-F to open the find menu. Change the "Look in"
option to "Current Project" and type "static void Main" in the find
box. If it comes up with a result, that's the main entry point to
your program (unless there's a setting I'm not aware of to change
it). Inside it, there's probably a line that says "Application.Run
(new Form1());". Change Form1 to the name of whatever form you want
to load first, and it should work. If not, at least it informs the
discussion a little bit.

~Dathan
 
R

RayLopez99

Try this: within Visual Studio (assuming that's the dev environment
you're using), hit Ctrl-F to open the find menu.  Change the "Look in"
option to "Current Project" and type "static void Main" in the find
box.  If it comes up with a result, that's the main entry point to
your program (unless there's a setting I'm not aware of to change
it).  Inside it, there's probably a line that says "Application.Run
(new Form1());".  Change Form1 to the name of whatever form you want
to load first, and it should work.  If not, at least it informs the
discussion a little bit.

Yeah I did that. Apparently my options were not Current Project or
whatever and I assumed some stuff that was wrong. But problem solved
now as per Gerrys' original reply.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top