Solved

Adding to a list from a static event

Posted on 2011-09-29
10
208 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 6

Expert Comment

by:EoDawg
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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now