Form location relative to another form - c#

I have a button on a form "Form1", when users click this button, another form "Form2" will pop up. How can I adjust Form2's location so that it is at the center of the Form1 when it pops up? I tried adjusting the location properties but it is not working.
Thanks for the help.

You need to set the StartPosition property of Form2 to CenterParent and show the form using
Form2 form2 = new Form2();
form2.ShowDialog();
If you don't want to show the form as dialog follow the confirmed answer of this question:
How do you set the StartPosition of a Windows Forms form using code?

Related

How to Bring One Form on Another form

Here Form2 is Drawing Viewer And Form3 is toolbar when form2 is maximized form3 is get to back of form2. I want form3(Toolbar) on form3. if i minimize or maximize still form3 should be in front of form2. How can i bring form3 in front of form2
In WinForms there is control named statusStrip. Maybe will be better to use it instead of new form.
Anyway, if you want to do it in your way, you can put in your Form2 to the bottom Panel, in which you can load your Form3 to.
Try using in a method where you have shown form 2
Form Form2 = new Form();
Form2.MdiParent = this;
Form2.Show();
Refer below link for more details :
https://www.codeproject.com/Articles/3553/Introduction-to-MDI-Forms-with-C

creating the windows forms with pages

I have created the multiple windows form in C#.For EX. We have two forms and we have next button in Form1 and previous button in Form2 . But when we try to go form Form2 to Form1 using previous button Form2 is not closing and Form1 is appearing on Form2. To show forms we are using ShowDialog. So how can we create the form one linked to another as pages.
What you can do is like this:
In form1's button handler,
form2 f2=new form2();
f2.show();
this.hide();
And in Form2's button handler,
form1 f1=new form1();
f1.show();
this.hide();
But make sure you write proper code in close button handler as the forms are just hidden. Not closed.
It looks like you're trying to implement a "wizard" in your application.
The more typical approach is to have each "page" as a control/user control and load the appropriate step in the same dialog as the user moves along.
You may benefit from looking at some examples leveraging existing libraries to make this simpler. One example is here: https://www.codeproject.com/articles/120607/simple-wizard-for-winforms
This answer also covers this topic and provides a few more resources:
Which wizard control can I use in a WinForms application?

How to open a child form from another child form?

I am working on a Windows application. I have a MainForm (Parent) and several childForm. There is a listview in MainForm that contains a childForm name list and by clicking on each name in the list, the relevant childForm shows and the previous ChildForm closes.
I use this codes to show childForm and close the previous childForm in MainForm.cs (ParentForm):
CloseForms();
frm_draft = new frm_ShowDraft();
frm_draft.MdiParent = this;
frm_draft.Show();
CloseForm() is a method that checks, which childForm is runnig and closes it.
So far everything is good.
In one of the childforms there is a Button. When the user clicks on it, it should close this childForm and show another. But when I click on the button, childForm2 shows out of MainForm. How can I show it inside of MainForm?
My code in the button click event:
this.close();
frm_c2 = new frm_child2();
frm_c2.MdiParent = new MainForm().ParentForm; /// Or this.MdiForm
frm_c2.Show();
You should set same MdiForm and call Close at the end:
frm_c2 = new frm_child2();
frm_cLetter.MdiParent = this.MdiParent;
frm_cLetter.Show();
this.Close();
http://www.independent-software.com/weifenluo-dockpanelsuite-tutorial-cookbook/
To show a child form in the main form use the WeiFen Luo library.
This control will make it easier to dock forms into your main form visual studio docking screens
Form with 3 forms inside:
Make sure the IsMdiContainter prop is true.
Example:
public Form1()
{
InitializeComponent();
Form2 f2 = new Form2(); // create new form
// dockPanel is an control from WeiFen Luo more info see the link
// dockPanel control is docked in your mainform.
// this will open Form2 in the dockPanel and align it left
f2.Show(dockPanel, DockState.DockLeft);
}
More docking options:
DockState.Fill docks form in over the hole dockPanel
DockState.Right docks form at the rightside of the dockPanel
DockState.Top docks form at the topside of the dockPanel
for more option check the link
This control will handel responsifnes of the docking forms and will handle allot of positioning calculations for you.

How to prevent clicks outside the current form?

I want the form which will open when the user click a button, I don't want the user to click anywhere else outside this form(preventing him from clicking any other buttons on the parent form), I want to restrict his actions inside this form until he close it (For sure this form is top most), and I want the form to make alert(flicker) to any clicks outside it so the user will understand that he have to close it first.
When opening the form instead of
Form form = new Form();
form.Show();
use
Form form = new Form();
form.ShowDialog()
Super easy fix!
I assume your code to show the form right now is
form.Show();
To create/show an instance of the form that is modal (restricts clicking to that form) all you need to change is that line to:
form.ShowDialog();
Liam

How can I access the controls from another running window form in c#

Actually, I am a newbie for C#. In my program, I got two window forms.Each window forms have one label and one activate button.Once I start(load) the program,two window forms will pop-up together.My intention is when I click on the form1 activate button,my form1 label's back colour will change to "green"(to let user know this form1 is activated).Up to here I can write the soft code.But when I click on the form2 activate button,form1 will no longer active, my form2 will be activated and form2 label's back colour will also change to "green".Since form1 is no longer active,I would like to change my form1 label's back colour to red.How can I change it?
How I write my program so far.
In form1 activate button click
1.form1 label's back colour = Colour.green // to let user know form1 is activate upon button click
2. form2 f2 = new form2();
f2.(form2 label's).BackColour = Colour.red
There is no change on form2.
when I add f2.Show() , once I click the activate button on form1 , form2 will pop-up with red label back colour.
Please remember that I am running both form1 and form2 on my Mainform.
Thanks In Advance.
Your forms are just regular c# classes, and the form controls are private members of these classes.
If you need to have public access to few of those controls, just create a public property for those controls.
public Button MyColorButton
{
get { return MyButton; }
}

Categories