Solved

Make a pictureBox public

Posted on 2006-07-15
6
390 Views
Last Modified: 2008-03-17
Hi, I got "The name 'pictureBox1' does not exist in the current context".
Should I make the pictureBox public? or does there have a simplier way?

using System;
...
namespace StartThreading
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread Receiver = new Thread(new ThreadStart(StartThreading.Receiver.receiverStart));
            Receiver.Start();
        }

    }
    public class Receiver
    {
        public static void receiverStart()
        {
             //The name 'pictureBox1' does not exist in the current context
             pictureBox1.Image = (Bitmap)converter.ConvertFrom(receiveBytes);
        }
    }
}
0
Comment
Question by:ddlam
  • 2
  • 2
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
It cannot access the PictureBox because it does not have a reference to either the PictureBox or the Form containing the PictureBox.  Either pass in one of those references to the class, or get rid of the class altogether:

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread Receiver = new Thread(new ThreadStart(receiverStart));
            Receiver.Start();
        }

        private void receiverStart()
        {
             pictureBox1.Image = (Bitmap)converter.ConvertFrom(receiveBytes);
        }

At any rate, you should be using Delegates and Invoke() to update the PictureBox from a Thread.

To pass in the references, don't use a static method.  Instead, create an instance of your class and pass in the reference to be stored in local variables.  Then create a Start() method that creates the thread internally and starts it.
0
 
LVL 9

Author Comment

by:ddlam
Comment Utility
Could you show more code for my reference?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
Comment Utility
I'm not at my computer (am inbetween houses at the moment)...

    public class Receiver
    {
        public PictureBox pb;

        public void receiverStart()
        {
             //The name 'pictureBox1' does not exist in the current context
             pictureBox1.Image = (Bitmap)converter.ConvertFrom(receiveBytes);
        }
    }

Then declare an instance and pass in the reference:

        private Receiver myReceiver;

        private void Form1_Load(object sender, EventArgs e)
        {
            myReceiver = new Receiver();
            myReceiver.pb = this.PictureBox1;
            Thread Receiver = new Thread(new ThreadStart(myReceiver.receiverStart));
            Receiver.Start();
        }
0
 
LVL 5

Accepted Solution

by:
paulb1989 earned 50 total points
Comment Utility
I think you meant:

pb.Image = (Bitmap)converter.ConvertFrom(receiveBytes);

In any case, I'd do it like this:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Receiver myReceiver = new Receiver();
            myReceiver.form = this;
            Thread Receiver = new Thread(new ThreadStart(myReceiver.receiverStart));
            Receiver.Start();
        }

        public void UpdatePictureBox(Image image)
        {
            pictureBox1.Image = image;
        }
    }

    public class Receiver
    {
        public Form1 form;

        public static void receiverStart()
        {
            typeof(Form1).GetMethod("UpdatePictureBox").Invoke(form, new object[] {(Bitmap)converter.ConvertFrom(receiveBytes)})
        }
    }

That way, you're not going to have issues with accessing the picturebox from another thread.
0
 
LVL 9

Author Comment

by:ddlam
Comment Utility
Sorry for late replying ;)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now