On Wed, 9 Apr 2008 22:46:26 -0700, "Steve Gerrard"
<(E-Mail Removed)> wrote:
>Loren wrote:
>> I have a TreeView population routine that is rather lengthy and have
>> decided to use a BackgroundWorker to run it in a background thread.
>> Since I am unable to access my TreeView control directly in the
>> BackgroundWorker, I create a new instance of a TreeView class in the
>> background thread, populate it, and then return it to my main thread
>> through the e.Result DoWorkEventArgs. All of this works fine.
>>
>> My question is how do I assign this populated TreeView class instance
>> to the TreeView Control on my form? When I set my TreeView control
>> to the populated TreeView class instance nothing is displayed in the
>> control. I have verified that the nodes in the TreeView contain
>> data. Any suggestion that anyone might have would be appreciated.
>
>I had to read that last part a few times myself. It can be confusing. 
>
>Lets say the TreeView on your form is called myTreeView. You are doing something
>like
> myTreeView = CType(e.Result, TreeView)
>and wonder why that doesn't put the new tree view on your form.
>
>The thing is, myTreeView is just a reference variable, pointing to a TreeView.
>It starts out pointing to the one created on your form. After a line like the
>one above, it points to the one you created in your worker thread. But the one
>on the form is still there, it just isn't being pointed to by your myTreeView
>variable anymore. And the new one is not on the form; you would have to put it
>there. Clear as mud?
>
>If you look in the form designer.vb code, you will see something like
> MyForm.Controls.Add (myTreeView)
>which actually puts the control in the forms control collection. There will also
>be a lot of properties set, such as size, position, and so on. You would need to
>remove the TreeView that is in the form's controls collection, and add your new
>one to it, transferring all the settings from the old one to the new one.
>
>Is there anyway to just get a collection of Nodes back, and assign that to the
>TreeView?
If you create the new Treeview in another thread, is it legal to then
add that control to the form and access it from the UI thread?