MarcIT MarcIT - 28 days ago 8
C# Question

selecting a radiobutton opens new form with selected image

I have a winform c# app in which the user can select from various

RadioButton
s with an
ImageBox
next to them.
What I wish to do is, when a
RadioButton
is selected the same image appears on another form to continue the process.
Basically what I need is to transport the selected image from
picturebox1
in
formA
to a
picturebox2
in
formB
.

What I have now is this:

private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
build build = new build();
build.ShowDialog();
}
else if (radioButton2.Checked)
{


...

This only opens the form
build
which has the
PictureBox
in with I want to load the same image from
formA
.

Thanks for the support,

EDIT:

I tried the solution by Mong Zhu but nothing happens when i click the button. How do i indicate the picturebox where i want the picture to display? My code is:

FormA:

private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
build build = new build (@"/Images/2C.png");


FormB:

public partial class build : Form
{
string img = @"/Images/2C.png";
public build(string img)
{
img = @"/Images/2C.png";
}


EDIT 2:

Once again many thanks for the help, So i used your code but now i get the following error:


An unhandled exception of type 'System.NullReferenceException' occurred in xxxx.exe
Additional information: Object reference not set as an object instance.


EDIT 3:

Ok, So, thanks your tips the code advances but now I get the following error:


An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll


Additional information: Invalid characters on path. In the line:

pictureBox2.Load(img_from_A);


I guess this is because of the path i used, the images are stored in the bin\Images folder of the project and they are added to the solution explorer.

The code i use is:

build build = new build("@|DataDirectory|/Images/JAF.jpg");


I also tried:

build build = new build("@../Images/JAF.jpg");


and

build build = new build("@/Images/JAF.jpg");


Same error. Any sugestions And thanks again.

Answer

What you basically need is a field in FormB like:

string image_path;

you can then rewrite the constructor and pass this path to the form when you call it in FormA:

FormB formB = new FormB(image_path_from_formA);

the constructor would initialize the field then:

public FormB(string image_path_from_somewhere)
{
    image_path = image_path_from_somewhere;
}

now you can use this path to load the image into the PictureBox of FormB

EDIT:

I will try to use your code:

FormA:

private void button1_Click(object sender, EventArgs e)
{
    if (radioButton1.Checked)
    {
        build build = new build (@"/Images/2C.png");
    }
}

In FormB you can either store the path for later use or load the picture right away:

public partial class build : Form
{
    // you don't need to initialize it. You will pass the right link
    // later through the constructor
    string img;

    public build(string img_from_A)
    {
        // store for later use
        img = img_from_A;

        // or load it right away
        pictureBox1.Load(img_from_A);
    }
}

EDIT 2:

Please make sure that you try to access any element in the constructor AFTER the InitializeComponent() call:

public partial class build : Form
{
    // you don't need to initialize it. You will pass the right link
    // later through the constructor
    string img;

    public build(string img_from_A)
    {
        // First this has to happen!
        InitializeComponent();


        // store for later use
        img = img_from_A;

        // or load it right away
        pictureBox1.Load(img_from_A);
    }
}

EDIT 3:

if you are using windows and your image is in bin\Images folder you should use a back-slash \ instead of / and use this line:

build build = new build("@..\Images\JAF.jpg");