1

I am extending the PictureBox example in a Microsoft tutorial on Visual Studio. I have a hierarchy of: Form - TableLayoutPanel - Panel - PictureBox; the tableLayoutPanel is set for a 2x2 grid; the Panel is set to span the first two columns so it occupies the top of the form, and there is a checkbox in the lower left cell of the tableLayoutPanel grid and some buttons in its lower right.

I have one of the buttons working to select an image and display it in the picture box. I want checking the checkbox to fit the image into the picture box at the largest size allowed by the space, an unchecking the checkbox to display whatever part of the full-size image will fit in the picture box, with scrollbars to move that view around.

When I check the checkbox, the fitted picture appears as I want it to. When I uncheck the checkbox, it displays that portion of the image that will fit in the space, but it does not display the scrollbars. How do I switch back and forth so that I get scroll bars when the image is bigger than the picture box, and not when the image is fitted in the space, all within these first two cells of a TableLayoutPanel?

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)  // gives state after the change
        {
            picMainPicture.SizeMode = PictureBoxSizeMode.Zoom;
        }
        else
        {
            picMainPicture.SizeMode = PictureBoxSizeMode.AutoSize;
        }
    }

Properties that I think matter for the panel at start:

Anchor: Top, Left
AutoScroll: True
Dock: Fill
ColumnSpan: 2
Location: 4,4
Size: 993, 501 // would happily reduce this if I knew how, it won't let me enter new numbers

Properties that I think matter for the pictureBox:

Anchor: Top, Left
Dock: Fill
Location: 0,0
Size: 993, 501
SizeMode: AutoSize
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
arcy
  • 12,845
  • 12
  • 58
  • 103
  • https://stackoverflow.com/a/4710193/5045688 – Alexander Petrov Nov 03 '19 at 16:42
  • Since the PictureBox is set to `Dock = Fill`, it adjusts to the current container's size. So the container won't scroll. You have to let the PictureBox overflow the container's client area. – Jimi Nov 03 '19 at 16:42
  • In other words, set `Dock = None`. When you set the PictureBox's `SizeMode = AutoSize`, it will overflow (and the container will show the scrollbars), when reset to `SizeMode = Zoom`, it will resize the initial Size (and the container will hide the scrollbars). – Jimi Nov 03 '19 at 16:50
  • @AlexanderPetrov yes, have been through that answer several times. What it said got it working once in my TableLayoutPanel, but I changed something and it quit working. All they have is an example of how it works, without the TableLayoutPanel. – arcy Nov 03 '19 at 17:03
  • @Jimi If I set Dock to None, then when I set to fit-to-frame mode the picture does show across the two TableLayoutPanel columns with scrollbars, great. But when I uncheck that box and set the picture sizemode back to "Autosize" or "Normal", it goes tiny in the upper left corner of the TableLayoutPanel. I want the "Autosize" functionality when I don't have the scrollbars, i.e., I want the entire picture to scale to fill the available space. – arcy Nov 03 '19 at 17:10
  • Use a Panel as the container of your PictureBox. – Jimi Nov 03 '19 at 17:13
  • @Jimi I did that, said so up top -- Form, TableLayoutPanel, Panel, PictureBox – arcy Nov 03 '19 at 17:15
  • Do you have images smaller than the viewport then? I thought the opposite. AutoSize causes the PictureBox to adjust its size to the size of the image it's showing. If the image is smaller than the container's area, the Zoom mode, when set, won't resize the PictureBox back to the container's Client area. You have to do it manually, checking whether the Image is smaller than your current viewport and resize the PictureBox accordingly. I'm not sure, in this case, why scrollbars would appear. If you can show an animation of your layout, maybe... – Jimi Nov 03 '19 at 17:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201793/discussion-between-arcy-and-jimi). – arcy Nov 03 '19 at 17:29
  • _up top_ this is not just z-order but about nesting! – TaW Nov 03 '19 at 17:51

0 Answers0