Treeview and Checkbox question

  • Thread starter Thread starter Elmo Watson
  • Start date Start date
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....
 
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.
 
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....
 
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....
 
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....
 
Back
Top