Treeview and Checkbox question

E

Elmo Watson

Is there any way to make checking a checkbox in a Treeview mutually
exclusive?

That is, to make it so that only one item may be selected....
 
R

Robbe Morris - [MVP] C#

You have to write your own code for that. If the current node
has just been checked, iterate through the tree and uncheck every
other node. If the current node is unchecked, do nothing.
 
E

Elmo Watson

What Treeview event do I use for determining whether a checkbox has been
checked?


Robbe Morris - [MVP] C# said:
You have to write your own code for that. If the current node
has just been checked, iterate through the tree and uncheck every
other node. If the current node is unchecked, do nothing.

--
Robbe Morris [Microsoft MVP - Visual C#]
.NET Setup Deployment - MSI, Cassini, SQL Server, NTFS
http://www.eggheadcafe.com/tutorial...5ab-35ca33da0f65/net-setup-deployment--m.aspx



Elmo Watson said:
Is there any way to make checking a checkbox in a Treeview mutually
exclusive?

That is, to make it so that only one item may be selected....
 
E

Elmo Watson

I've got this code:

If Treeview1.SelectedNode.Checked = True Then
For Each nd As TreeNode In Treeview1.Nodes
If nd.Text <> Treeview1.SelectedNode.Text Then
nd.Checked = False
End If
Next
End If
I've tried this in the NodeMouseClick event
but, when I click on the checkbox in the treeview, the node is not selected,
so I get the old
'object reference not set to an instance of an object'
error

I need to, when I check the node (I guess), have the node selected at the
same time - - am I right?

If so, how do I do that?



Robbe Morris - [MVP] C# said:
You have to write your own code for that. If the current node
has just been checked, iterate through the tree and uncheck every
other node. If the current node is unchecked, do nothing.

--
Robbe Morris [Microsoft MVP - Visual C#]
.NET Setup Deployment - MSI, Cassini, SQL Server, NTFS
http://www.eggheadcafe.com/tutorial...5ab-35ca33da0f65/net-setup-deployment--m.aspx



Elmo Watson said:
Is there any way to make checking a checkbox in a Treeview mutually
exclusive?

That is, to make it so that only one item may be selected....
 
R

Robbe Morris - [MVP] C#

You've always got to check to see if .SelectedNode != null
first.

Here, download this

http://www.eggheadcafe.com/tutorial...8029-26c15de209d1/net-treeview-faq--drag.aspx

It is a TreeView basics code sample that demonstrates some things you'll
need.

--
Robbe Morris [Microsoft MVP - Visual C#]
..NET Setup Deployment - MSI, Cassini, SQL Server, NTFS
http://www.eggheadcafe.com/tutorial...5ab-35ca33da0f65/net-setup-deployment--m.aspx



Elmo Watson said:
I've got this code:

If Treeview1.SelectedNode.Checked = True Then
For Each nd As TreeNode In Treeview1.Nodes
If nd.Text <> Treeview1.SelectedNode.Text Then
nd.Checked = False
End If
Next
End If
I've tried this in the NodeMouseClick event
but, when I click on the checkbox in the treeview, the node is not
selected, so I get the old
'object reference not set to an instance of an object'
error

I need to, when I check the node (I guess), have the node selected at the
same time - - am I right?

If so, how do I do that?



Robbe Morris - [MVP] C# said:
You have to write your own code for that. If the current node
has just been checked, iterate through the tree and uncheck every
other node. If the current node is unchecked, do nothing.

--
Robbe Morris [Microsoft MVP - Visual C#]
.NET Setup Deployment - MSI, Cassini, SQL Server, NTFS
http://www.eggheadcafe.com/tutorial...5ab-35ca33da0f65/net-setup-deployment--m.aspx



Elmo Watson said:
Is there any way to make checking a checkbox in a Treeview mutually
exclusive?

That is, to make it so that only one item may be selected....
 

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