本文介绍了将数据从一种形式发送到另一种形式的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个form2,其中我要使用Constructor方法将数据传递给Form.
private void OKbtn_Click(object sender, EventArgs e)<br />
{<br />
Form1 frm = new Form1(txtRead.Text, pictureBox1.Image);<br />
frm.Show();<br />
}<br />
在Form1中,我初始化了另一个构造函数,
public Form1(string strTextBox, Image PicFile)<br />
{<br />
InitializeComponent(); <br />
lb.Text = strTextBox;<br />
pictureBox1.Image = PicFile;<br />
}<br />
在Form1中,我有一个TableLayout,其中每个单元格都有一个图片框和一个标签.当用户指定行数和列数时,将在运行时创建这些单元格.单击任何这些PictureBox打开Form2.用户在Form2中设置一些文本和图像.现在,按下OK按钮,数据将发送到Form1.我必须将图像发送到单元格中的PictureBox,并将文本发送到单元格中的标签.但是,我不知道控件的名称,因为它们是在运行时创建的?在这种情况下我该怎么办?
如果我必须以相同的形式进行宣誓,我会使用
(PictureBox) b=(PictureBox) sender();<br />
// now just change the properties of b.
在按钮单击事件上更改相应的PictureBox的属性.
但是在这里,当用户单击按钮时,将打开Form2并由用户指定图像等.但是(PictureBox) b=(PictureBox) sender();
不能使用,因为它不是必须更改其属性的同一按钮. class ="h2_lin">解决方案
Hi,
I have a form2 in which I am passing data to Form using the Constructor method.
private void OKbtn_Click(object sender, EventArgs e)<br />
{<br />
Form1 frm = new Form1(txtRead.Text, pictureBox1.Image);<br />
frm.Show();<br />
}<br />
In Form1, I initialized another constructor,
public Form1(string strTextBox, Image PicFile)<br />
{<br />
InitializeComponent(); <br />
lb.Text = strTextBox;<br />
pictureBox1.Image = PicFile;<br />
}<br />
In the Form1, I have a TableLayout in which each cell has a picture box and a label. These cells are created at runtime when the user specifies the number of rows and columns. Clicking on any of these PictureBox opens Form2. A user sets some Text and an Image in Form2. Now on pressing the OK button, the data is sent to the Form1. I have to send the image to the PictureBox in the Cell and the Text to the Label in the cell. However, I am not aware of the name of the controls since they are created at runtime? What should I do in this case?
If I would have to perform an oeration on the same form, I would have used
(PictureBox) b=(PictureBox) sender();<br />
// now just change the properties of b.
on the button click event to change the properties of the corresponding PictureBox.
But here, when the user clicks on the button, Form2 opens up and the user specifies the image etc. but (PictureBox) b=(PictureBox) sender();
can''t be used since it''s not the same button whose properties have to be changed.
解决方案
这篇关于将数据从一种形式发送到另一种形式的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!