?
Solved

Make a pictureBox public

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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