问题描述
我的项目中有多种形式. Form1包含一个显示jpeg的pictureBox.在Form2中,我有一个trackBar,我想控制Form1中图像的缩放级别.为了简单起见,我只需要2或3个缩放级别.我已在Designer视图中将pictureBox设置为public.但是,当我尝试在Form2中引用pictureBox时,它说它不存在.下面是我用来在Form1中调用Form2的代码
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)从单独的形式更改pictureBox1的属性.2)创建一个简单的缩放公式.
1) Changing the properties of pictureBox1 from a separate form.2) Creating a simple Zoom Formula.
推荐答案
1)将form1引用传递给form2的构造函数:
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;
}
然后,在Form2s TrackBar_Scroll事件中,通过私有成员变量form1引用PictureBox:form1.PictureBox1.Property
Then in Form2s TrackBar_Scroll event reference the PictureBox via the private member variable form1: form1.PictureBox1.Property
2)使用PictureBox放大图片,以便您可以使用鼠标滚轮进行缩放
更好的方法是事件:
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);
}
}
这篇关于使用两种形式缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!