3

I have a SplitContainer within a Form with the Dock property set to Fill .SplitPanel1 contains a picturebox,so when the picturebox size reduces,the right side of the form has blank space.

How can i trim the form size so that it matches its content?

I have tried

Myform.Size = Myform.splitContainer1.Size;

from the launching form.

But this is not working.What i'm i doing wrong?

UPDATE:

Screenshot

Design View

This is the Design View here you can see that the spiltpanel fills the form. There are 2 panels.The Left Panel contains a picturebox and Right Panel contains another panel.

enter image description here

RuntimeView

This is the runtime view.You can see that the picturebox size has reduced.I have set the splitcontainer to have borders and it occupies the full form

enter image description here

This is the code behind the Main form where i launch 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 do the following the Load event

splitContainer1.SplitterDistance = endPointPictureBox1.Width;
splitContainer1.Width = endPointPictureBox1.Width + splitContainer1.Panel2.Width;
this.Width = splitContainer1.Width;
techno
  • 6,100
  • 16
  • 86
  • 192
  • If the size of a picturebox in SplitPanel1 reduces, how would that imply that the splitcontainer's size would reduce? i.e. tell us more.. Did you use the AutoSize property? – Fixation May 11 '17 at 12:16
  • Set `AutoSize = true`, `AutoSizeMode = AutoSizeMode.GrowAndShrink`. – Alexander Petrov May 11 '17 at 12:18
  • @AlexanderPetrov By setting the properties of the Form as you have mentioned the form gets shrunken and no control is visible. – techno May 11 '17 at 16:51
  • @Fixation The Split Container does not have an `Autosize` property. – techno May 11 '17 at 16:53
  • 2
    @techno if it is not difficult for you please add screenshot of the situation – Samvel Petrosov May 13 '17 at 13:14
  • 1
    What triggers the picture size to reduce? And if the SplitContainer is docked to the form, how does the form have any blank space on the right? The SplitContainer should be completely filling the form if it's docked. Or are you talking about Panel2? You need to show us what you're doing (a screenshot would be a good start) and/or provide some form setup code so that we can reproduce it. As of now it's very unclear what you're trying to do. – Brian Rogers May 13 '17 at 16:11
  • @S.Petrosov Please see the update.. – techno May 14 '17 at 04:38
  • @BrianRogers Please see the update.. – techno May 14 '17 at 04:38
  • @techno you don't want Right Panel to be increased in width and instead you want the form to fit the size of the reduced SplitContainer, right? – Samvel Petrosov May 14 '17 at 07:51
  • @S.Petrosov yes,you are right. – techno May 14 '17 at 10:16

1 Answers1

2

UPDATED 2

Here is code that you need to do that:

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 button click, but I think there is no difference.
The result of this is the following:

Before click:

enter image description here

After click: enter image description here

Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • I had to modify `pictureBox1.Image.Width;` to `pictureBox1.Width` for this to work.This works when the picture is vertical .. when its horizondal the splitpanel2 gets shrunken.. – techno May 14 '17 at 16:52
  • @techno I had set it to pictureBox1.Image.Width thinking that your panel1's width must be always equal to image's width, I have not looked for horizontal case as there was nothing about it in the question – Samvel Petrosov May 14 '17 at 17:24
  • @techno what behavior must be on horizintal case?? – Samvel Petrosov May 14 '17 at 17:26
  • I mean when the picture has around equal width and height(like before click)... the splitpanel2 is covered up by the picturebox.. – techno May 14 '17 at 17:36
  • @techno Is it being so while pictureBox.SizeMode = PictureBoxSizeMode.StretchImage; ?? – Samvel Petrosov May 14 '17 at 18:04
  • @techno As you can see on this recording I'm not getting such behavior https://monosnap.com/file/VR6raBDtcu27UptdM5gIsZomww1OR9 – Samvel Petrosov May 14 '17 at 18:06
  • Thanks for posting the recording .. have you tried placing button for vertical image too.. – techno May 14 '17 at 18:15
  • @techno yes, but it works only if the pictureBox.SizeMode is set to PictureBoxSizeMode.StretchImage. In other cases you will need additional logic for pictureBox.width and pictureBox.height resizing. – Samvel Petrosov May 14 '17 at 18:19
  • sorry.. i was doing something stupid in the Main form,that caused the issue.Your second answer is not what im i looking for.Your previous answer is correct,could you please roll back your answer,so that i can accept it as the solution.Thanks – techno May 16 '17 at 04:19
  • @techno this one? – Samvel Petrosov May 16 '17 at 04:21