How to reflect inputs of one form to another?

G

Guest

Hi all,

i am currently writing a small application which involves two forms. Form A
is used primarily for display purposes while form B is used to gather user
input via a number of textboxes and numeric updowns.

The situation now is that when the user keys in his/her input to form B and
clicks a "add" button, i need to reflect the new inputs on form A. What i
have done now is to start a new thread on form A to wait for changes to take
place on form B. When user clicks the "add" button, a boolean variable
changes to true and the thread spawned from form A attempts to read the value
and reflect it on a Listview control on form A.

However, the problem i face now is that i get an exception which states that
i am not allowed to access members from another thread, meaning the thread
spawned from form A is not allowed to access the listview control on form A.

Is there anything i can do to go around this problem, or is there any other
methods i can use to reflect inputs from form B on form A.

I am using visual c# by the way... Thanks for any help in advance.
 
D

Dave Sexton

Hi paradox81,

You must call ListView.Invoke from your background thread to marshal the call to the UI thread.

delegate void UpdateListViewInternalInvoker(object data);

public void HandleDataChanged(object data)
{
myListView.Invoke(new UpdateListViewInternalInvoker(UpdateListView), data);
}

private void UpdateListViewInternal(object data)
{
// TODO: update ListView
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top