Solved

Adding to a list from a static event

Posted on 2011-09-29
10
210 Views
Last Modified: 2013-12-17
(I don't often write C# code with forms so....)

I have a form that contains a listview.
I wish to add an item to the listview from a static event.
The event is the  FileSystemEventHandler(onchanged) event.
Obviously, this is staic, but how do I write to the list from a static event or method?

Many thanks i advance.
0
Comment
Question by:Spike_66
  • 6
  • 3
10 Comments
 
LVL 7

Expert Comment

by:Eoin Ryan
ID: 36813833
what do you want added when the event triggers? are you reading a file folder or something? what do you want added to the list?

event happens
assign a string a value
add sting to list

something like that?
0
 

Author Comment

by:Spike_66
ID: 36813873
Yes, basically
0
 

Author Comment

by:Spike_66
ID: 36813889
I can get the string value (from the- as in e.Name)- but can't add it to the list view
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Accepted Solution

by:
jagrut_patel earned 500 total points
ID: 36814175
It is not necessary to have Changed event-handler to be static.
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36814190
If you are referring this example then here is it static because event-handler is not accessing any instance members.
0
 

Author Comment

by:Spike_66
ID: 36814327
I'm using something similar.

However, if I remove static, although I can call a method to populate the list, but get the error
"Cross-thread operation not valid. 'lstThing' accessed from a thread other than the thread it was created on.
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36814449
That is a different issue. It is because UI controls have thread affinity.
See if this thread help you to understand how to access UI control from a different thread http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_27283181.html

You can get this error if your FileSystemWatcher is created on a different thread than the one (main thread) on which your listview is created.


0
 

Author Comment

by:Spike_66
ID: 36814500
Hi
I ony posted the error to demonstate why I could remove "static".

Back to the original issue- I can't update the listview from the FileSytemEventhandler(OnChanged).
0
 

Author Comment

by:Spike_66
ID: 36814504
sorry "could" should have read "couldn't"!
0
 

Author Comment

by:Spike_66
ID: 36814717
sorted it!

removed Static (as suggested) then just used a delegate and Invoke

delegate  void MyNewDel(string thing1);
i.e.

private vioid AddThing(string thing2)
{
if Ithis.InvokeRequired)
{
        MyNewDel nd= new MyNewDel(AddThing);
        this.Invoke(nd, new objrct[] {thing2};
}
else
{
       list1.Items.Add(thing2);
}
}
       

Thanks for pointing me in the right direction.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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