private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
Form editorForm = new Form();
editorForm.Size = new Size(600, 600);
editorForm.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
var picture = new PictureBox
{
Name = "pictureBox",
Anchor = AnchorStyles.None,
Size = new Size(500, 500),
};
editorForm.Controls.Add(picture);
Bitmap bmp = new Bitmap(Image.FromFile(@"D:\test.jpg"));
CenterPictureBox(picture, bmp);
editorForm.Show();
}
private void CenterPictureBox(PictureBox picBox, Bitmap picImage)
{
picBox.Image = picImage;
picBox.Location = new Point((picBox.Parent.ClientSize.Width / 2) - (picImage.Width / 2),
(picBox.Parent.ClientSize.Height / 2) - (picImage.Height / 2));
picBox.Refresh();
}
For setting the pictureBox(picture) to be in the center of the new form(editorForm) it's working fine.
now i want to set the location of the editorForm to be in the center of form1. i tried this
editorForm.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
but it's not working the editorForm is on the left side of the form1
CodePudding user response:
After further review, there is more to this than appears in relation to the picture box comparison. 1) the form that “contains” the picture box… KNOWS where its top left Location is… AND 2) it knows the SIZE of the picture box.
If you create a new form to display, then in order to center it as you describe, you will need to know what that form’s size is… which you wont necessarily know until the form is displayed.
Also, even if you DID KNOW these things… you can set the second forms Location like…
EditorForm editorForm = new EditorForm();
editorForm.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
all day long and it will NOT get set since the form is NOT displayed. In other words, in order to center the second form, we need it displayed and this is going to be a challenge from the first form.
So it should appear clear that we need the second form to do this centering. However its not going to know anything about the parent form… UNLESS … we pass that info to the second form. If the second form KNEW what the parent forms width and heigh and top left x and y values were… then we COULD computer the top left x, y values for the second form.
Therefore, a simple solution is to pass the first forms Location and Size to the second form and let it set the location when it loads. We will need to set the second forms Location in its Load event. If we put the code in the form’s constructor, then as mentioned previously, this setting will be ignored as it has not yet been displayed. Therefore we will need to create two global variables to hold the passed in values. We then can use those variables in the second forms load event.
About the only thing you need to look for is IF the second form is larger in Size than the first form. If we ignore this, then the second form may be displayed outside the screen bounds and the user will possibly not be able to use it. A simple fix is to check the computed top left x and y values and if either value is less than 0, then simply set it to zero.
Below is an example of what is described above…
Point ParentTopLeft;
Size ParentSize;
public Form2(Point parentTopLeft, Size parentSize) {
InitializeComponent();
ParentTopLeft = parentTopLeft;
ParentSize = parentSize;
}
private void Form2_Load(object sender, EventArgs e) {
int tempY = (ParentSize.Height - this.Height) / 2;
int tempX = (ParentSize.Width - this.Width) / 2;
int newX = ParentTopLeft.X tempX;
int newY = ParentTopLeft.Y tempY;
if (newX < 0) {
newX = 0;
}
if (newY < 0) {
newY = 0;
}
this.Location = new Point(newX, newY);
}
Usage...
Form2 f2 = new Form2(this.Location, this.Size);
CodePudding user response:
try it
Point point = new Point();
Form editorForm = new Form();
point.X = this.Location.X;
point.Y = this.Location.Y;
editorForm.Location = point;
