Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 4-Oct-10 19:22pm
v2

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:
C#
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; }
   }
 
Share this answer
 
Comments
shaikh-adil 12-Dec-12 1:15am    
+5
"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[^].
 
Share this answer
 
Comments
shaikh-adil 12-Dec-12 1:15am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900