Solved

Make a pictureBox public

Posted on 2006-07-15
6
392 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
ID: 17116116
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
ID: 17116798
Could you show more code for my reference?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
ID: 17118944
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
ID: 17119227
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
ID: 17347159
Sorry for late replying ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 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

19 Experts available now in Live!

Get 1:1 Help Now