将数据从一种形式发送到另一种形式的控件 [英] Send data from one form to another form's control

查看:84
本文介绍了将数据从一种形式发送到另一种形式的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个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">解决方案

哦,亲爱的,哦,亲爱的,哦,亲爱的.

重述您的工作:
在Form1中,您具有TableLayout.
当您单击一个单元格时,它将打开一个Form2实例,该实例允许用户选择一个文本文件和一个图像.
当按下Form2中的OKbtn时,您将创建一个Form1的新实例,并将该文件和图像传递给它.
然后显示新的Form1.
然后,您会感到困惑,因为信息没有返回到原始Form1.

返回您的讲义.阅读实例.您需要学习这些东西!

在Form1中,在单元格上单击,请执行以下操作:

 Form2 f2 =  Form2();
f2.ShowDialog();
字符串 myFileName = f2.FileName;
图片myImage = f2.Image; 


并将您的Form2更改为包括以下内容:

public string FileName
   {
   get { return textRead.Text; }
   }
public Image Image
   {
   get { return pictureBox1.Image; }
   }


"使用构造函数方法将数据传递到Form "

这不是造成问题的原因,而是一种使事情困惑的讨厌方法.您的Form2Form1或它的控件无关,它应该只是您所描述的文本和图像获取器.

解决此问题的最佳方法是,在具有所需数据的情况下在Form2中引发一个事件. Form1可以预订此事件并使用Form2数据更新动态控件,该数据应包含在该事件传递的自定义事件args实例中.

这是在框架内完成几乎所有事情的方式,这是有充分理由的!我建议你也这样做.

请参阅此提示 [ 解决方案

Oh dear, oh dear, oh dear.

To rephrase what you are doing:
In Form1, you have a TableLayout.
When you click on a cell, it opens an instance of Form2, which allows the user to select a text file, and an image.
When the OKbtn in Form2 is pressed, you create a new instance of Form1 and pass it the file and the image.
You then show the new Form1.
You are then confused because the information is not going back to the original Form1.

Go back to your lecture notes. Read up on instances. You need to learn this stuff!

In Form1, on cell click, do the following:

Form2 f2 = new Form2();
f2.ShowDialog();
string myFileName = f2.FileName;
Image myImage = f2.Image;


and change your Form2 to include the following:

public string FileName
   {
   get { return textRead.Text; }
   }
public Image Image
   {
   get { return pictureBox1.Image; }
   }


"passing data to Form using the Constructor method"

This is not the cause of your problem, but a nasty way to do it which confuses things. Your Form2 has nothing to do with Form1 or it''s controls, it should just be a Text and Image getter from what you have described.

The best way to deal with this is to raise an event in Form2 when it has the data required. Form1 can subscribe to this event and update the dynamic controls using the Form2 data which should be contained in a custom event args instance passed by the event.

This is the way nearly everything is done within the framework with good reason! I suggest you do the same.

See
this tip[^].


这篇关于将数据从一种形式发送到另一种形式的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发语言最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