How to add subforms to a form in c# - c#

i have two little questions to you:
How to add several subforms to a form in C# like on the pictures below:
[Cascade]
[Tile]
And the second one is how to manage their position(Cascade, Tile, etc.).
Thank you for your time!

This is a Multiple Document Interface (MDI) form but, as Robert Harvey mentioned in a comment, there's probably a better way to build your UI.
To layout the child forms you will use the LayoutMdi method of the parent.

It's called MDI (Multiple Document Interface). To do that, you need to change the IsMdiContainer property of the MainForm to true. And then you need to change the MdiParent property of the child Forms to your MainForm instance.
You can have a method in your MainForm class like:
public void MakeChildForm(Form childForm)
{
childForm.MdiParent = this;
}

Related

How to Share Common Groups of Controls in Winforms C#

The majority of my forms for my project include an OK and Cancel button. (always positioned at the bottom right of the form). Currently I have a base class that inherits from System.Windows.Forms which contains an OK and Cancel button. All forms that use this then inherit from this base form class. Is there a better way of doing this that takes localization into consideration?
I would use MDI Child Forms for this. Parent Form Can contain OK/Cancel button where as you would have your child form in MDI container.
For More help visit
https://msdn.microsoft.com/en-us/library/aa984329(v=vs.71).aspx
You could just create a single form that has an empty panel or table layout, where you dynamically load the desired user control. It is basically the composition over inheritance principle.
public partial class MyFormWithButtons : Form
{
public MyFormWithButtons(UserControl control)
{
InitializeComponent();
control.Dock = DockStyle.Fill;
myPanel.Controls.Add(control);
}
}
Doing form inheritance is very useful in many levels:
Make a base form, and name it for ex: FrmBase.
Add the Ok, Cancel Buttons to it and set the Anchor property for both to Bottom.
Set the Buttons "Modifiers" property to "Internal", this way you can access these buttons from inherited forms:
Make as many forms as you want and make each inherit from the FrmBase ex: Form1 : FrmBase
now you can access the buttons from this from, using the properties.
Hope this being useful for you.

Showing a modeless form's 'title bar' inside the parent form when it's minimized

I'm trying to implement some complement views inside my application and I would like to have a better layout control over them. I don't know how to explain in words what my desired functionality is, so I made it through with some photoshop help, hoping you could give me a hand to implement it.
My application now looks like this:
(i need reputation to post images so.. sorry for the links)
http://i59.tinypic.com/2ikv8m1.jpg
When I minimize the modeless form which is focused in the previous image, I would like to be able to see it (and handle it to maximize or close) inside my main form as I show in the image below (made it in photoshop)
http://i58.tinypic.com/1e28go.jpg
Hope someone can lead my into a solution and thanks for the support.
EDIT: I need to be able to move that form outside my main form, even to a different monitor.
If you don't want to use the MDI approach, then set TopLevel of the modeless Form to false and add it to the main Forms Controls collection before showing it:
Form frm = new Form();
frm.TopLevel = false;
this.Controls.Add(frm);
frm.Show();
*Obviously changing Form to the correct type of your modeless form.
If i understand what you are trying to do, you want to minimize a certain form but still see it within your app (im assuming like Excel or Word)
You can do something similar to what Idle_Mind said, but enclose both in a Form instead of the parent.
Form fParent = new Form();
fParent.Dock = DockMode.Fill;//i think this is the syntax. Use this if you want the form to fill to the screen
Form fChild = new Form();
fChild.TopLevel = false;
fParent.Controls.Add(fChild);
fChild.Show();
Here, it should minimize to the lower left part of the parent form. You can then size the parent to whatever you want it to be.

Can a Form be used as a user control?

