473,379 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

An object reference is required for the non-static field, method, or property message

The problem is with my listbox on the form.
I am trying to run 2 threads simulataneously an update two different listbox
controls but my functions are complaining that the listbox doesn't have an
object reference, my listboxes were created by the ide and drawn on the
form at design time.
How can I give my listbox a reference?
Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

static List<stringlist = new List<string>();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
new Thread(AddItems1).Start();
new Thread(AddItems2).Start();
}
static void AddItems1()
{
for (int i = 0; i < 10000; i++)
//lock (list)
list.Add("Item " + list.Count);

string[] items;
items = list.ToArray();
foreach (string s in items) Form1.listBox1.Items.Add(s);
}
static void AddItems2()
{
for (int i = 0; i < 10000; i++)
//lock (list)
list.Add("Item " + list.Count);

string[] items;
items = list.ToArray();
foreach (string s in items) Form1.listBox2.Items.Add(s);
}

}

}

Jun 27 '08 #1
2 2254
On Jun 18, 5:58*pm, "Dave" <d...@d.netwrote:
The problem is with my listbox on the form.
I am trying to run 2 threads simulataneously an update two different listbox
controls but my functions are complaining that the listbox doesn't have an
object reference, *my listboxes were created by the ide and drawn on the
form at design time.
How can I give my listbox a reference?
Very simple: make methods you use as thread entry points (in your
case, AddItems1 and AddItems2) non-static.

In addition to that, you cannot call (most) methods and properties on
Windows Forms controls from any thread other than the one which had
created them. In your case, trying to add an item to the ListBox from
another thread will result in an exception. You can work around this
by using Control.Invoke and Control.BeginInvoke as needed - MSDN has
more details and examples, as usual.
Jun 27 '08 #2
I believe the line below, and a similar one later:

foreach (string s in items) Form1.listBox1.Items.Add(s);

should be:

foreach (string s in items) this.listBox1.Items.Add(s);

Hope this help...

"Dave" wrote:
The problem is with my listbox on the form.
I am trying to run 2 threads simulataneously an update two different listbox
controls but my functions are complaining that the listbox doesn't have an
object reference, my listboxes were created by the ide and drawn on the
form at design time.
How can I give my listbox a reference?
Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

static List<stringlist = new List<string>();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
new Thread(AddItems1).Start();
new Thread(AddItems2).Start();
}
static void AddItems1()
{
for (int i = 0; i < 10000; i++)
//lock (list)
list.Add("Item " + list.Count);

string[] items;
items = list.ToArray();
foreach (string s in items) Form1.listBox1.Items.Add(s);
}
static void AddItems2()
{
for (int i = 0; i < 10000; i++)
//lock (list)
list.Add("Item " + list.Count);

string[] items;
items = list.ToArray();
foreach (string s in items) Form1.listBox2.Items.Add(s);
}

}

}

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alexander Tulai | last post by:
Hi, I've started using Java recently so the question may seem trivial. Is it possible to get rid of an object by simply creating a method (let's call it "die") in which one simply includes the...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
6
by: Matthias Kaeppler | last post by:
Hello, during a discussion on a C++ internet forum, some question came up regarding references and the lifetime of the objects they alias. I can't find any clear wording on that in the draft...
12
by: Belebele | last post by:
Suppose that a class A depends on class B because an object of class B is passed into A's constructor. See below: class B { ... }; class A { public: A(B const& b); ... };
5
by: removeps-generic | last post by:
Hi. I'm using placement new to re-initialize part of an object. Is this OK? struct BaseImp { All& r_all; BaseImp(All& all) : r_all(all) }; struct Calc::Imp : public BaseImp
21
by: Andrew Ward | last post by:
Consider the following: #include <iostream> struct X { int x; X() { x = 6; } };
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
10
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class X { public: int value() const { return val; }
4
by: cxf | last post by:
class A{ public: A() { func(0); }; virtual void func(int data) { printf("A1 :%d\n",data); }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.