Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Make a pictureBox public

Posted on 2006-07-15
6
Medium Priority
?
408 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 200 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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

604 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