Set Location for new form - c#

I use below code to make lightbox effect and it works as i expected. However if i move the parent form it still pop-ups on the center of the screen.
// Execute this code from parent form
Form f = new Form();
f.ShowInTaskbar = false;
f.BackColor = Color.Black;
f.Size = this.Size;
f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f.StartPosition = this.StartPosition;
f.Opacity = 0.6;
f.Show();
So i changed the above code like below;
f.StartPosition = FormStartPosition.CenterParent;
However it still doesn't pop-up center of the parent form.
Also i tried below, It didn't work too;
f.SetBounds(this.Location.X, this.Location.Y,this.Width, this.Height);
I already tried the solutions here;
Show a child form in the centre of Parent form in C#
They also didn't work.
What i want to do is, creating a second form with the same size and same location.

Just changing this line;
f.Show();
to this line, It worked;
f.ShowDialog();

Related

Is there a way to create a new object for form1 after closing a form2?

I'm working on a Agenda using windows forms C#, I'm trying to create a colored picture box for each appointment object in the project. Using this code that is used in a loop for each object in my appointment list im creating each picturebox on the right location on the form1 screen.
PictureBox Point = new PictureBox();
this.Controls.Add(Point);
Point.Location = new Point(obj.Location.X, 45 + obj.Location.Y);
Point.BackColor = color;
Point.Size = new Size(96, 25);
Point.Enabled = false;
Point.Tag = "Point";
Point.TabIndex = 100;
Point.Visible = true;
When I'm calling this method from input on the same form, for example a button click. It will work just fine and create all the picture boxxes as needed. But when I'm calling it from the form2.closed event it wont work. Form 2 is my appointment planner form, when clicking on save on this form it will add a new object to the list, so a new picturebox should be created. I have checked the debug using breakpoints, and strange enough it will go through the create code, but no matter what I do it wont render the pictureboxxes.
I personally think it has to do with the form1 not Initializing when called from form2.closed event. But even when using InitializeComponent(); end Refresh(); inside my code it still doesnt work.
Am I using the wrong event or is there a specific call I need to make to generate the pictureboxxes?
Sorry if my post is lacking code or info, I'm not used to posting on stackoverflow, feel free to ask for more information if needed.
if you run your code from Form2 and you want to add control to Form1 you cannot use "this". Form1 has to be accessible from Form2.
PictureBox Point = new PictureBox();
Point.Location = new Point(obj.Location.X, 45 + obj.Location.Y);
Point.BackColor = color;
Point.Size = new Size(96, 25);
Point.Enabled = false;
Point.Tag = "Point";
Point.TabIndex = 100;
Point.Visible = true;
Form1.Controls.Add(Point);

Child form appearing underneath controls on parent form

I have a C#, .net, Windows Forms application. I have a form set as an MDI container on which I dynamically add buttons. Clicking a button opens a child form. However, the buttons I created appear on top of the child form instead of the child form appearing over (and covering) everything on the parent form. What am I doing wrong?
Here's how I add the buttons to the form:
Button btn1 = new Button();
btn1.BackColor = ColorTranslator.FromHtml("#456EA4");
btn1.Text = department.DepartmentName;
btn1.Location = new System.Drawing.Point(posX, posY);
btn1.Size = new System.Drawing.Size(sizeX, sizeY);
btn1.Font = new System.Drawing.Font("Calibri", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
btn1.ForeColor = Color.WhiteSmoke;
btn1.TabStop = false;
this.Controls.Add(btn1);
Here's is where I open the child form:
frmToBuildFromSchedule frmToBuild = new frmToBuildFromSchedule(department);
frmToBuild.FormClosed += new FormClosedEventHandler(frmToBuildFromSchedule_FormClosed);
frmToBuild.MdiParent = this;
frmToBuild.Show();
Here is the result:
You are putting the buttons directly on the parent MDI form, which is not typical of an MDI style application. Instead, put the code that is currently in the click event of your buttons in a menu option or a ribbon button (those can also be dynamically created).
Alternatively, create a child form, maximize it, and place your buttons on that.
I think the answer by sam on the posted duplicate link is interesting. Rather than using MDI, set form.TopLevel = false; and add the form as a child control. I'm not sure if there are any downsides to this approach.
Form fff = new Form();
fff.Controls.Add(new Button { Text = "Button1" });
fff.Load += delegate {
Form ffff = new Form { TopLevel = false };
fff.Controls.Add(ffff);
ffff.Visible = true;
ffff.BringToFront();
};
Application.Run(fff);

bring to front not working winform c# my code

i have telerik panel i want show to form in front but form not show bringtofront not working. i don't know why please check below image and code, i am clicking product button but product form show back side ;(
frmProductList objForm = new frmProductList();
pndocmain.DockControl(objForm, DockPosition.Fill, DockType.Document);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.TopMost = true;
objForm.BringToFront();
objForm.Show();
first thing, that I would try is show the form with owner. But I really don’t know, why you haven’t that in front even though you have TopMost as true.
frmProductList objForm = new frmProductList();
pndocmain.DockControl(objForm, DockPosition.Fill, DockType.Document);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//objForm.TopMost = true;
//objForm.BringToFront();
objForm.Show(this);

Mdiparent and SplitContainer

I am using a SplitContainter in MDI parent Form.
My problem is I loaded a form in panel1 named First Form. In this First Form with a button I load SecondForm in panel2.
I am using this code:
Form In_but = new SecondForm();
In_but.MdiParent = this.ParentForm;
In_but.TopLevel = false;
this.splitContainer1.Panel2.Controls.Add(In_but);
In_but.Show();
But it's not working. The error is: does not contain definition splitContainer1.
From looking at your code sample, I suspect your problem is when you refer to this.splitContainer, this is your 'First form' on panel 1, and your SplitContainer is on this.ParentForm.
I'd suggest changing that line to this.(ParentForm as <whatever class your parent form is>).splitContainer1.Panel2.Controls.Add(In_but);
try this
frmChild frmChild = new frmChild();
frmChild.TopLevel = false;
frmChild.Parent = this.splitContainer3.Panel2;
frmMasterlistAdministrationAdd.Show();
frmTest fs = new frmTest(); //frmTest is the form that you going to call
fs.MdiParent = this; //the main form is a mdiform and have a splitcontainer with
//two panels
this.splitContainer1.Panel2.Controls.Add(fs); //add the fs form to the panel2
fs.Show(); //show the form

Cannot seem to center mdichild in parent form

I have tried as follows when clicking on a menu but the form still appears to be on the top left of the mdichild.I have also tried manual position but does not work.
Any suggestions?
Form1 form= new Form1{MdiParent = this};
form.StartPosition = FormStartPosition.CenterParent;
form.Show();
try
form.StartPosition = FormStartPosition.CenterScreen ;

Categories

Resources