Click here to Skip to main content
       

Windows Forms

 
QuestionEditingPanel Erasing Top Cell Border PinmemberMark Regal13-Aug-12 20:06 
AnswerRe: EditingPanel Erasing Top Cell Border PinmemberMark Regal14-Aug-12 4:14 
QuestionVS2010 Setup and Deployment Project - CommonAppDataFolder for XP PinmemberMember 269415310-Aug-12 10:05 
AnswerRe: VS2010 Setup and Deployment Project - CommonAppDataFolder for XP PinmvpDave Kreskowiak10-Aug-12 15:19 
GeneralRe: VS2010 Setup and Deployment Project - CommonAppDataFolder for XP PinmemberMember 269415313-Aug-12 3:18 
AnswerRe: VS2010 Setup and Deployment Project - CommonAppDataFolder for XP Pinmemberictxiangxin13-Aug-12 5:03 
QuestionWrapping of Data Grid View column [modified] Pinmemberukraju5-Aug-12 21:58 
AnswerRe: Wrapping of Data Grid View column PinmemberMember 908236510-Aug-12 13:59 
QuestionWindows Form PinmemberDSPNEOqqq3-Aug-12 10:15 
RantRe: Windows Form PinmemberEddy Vluggen3-Aug-12 10:30 
GeneralRe: Windows Form OT PinmemberWes Aday3-Aug-12 10:41 
GeneralRe: Windows Form OT PinmemberEddy Vluggen3-Aug-12 11:01 
AnswerRe: Windows Form PinmemberChristian Amado6-Aug-12 11:47 
AnswerRe: Windows Form [modified] PinmemberBillWoodruff11-Aug-12 16:27 
Hi,
 
First, I believe that Child Forms should never be used, that they lead to awkward user-interfaces, and the old MDI-architecture, which supported MDIChildForms with "special features," deserves the status of a "dinosaur" skeleton: to be looked at, but not re-animated Smile | :)
 
I think you can get better responses to this scenario if you describe what the function of your proposed use of "Child Forms" are, in relation to their "Parent" Form.
 
Is it possible a Dialog can fit your needs here ?
 
Consider that you create a "Child Form" with code like this in response to some event:
            Form form2 = new Form();
            form2.TopLevel = false; // mandatory for Child Forms
            form2.ShowInTaskbar = false;
            form2.Parent = this;
            form2.Name = "form2";
            form2.Text = "form2";
            form2.Size = new Size(300, 200);
            form2.Show();
            form2.BringToFront();
You now have a Form that is in front of your MainForm, and that, depending on FormBorderStyle, can be moved around so parts of it are invisible, as they extend beyond the DisplayRectangle of the MainForm. Depending on its z-order within the Form ... that is if some other control on the Form has been brought-to-the-front: it could appear "behind" other Controls on the Form.
 
Consider that you create a Form which is not a ChildForm: like this:
            Form form2 = new Form();
            form2.ShowInTaskbar = false;
            form2.Name = "form2";
            form2.Text = "form2";
            form2.Size = new Size(300, 200);
            form2.Show();
            form2.BringToFront();
Now you have a Form you can move anywhere on the screen, depending on the setting of FormBorderStyle, and it will always be visible ... unless you activate the MainForm and bring it to the front, and the MainForm covers part of the second Form.
 
If you wanted this new Form to always remain on top, no matter whether the MainForm was activated, or not: just set the new Form's TopMost property to 'true, while making the sure the MainForm's TopMost property is set to 'false.
 
Another area to investigate, if you are concerned with your new Form always appearing on top, is the use of 'ShowModal(), in contrast to 'Show().
 
Keep in mind that: if your WinForms project opens in the rather standard way by creating an instance of the MainForm in the Program.cs class: that closing the MainForm will always automatically close any secondary Forms created.
 
In either case, if you need to exchange information between the MainForm and ChildForm, or MainForm and/or new, "independent" Form, the issues will be exactly the same: you'll need to keep a reference to the newly created Form in the MainForm ... assuming that's where you'll always create this new Form, whether Child, or "independent;" so: make sure that outside the method that creates the Form (i.e., at Form level scope) you create a reference you can access, like:
            private Form form2;
 
            private void SomeButton_Click(object sender, EventArgs e)
            {
                form2 = new Form();
                form2.ShowInTaskbar = false;
                form2.TopMost = true;
                form2.Name = "form2";
                form2.Text = "form2 within Form class scope";
                form2.Size = new Size(300, 200);
                form2.Show();
                form2.BringToFront();
            }
