Solved

Make a pictureBox public

Posted on 2006-07-15
6
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 86

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 86

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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