Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

c# - Zoom using two forms

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...