PC Review


Reply
Thread Tools Rate Thread

Assigning TreeView Class Instance to a TreeView Control

 
 
Loren
Guest
Posts: n/a
 
      10th Apr 2008
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.

--
Loren Baker
 
Reply With Quote
 
 
 
 
Jack Jackson
Guest
Posts: n/a
 
      10th Apr 2008
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?
 
Reply With Quote
 
Loren
Guest
Posts: n/a
 
      10th Apr 2008
Steve thanks for your reply. You understood my problem exactly … even right
down to myTreeView = CType(e.Result, TreeView) code.

I tried to assign the Nodes collection to the TreeView control, but it’s
read only. But your other suggestion seems to work.

To simplify my problem and to test your suggested solution I created a new
test project with a TreeView control and three buttons. (See the code
below.) btnStart creates a simple test tree using the myTreeViewControl.
btnStartCreateNewTV creates the same test tree except it uses a new TreeView
class instance. btnAddNewChildNode adds a new child and grandchild node to
TreeView control, and seems to work with both ways of creating the test tree.

To address Jack’s concern about creating a the new Treeview in another
thread while the user might change the content of the TreeView control, I’ve
disabled the TreeView control before starting the background thread.

Thanks again for you help.

Public Class Form1

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
myTreeViewControl.Nodes.Clear()
Dim newNode As TreeNode

newNode = New TreeNode("Root Node 0")
myTreeViewControl.Nodes.Add(newNode)

Dim rootNode As TreeNode = myTreeViewControl.Nodes(0)
For i As Integer = 0 To 3
newNode = New TreeNode("Child Node 0" & i.ToString)
rootNode.Nodes.Add(newNode)
Next i

Dim childNode02 As TreeNode = myTreeViewControl.Nodes(0).Nodes(2)
For i As Integer = 0 To 3
newNode = New TreeNode("Grandchild Node 02" & i.ToString)
childNode02.Nodes.Add(newNode)
Next i

End Sub

Private Sub btnStartCreateNewTV_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnStartCreateNewTV.Click
myTreeViewControl.Enabled = False

Dim newTreeView As TreeView = New TreeView

Dim newNode As TreeNode

newNode = New TreeNode("Root Node 0")
newTreeView.Nodes.Add(newNode)

Dim rootNode As TreeNode = newTreeView.Nodes(0)
For i As Integer = 0 To 3
newNode = New TreeNode("Child Node 0" & i.ToString)
rootNode.Nodes.Add(newNode)
Next i

Dim childNode02 As TreeNode = newTreeView.Nodes(0).Nodes(2)
For i As Integer = 0 To 3
newNode = New TreeNode("Grandchild Node 02" & i.ToString)
childNode02.Nodes.Add(newNode)
Next i

' Remove myTreeViewControl from the form
' and add the newTreeView to the form with a name =
"myTreeViewControl"

Me.Controls.Remove(myTreeViewControl)

newTreeView.Name = "myTreeViewControl"
newTreeView.Location = New System.Drawing.Point(12, 12)
newTreeView.Name = "myTreeViewControl"
newTreeView.Size = New System.Drawing.Size(377, 389)
newTreeView.TabIndex = 0

newTreeView.SelectedNode = newTreeView.Nodes(0)
newTreeView.SelectedNode.Expand()

myTreeViewControl = newTreeView
myTreeViewControl.Enabled = True

Me.Controls.Add(myTreeViewControl)

End Sub

Private Sub btnAddNewChildNode_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnAddNewChildNode.Click

myTreeViewControl.Nodes(0).Nodes.Add(New TreeNode("New Child Node
04"))
myTreeViewControl.Nodes(0).Nodes(1).Nodes.Add(New TreeNode("New
Grandchild Node 010"))

End Sub
End Class

--
Loren Baker


"Steve Gerrard" wrote:

> Jack Jackson wrote:
> >
> > 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?

>
> Good question, I'm not really sure. It seems like it should be okay, as long as
> nothing UI related happens before you get it back to the UI thread and add it to
> the form. At that point, all code will be running in the UI thread, and the
> memory itself is okay since it is all the same process.
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recreate a >>C# WinForms<< TreeView "state" -after- treeview rebuild ?? Any TreeView GODS out there ?? frostbb Microsoft C# .NET 6 18th Aug 2008 05:08 PM
HOW TO: Treeview, obtain the first node index in a sorted Treeview =?Utf-8?B?S2V2aW4gTWNDYXJ0bmV5?= Microsoft Access Form Coding 5 21st Aug 2007 04:16 PM
HOW TO: Treeview, obtain the first node index in a sorted Treeview =?Utf-8?B?S2V2aW4gTWNDYXJ0bmV5?= Microsoft Access Forms 0 20th Aug 2007 06:52 PM
Generate treeview in server manipulate treeview in client side Ravi Microsoft Dot NET 0 2nd Sep 2004 04:00 PM
Re: TreeView, TreeView.HideSelection=false, Window.Minimize NotYetaNurd Microsoft Dot NET Framework Forms 2 30th Jun 2003 06:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:05 PM.