I need to make a UserControl that can be used for multiple projects. But it needs to be a Form so the user can just add a reference to the library and call the form.
I've seen third party companies like Telerik and DevExpress use custom forms that can be added to a project.
How would I accomplish this? I've been looking through SO and various posts from Google, but have not been successful in my searches.
EDIT I was assuming it had to be a UserControl for some reason. But it doesn't. I took the suggestion of just adding a form and calling it from that namespace. Works exactly as needed. Thanks everyone.
Just create the form in your library, make it public, and you can call it from anywhere.
Methods to create and call form are:
YourFormClassName FormForUser = new YourFormClassName();
FormForUser.Show();
FormForUser.ShowDialog();
Maybe I don't understand. If I do, then it's straight forward.
Add a project (ProjectWithReusedForm) to your solution that contains the form to be reused.
Add a reference to ProjectWithReusedForm in the second project where you want to use the form
Add a 'using ProjectWithReusedFormNamespace' to the code where you want to use the form
You then can add the statement ReusedForm myForm = new ReusedForm();
You can create BaseForm (either add it into a project directly by adding .cs file or reference something compiled - class library to example). Then just add a new form to a project
namespace MySolution
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
and chang Form to BaseForm
public partial class Form1 : BaseForm
Just Create form with all controls. and create empty user control
Ex:
do this code inside usercontrol constructor after initialize function
dim obja as new RegForm()
obja.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
obja.Visible = true
obja.Dock = System.Windows.Forms.DockStyle.Full
me.Controls.Add(obja)
You have to be careful here. Your tag lists winforms, so I am assuming you are using .net and UserControls. Winforms only allows a single form per page. You can add multiple UserControls to a page. If you go with the base form route, the programmer will have to add everything else to your base page. UserControls will offer a little more flexibility in that they can be added to an existing page.

specify parent MDI form

I am trying to specify the parent MDI form when showing a form in c#
All the examples suggest just using
FormVariable.Parent = this;
this works ok assuming you want the form to be opened from the parent window all the time.
I want to be able to open a form and set the Parent form to my MDI Parent form by specifying the name.
in VB.net I have used
Me.MdiParent = TheNameOfMyParentForm
When I try anything similar in c#
this.MdiParent = CruxMDI();
I get
'Crux.CruxMDI' is a 'type' but is used like a 'variable'
Form.MdiParent has to reference a concrete instance. So, maybe it would be a good idea to implement a Singleton pattern (you probably don't want to allow multiple parent windows either way, do you?) in your Parent container, so that you can reference it from wherever you need to. Then you'll just type:
this.MdiParent = CruxMDI.Instance;
If you want to add such behavior automatically and it needs to happen in many Forms in your application, you may consider an option when you'd create a custom base class inheriting from Form. That way you specify this once and then you just need to be sure to inherit your new Forms from this baseclass instead of a default Form.
Either way, you need to have some kind of mechanism to reference the instance of your MDI container.

What is best way to pass variables between GUI in Winforms C#?

I've WinForms program with 2 GUI's. I work with one GUI and open another GUI using
var gui = new FormGui("SomeVar", someOthervar);
gui.ShowDialog();
I use ShowDialog() or Show() depending on what I need to get. When I'm done I would like to pass results (sometimes it's 2 strings, sometimes it's more then that) back to the Mother GUI which called Child GUI.
What's the best way to do that? I was thinking about using global variables but not sure if that's best approach?
You can create properties on your FormGui and set those within the form. When you're done with the form, you can grab those properties from your reference to the form:
var gui = new FormGui("SomeVar", someOthervar);
gui.ShowDialog();
var result = gui.Result;
EDIT: Regarding your comment:
Say your child form has some button on it or something that the user can interact with. Or if there's a close button they click on:
private void buttonCloseClick(object sender, EventArgs e)
{
this.Result = new ResultObject()....
}
EDIT #2 Regarding your second comment:
Yes, on your FormGui class, you need to define an object called Result:
public partial class FormGui : Form
{
public ResultObject Result {get;set;}
}
ResultObject is just something I'm making up. The point being that you're in control of FormGui, so you can add any property you want, and then access it on the FormGui object.
You can add a property on the FormGui class that contains the results you want to use in the parent form.
Also, you can use the result of ShowDialog() to pass information back as well - although this is limited values of the DialogResult enum.
You can call with ShowDialog, on the child window, use a new public property to set the result, and when you close the dialog the parent GUI should be able to see the result in the next code line.
The answer by BFree is enough for your task
I am suggesting easy way to add properties to all forms
Make a new class extend it with System.Windows.Form
public class Form : System.Windows.Forms.Form
{
//add properties
}
Check your properties in your forms

Categories