If you do need access to whatever in the MainForm to whatever in new Form, or vice-versa (data, contents, position, state, etc.): then there are plenty of solutions for how to do that here on CP in the QA forum (it's one of the most frequent questions asked). Hint: expose public properties.
 
... edit 1 ...
 
For example:
 
In the MainForm, create a Public Property that will "expose" the instance of the MainForm you create:
// in the definition of the MainForm Class
//
public static MainForm TheMainForm { get; set; }
//
// then in the MainForm Load event, or after the call to InitializeComponent: set the instance:
ThisMainForm = this;
Once you have done this, you can access anything declared Public on the MainForm, from outside the MainForm, by simply using: "MainForm.ThisMainForm."
 
Warning: this technique should only be used on Forms where you create one instance only ! Why: because a Form-level public static Property is always going to point to one, and only one, instance: if you had multiple instances (heaven help you) of your MainForm, changing some public variable's value there would also change it "globally:" so in all other instances: it would have the same value !
 
For your secondary Forms, you do not wish to use this same technique of a static Form-level Property to enable access to their "public stuff:" because, after all, each instance of a secondary Form is going to have the same controls, the same Public Form-scoped variables, etc., and, at run-time these may take on unique values.
 
But, your MainForm can access every instance of a secondary Form it creates (and everything scoped Public on it) by simply putting it in a unique public variable when it is created.
 
Finally, note there are other "models" for passing information between Forms, including using custom Events, and EventArg data structures: you'll find examples of other approaches, and lots of debate about the different ways of enabling Form to Form interaction in the QA section here on CP.
 
... end edit 1 ...
 
best, Bill
"Everything we call real is made of things that cannot be regarded as real." Niels Bohr


modified 12-Aug-12 23:35pm.

GeneralRe: Windows Form PinmemberEddy Vluggen12-Aug-12 4:43 
QuestionWindows Form Application Size Issue Pinmemberukraju29-Jul-12 20:45 
AnswerRe: Windows Form Application Size Issue PinmemberSimon_Whale29-Jul-12 21:11 
AnswerRe: Windows Form Application Size Issue PinmemberBernhard Hiller29-Jul-12 23:07 
AnswerRe: Windows Form Application Size Issue PinmemberBillWoodruff11-Aug-12 16:31 
GeneralWindows Forms Application With Collapsible Panel Pinmemberukraju20-Jul-12 21:15 
AnswerRe: Windows Forms Application With Collapsible Panel PinmemberEddy Vluggen21-Jul-12 9:16 
GeneralRe: Windows Forms Application With Collapsible Panel [modified] PinmemberBillWoodruff29-Jul-12 18:01 
GeneralRe: Windows Forms Application With Collapsible Panel PinmemberGer Hayden1-Aug-12 20:45 
GeneralRe: Windows Forms Application With Collapsible Panel [modified] PinmemberBillWoodruff12-Aug-12 2:15 
Questionopen web page with out address bar from win forms Pinmembervikaskardode6-Jul-12 7:32 
AnswerRe: open web page with out address bar from win forms PinmvpDave Kreskowiak6-Jul-12 10:46 
JokeRe: open web page with out address bar from win forms PinmemberMycroft Holmes7-Jul-12 0:15 
AnswerRe: open web page with out address bar from win forms PinmemberEddy Vluggen21-Jul-12 14:10 
Questionat this point in time would a series of tutorials on WinForms be useful ? PinmemberBillWoodruff30-Jun-12 19:34 
AnswerRe: at this point in time would a series of tutorials on WinForms be useful ? PinmemberPaul Conrad30-Jun-12 20:18 
AnswerRe: at this point in time would a series of tutorials on WinForms be useful ? PinmvpRichard MacCutchan30-Jun-12 22:32 
AnswerRe: at this point in time would a series of tutorials on WinForms be useful ? PinmemberEddy Vluggen1-Jul-12 0:10 
QuestionDisplay large content in windows application PinmemberRajeev_8721-Jun-12 14:18 
AnswerRe: Display large content in windows application PinmemberMycroft Holmes21-Jun-12 17:36 
GeneralRe: Display large content in windows application PinmvpRichard MacCutchan21-Jun-12 23:47 
Questionupdating, deleting adn adding rows in superlist control PinmemberKandepu Rajesh13-Jun-12 0:37 
AnswerRe: updating, deleting adn adding rows in superlist control PinprotectorPete O'Hanlon13-Jun-12 1:01 
QuestionError while binding the datatable to a superlist control PinmemberKandepu Rajesh8-Jun-12 1:07 
AnswerRe: Error while binding the datatable to a superlist control PinmemberEddy Vluggen8-Jun-12 1:33 
Questionerrro while binding datatable to asuperlist control PinmemberKandepu Rajesh5-Jun-12 22:16 
AnswerRe: errro while binding datatable to asuperlist control PinmemberEddy Vluggen6-Jun-12 4:28 
QuestionError while binding datatable to the superlist control PinmemberKandepu Rajesh5-Jun-12 22:12 
AnswerRe: Error while binding datatable to the superlist control PinmvpDave Kreskowiak6-Jun-12 2:50 
GeneralRe: Error while binding datatable to the superlist control PinmemberKandepu Rajesh8-Jun-12 3:51 
GeneralRe: Error while binding datatable to the superlist control PinmvpDave Kreskowiak8-Jun-12 4:21 
QuestionThe code of closing X and opening new form PinmemberIamateur27-May-12 23:11 
AnswerRe: The code of closing X and opening new form PinmemberEddy Vluggen5-Jun-12 2:08 
QuestionC# code problem Pinmembernitish_0726-May-12 21:53 
AnswerRe: C# code problem repost PinmvpRichard MacCutchan26-May-12 22:35 
GeneralRe: C# code problem repost Pinmembernitish_0726-May-12 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 25 Dec 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid