Click here to Skip to main content
15,868,292 members
Home / Discussions / Windows Forms
   

Windows Forms

 
QuestionWrapping of Data Grid View column Pin
ukraju5-Aug-12 20:58
ukraju5-Aug-12 20:58 
AnswerRe: Wrapping of Data Grid View column Pin
908236510-Aug-12 12:59
908236510-Aug-12 12:59 
QuestionWindows Form Pin
DSPNEOqqq3-Aug-12 9:15
DSPNEOqqq3-Aug-12 9:15 
RantRe: Windows Form Pin
Eddy Vluggen3-Aug-12 9:30
professionalEddy Vluggen3-Aug-12 9:30 
GeneralRe: Windows Form OT Pin
Wes Aday3-Aug-12 9:41
professionalWes Aday3-Aug-12 9:41 
GeneralRe: Windows Form OT Pin
Eddy Vluggen3-Aug-12 10:01
professionalEddy Vluggen3-Aug-12 10:01 
AnswerRe: Windows Form Pin
Christian Amado6-Aug-12 10:47
professionalChristian Amado6-Aug-12 10:47 
AnswerRe: Windows Form Pin
BillWoodruff11-Aug-12 15:27
professionalBillWoodruff11-Aug-12 15: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 Pin
Eddy Vluggen12-Aug-12 3:43
professionalEddy Vluggen12-Aug-12 3:43 
QuestionWindows Form Application Size Issue Pin
ukraju29-Jul-12 19:45
ukraju29-Jul-12 19:45 
AnswerRe: Windows Form Application Size Issue Pin
Simon_Whale29-Jul-12 20:11
Simon_Whale29-Jul-12 20:11 
AnswerRe: Windows Form Application Size Issue Pin
Bernhard Hiller29-Jul-12 22:07
Bernhard Hiller29-Jul-12 22:07 
AnswerRe: Windows Form Application Size Issue Pin
BillWoodruff11-Aug-12 15:31
professionalBillWoodruff11-Aug-12 15:31 
GeneralWindows Forms Application With Collapsible Panel Pin
ukraju20-Jul-12 20:15
ukraju20-Jul-12 20:15 
AnswerRe: Windows Forms Application With Collapsible Panel Pin
Eddy Vluggen21-Jul-12 8:16
professionalEddy Vluggen21-Jul-12 8:16 
GeneralRe: Windows Forms Application With Collapsible Panel Pin
BillWoodruff29-Jul-12 17:01
professionalBillWoodruff29-Jul-12 17:01 
GeneralRe: Windows Forms Application With Collapsible Panel Pin
Ger Hayden1-Aug-12 19:45
Ger Hayden1-Aug-12 19:45 
GeneralRe: Windows Forms Application With Collapsible Panel Pin
BillWoodruff12-Aug-12 1:15
professionalBillWoodruff12-Aug-12 1:15 
Questionopen web page with out address bar from win forms Pin
vikaskardode6-Jul-12 6:32
vikaskardode6-Jul-12 6:32 
AnswerRe: open web page with out address bar from win forms Pin
Dave Kreskowiak6-Jul-12 9:46
mveDave Kreskowiak6-Jul-12 9:46 
JokeRe: open web page with out address bar from win forms Pin
Mycroft Holmes6-Jul-12 23:15
professionalMycroft Holmes6-Jul-12 23:15 
AnswerRe: open web page with out address bar from win forms Pin
Eddy Vluggen21-Jul-12 13:10
professionalEddy Vluggen21-Jul-12 13:10 
Questionat this point in time would a series of tutorials on WinForms be useful ? Pin
BillWoodruff30-Jun-12 18:34
professionalBillWoodruff30-Jun-12 18:34 
AnswerRe: at this point in time would a series of tutorials on WinForms be useful ? Pin
Paul Conrad30-Jun-12 19:18
professionalPaul Conrad30-Jun-12 19:18 
AnswerRe: at this point in time would a series of tutorials on WinForms be useful ? Pin
Richard MacCutchan30-Jun-12 21:32
mveRichard MacCutchan30-Jun-12 21:32 

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

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