Adjust the width of the form to fit the size of the SplitContainer
I have a SplitContainer inside a Form with a property Dock
set to Fill
.SplitPanel1 contains an image, so when the image is reduced in size, the right side of the form has white space.
How can I crop the size of the form to fit its content?
I tried
Myform.Size = Myform.splitContainer1.Size;
from the starting form.
But it doesn't work. What am I doing wrong?
UPDATE:
Screenshot
Constructive view
This is the kind of design here, you can see the saliva panel filling the form. There are 2 panels. The left pane contains an image, and the right pane contains another pane.
RuntimeView
This is a runtime view. You can see that the size of the box has decreased. I have set a border for the divider of the container and it takes the full shape
This is the code of the main form where I run the form above
myform.endPointPictureBox1.Width = myform.splitContainer1.Panel1.Width/2;
myform.endPointPictureBox1.Height = myform.splitContainer1.Panel1.Height;
myform.endPointPictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
In the second form I am doing the following Load event
splitContainer1.SplitterDistance = endPointPictureBox1.Width;
splitContainer1.Width = endPointPictureBox1.Width + splitContainer1.Panel2.Width;
this.Width = splitContainer1.Width;
source to share
UPDATED 2
Here is the code you need to do:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"c:\Users\Admin\Desktop\tmp.png");
if(splitContainer1.Orientation== Orientation.Vertical)
{
var prevWidthPanel2 = splitContainer1.Panel2.Width;
splitContainer1.SplitterDistance = pictureBox1.Image.Width;
this.Width = (this.Width - splitContainer1.Panel2.Width) + prevWidthPanel2;
splitContainer1.SplitterDistance = pictureBox1.Width;
}
}
I was doing this on a button click, but I think there is no difference.
This results in the following:
Before clicking:
source to share