4

I have a multiple forms in my project. Form1 contains a pictureBox that displays a jpeg. In Form2 I have a trackBar that I would like to control the zoom level of the image in Form1. To keep it simple I only need 2 or 3 zoom levels. I've set the pictureBox to public in the Designer view. However, when I try to reference the pictureBox in Form2 it says it doesn't exist. Below is the code I'm using to call Form2 in Form1

Form2 dataWindow = new Form2();
dataWindow.ShowDialog();

So in short the two things I need help with is:

1) Changing the properties of pictureBox1 from a separate form. 2) Creating a simple Zoom Formula.

  • @Servy has the best advice, I edited my answer to include a full implementation for your convenience. Just add the PictureBox and Button to form1 and TrackBar to form2 and paste in the code. – Jeremy Thompson Nov 24 '12 at 04:20

2 Answers2

3

It's considered bad design to allow other classes to modify the internal controls of the form. The form should be responsible for all of it's components. You shouldn't ever make any of the internal controls public. It's also considered bad practice for a child form to have a reference to the parent form.

The appropriate way to approach this problem is through events. The child form, Form2, should define a public event:

public event Action<int> TrackBarMoved;

Form2 can fire that event when the track bar is moved and pass, as the parameter, the position of the trackbar (if it makes sense to pass something else, such as the zoom level, or whatever else you want, that's fine too).

Form1 can the subscribe to that event when it creates From2 and change the zoom on the picture (internally, from within Form1) based on what the trackbar position is.

Servy
  • 202,030
  • 26
  • 332
  • 449
2

1) Pass a form1 reference into form2's constructor:

Form2 dataWindow = new Form2(this);
dataWindow.Show();

...

private form1 as Form1;
public Form2(Form1 frm1)
{
  form1 = frm1;
}

Then in Form2s TrackBar_Scroll event reference the PictureBox via the private member variable form1: form1.PictureBox1.Property

2) Magnify your pictures using a PictureBox so that you can zoom with the Mouse Wheel


The better way is events:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var form2 = new Form2();
        form2.TrackBarMoved += new Action<int>(ZoomPictureBox);
        form2.ShowDialog();
        form2.TrackBarMoved -= new Action<int>(ZoomPictureBox);

    }

    private void ZoomPictureBox(int zoomFactor)
    {
        pictureBox1.Width = 100 * zoomFactor;
        pictureBox1.Height = 100 * zoomFactor;
    }
}


public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public event Action<int> TrackBarMoved;

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        TrackBarMoved(trackBar1.Value);
    }
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    It's generally considered bad practice to pass a reference of the parent form to the child form. – Servy Nov 24 '12 at 04:04
  • Thanks awnserers. I tried out parts of the example code. Quesiton... there appears to be two actions one before the Show dialog and one after.. I tried the code with only the += new action and it has the same effect as the code without the second -= action.. What is intended purpose of the second action? – Trevor Thompson Nov 24 '12 at 07:07
  • When you subscribe to an Event Handler `+=`, you need to unsubscribe `-=` when your done. Otherwise it could lead to a memory leak. – Jeremy Thompson Nov 24 '12 at 07:10
  • 1
    Thanks again to both of you. The code works great. I'm now creating some scroll bars on the second form using the same concept. a little knowledge in this language seems to go a long way. – Trevor Thompson Nov 24 '12 at 07:10