Question

It should get text from textbox3 from form1 but it doesn't. Preview form is picturebox, I want to Load image by picureBox1.Load(url_from_textbox3_form_form1) and change form size (width = picturebox.image.with and height = same). If I paste text in visual studio editor all works fine.

Form1:

public string previewImageUrl
        {
            get { return textBox3.Text; }
        }

private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (textBox3.Text != null)
            {
                previewImage frm = new previewImage();
                frm.ShowDialog(this);
            }
            else
                MessageBox.Show("You're an idiot.", "LOL", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

previewImage form:

public void load()
        {
            Form1 frm = new Form1();
            string url = frm.previewImageUrl;
            MessageBox.Show(url);
            try
            {
                pictureBox1.Load(url);
                this.Width = pictureBox1.Image.Width;
                this.Height = pictureBox1.Image.Height;
                MessageBox.Show(pictureBox1.Image.Height.ToString() + " ; " + pictureBox1.Image.Width.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("Something went wrong!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void previewImage_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(load));
            thread.Start();
        }

This thread is not needed. I just tried multiple methods with no effect.

Was it helpful?

Solution

The thing is that you have ceated a new instance of the form1 like this:

 Form1 frm = new Form1();

So what you really is gets here is a new instance of the form1 (with a empty textBox3). What you would have to do is to pass the value (TextBox3) into the previewImage form Or add the value as a parameter into the previewImage form.

Here is a way to do it:

  1. Add a property in the previewImage form, like this:

    Public Property imageUrl As String

  2. In the Form1 you would do like this:

    if (textBox3.Text != null)

    {

     previewImage frm = new previewImage();
    
     frm.imageUrl=TextBox3.Text;
    
     frm.ShowDialog(this);
    

    }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top