I want to make an application that have one main form that have one functionality = Add new form.
I dont know how forms will be created, they are created dynamically by the user (AddForm methos from the main form).
All the subs forms are the same but some of thier prop receive differnce parameters (it can be in the ctor or later).
I want to be able to close all the forms when I close the main form.
Before new form will displayed I want to display setting form (to take the form parameters) maybe with ShowDialog method and do validating check on the form and just if the form validate the new form will displayed, if not (or if the user prees cancel) the form will disposed.
I know aboout MDI but I really perfer other way
Any ideas?
Thanks!
Closing all forms when main form is closed is somewhat easy, you just pass the main form reference in Show() method of the "child" forms; e.g. if you show child from main, you do:
child.Show(this);
This needs to be done if the your main form is not the real "main form of the program", but you want to close all "child" forms.
However, wiring all this together would preferably be done in some special class for this purpose, maybe called ScreenRepository. In this class, you would have a collection of open forms at any moment, you would deffer form creation to this class (so that this class automatically injects form parent) etc... Having this class would be easy to re-activate (give focus) the form if it is behind other forms, create new form if needed etc...
Idea is simple create a application that open a main form a the beginning and then open open other forms if needed if you close main form rest of forms are also closed. Like in GIMP.
Related
My main form is this
Now my problem occurs when I click Add -> Student, which is meant to bring up a new window that, instead it does this
So as you can see it opens the new form inside the same window. It didn't use to do this, then I made the main form an MdiContainer and set it as the MdiParent of the second form.
Is there a property I can change to make this new form pop up in a new window? Becuase I need to leave the MdiContainer property as True so that I can take data from Form2 and use it in Form1
Please let me know any other information you may need to help me fix this issue, as I'm not sure what caused it so I don't know what to change in order to fix it or even where I'm meant to be looking
you need to hide form enroll
when you open add window add.show()
you need to hide enroll form enroll.hide()
I'm trying to make a WindowsFormApplication in Visual Studio 2015 and need some help.
I've been trying to search for the answer on internet but can find out how to do the following:
I have two windows (solutions?). I open the second window with a button in the first one with this code:
this.Hide();
intermec prodinter = new intermec();
prodinter.ShowDialog();
My question is:
How can i "include" the second window (like "include" in PHP) instead of close the first window and then open the next one, like it does now?
A Form is just another Control. Think of it as a Container (because it holds other Controls).
A User Control can also hold more than one Control. There are ways you can display a Window inside another Window in a WinForms app, but the desired effect is not always guaranteed. So it would be best to place all of your controls (for "page 1", for example) in a User Control called "Page1", and then, when appropriate, add that User Control to the Form, and set its Dock property to Fill.
And when it's time to show a different "page", Hide(); "Page1", and Show(); "Page2".
I think you are talking about form inheritance:
Just create a form, lets call it as frmBase. And add some controls onto frmBase which you want to have on other forms as well.
Create other form, lets call it as frmDerived.
In the code behind of frmDerived, just do the following:
// derive the frmDerived form from frmBase
public partial class frmDerived : frmBase
{
public frmDerived()
{
InitializeComponent();
}
}
And then just check the frmDerived form design, it should include everything from frmBase.
And you may want to make the access modifier of some controls of frmBase to Public as required to access them on frmDerived.
I hope this will help you. :)
I want to do something like the answer here:
How can I close a login form and show the main form without my application closing?
...but I want to pass a value selected on the initial form to the next (main) form. If I call an overridden constructor on the main form, where do I store the value in the meantime (between the initial form being dismissed and the main form being called)?
OTOH, if, instead of using the program.cs file to do this, I create the "initial form" inside the main form's Load() event (is there a better place), I could do something like this, but admittedly it seems rather kludgy:
0) Set the main form's size to 0,0 to hide it
1) Show the "initial" form/modal dialog, and store the value the user selects (in a button click event) in a form-global variable
2) Once the initial form/modal dialog closes, set the main form's size back to what it should be (unless modal result <> OK, in which case I close the main form and the app)
I know there's a better way to do this...
You don't have to pass a value to the main form. Just like your link explains, open your main form first. Then your main form can open the other form. This other form can place the information in a public property that the main form can access. Since the main form controls the lifetime of this other form, the main form gets the information held in the other form's public property, then closes the other form.
string myVariable;
using (OtherForm otherForm = new OtherForm())
{
otherForm.ShowDialog();
myVariable = otherForm.OtherVariable;
}
Try using ApplicationContext (System.Windows.Forms.ApplicationContext). You can show multiple forms as shown in the example in the following MSDN thread. And regarding data,you can have a common data object which is created once and the forms are instantiated with the data object passed to them before showing them.
http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext%28v=vs.100%29.aspx
lets say i have a main form which have a lot of functionallity.
this form have a tab control in which each tab contain some set of functionality.
what i want to do is when i click on each tab controls button i want to load a form into the client area of the tab control.
so instead of having a lot of controls in the main form , i will only have set of forms and each form will have its control.
i think this is will be better in term of managing the controls so i dont have like 150 control on the main form.
so basically i want to load a form on another form and not show the form in a seperate view.
if its not possible with forms then can i use another control that will group the controls and will be loaded on the main form?
thanks
Alternate 1 :
You can make each of the form as a User Control and then you can load the appropriate user control in a blank panel in your main form whenever required.
You should be able to find a way to communicate between your form and those user controls.
Alternate 2 :
You can show the appropriate Form using ShowModal() method, with the main form as parent, that way user can finish the work with the child form, before coming back to the main form.
Disadvantages here are user wont be able to interact with the main form as long as the child form is closed.
I would recommend looking into User Controls.
User Controls come with a designer, just like forms, and have a rich event model to tap into. Unlike forms, they are easy to embed into other controls and forms. As a matter of fact, user controls will show up in your toolbox to drag-and-drop onto another form.
It's at least worth taking a look at.
Following code adds one Form to a panel in another form.
Add this code in Form1
Form2 ff = new Form2();
ff.TopLevel = false;
ff.Dock = DockStyle.Fill;
ff.ControlBox = false;
ff.Text = "";
panel1.Controls.Add(ff);
ff.Show();
The flip side is your panel should be big enough to accomodate the form...
I have found the Form.TopMost property but it puts the form on top of everything, including stuff that isn't part of my app. I've got a suspicion that I'm missing something obvious here. (Is Form the proper bases class for a non modal dialog box?)
Use the Form.Owner property of your dialog form and set it to the main Form.
Read more here
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx
The Owned form will never be displayed behind the Owner Form.
It is very simple; You just have to pass the owner when you call the Show() method
YourForm.Show(parentForm);
You can specify parent-child relationships between windows by supplying the parent Form as parameter to the ShowDialog() method called on the child Form. The child window will then stay on top of the parent and also minimize and restore along with the parent.
If i understand you correctly your opening a form from your application, and you want your new form to be on top of the old one.
To do this you can use ShowDialog() and StartPosition
SomeForm MyNewForm = new SomeForm();
MyNewForm.ShowDialog();
this will make that form stay on top of the orignal one, and you can also use
MyNewForm .StartPosition = FormStartPosition.CenterParent;
To control where that new form shows on the screen.