hazz

Mikael Wiberg has a C# code example on MultiSelect in the Treeview.

He recommends that the BeforeSelect event be used.

I see that when I select a node, the canceleventargs in the BeforeSelect event tell me the nodes index and level along with the parents index and level.

I am employing the DrawNode method as per Hans Passant's suggestion as I need to display a custom backcolor in the rectangle for each text, other than the default select blue.

With the sole requirement that the user be able to select a node at a specific level, level 2, and then when they hold down the shift key and then select the next node which will then define the range of nodes that need to be selected..... how do I do that

By creating an array

I seem to recall that Mikael said that Selnode property needs to be used rather than SelectedNode property.

I am not sure what that means yet.

Thanks,

-Greg



Re: Windows Forms General treeview - shift key - select multi nodes in same level

nobugz

Ah, so that's what you're trying to do. I assume you're trying to avoid using the check boxes. You can still use them internally, just make the checked node look different. Here's an example:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
treeView1.DrawMode = OwnerDrawText;
treeView1.DrawNode += treeView1_DrawNode;
treeView1.NodeMouseClick += treeView1_NodeMouseClick;
}
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) {
// Show checked nodes with an underline
using (SolidBrush br = new SolidBrush(e.Node.TreeView.BackColor))
e.Graphics.FillRectangle(br, e.Node.Bounds);
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = e.Node.TreeView.Font;
if (e.Node.Checked) nodeFont = new Font(nodeFont, FontStyle.Underline);
using (SolidBrush br = new SolidBrush(e.Node.TreeView.ForeColor))
e.Graphics.DrawString(e.Node.Text, nodeFont, br, e.Bounds);
if (e.Node.Checked) nodeFont.Dispose();
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
if (Control.ModifierKeys == Keys.Shift && e.Node.Parent != null) {
// Extend selection
bool check = false;
foreach (TreeNode node in e.Node.Parent.Nodes) {
if (node.Checked) check = true;
node.Checked = check;
if (node == e.Node) break;
}
}
else {
unselectNodes(treeView1.Nodes);
e.Node.Checked = true;
}
}
private void unselectNodes(TreeNodeCollection nodes) {
foreach (TreeNode node in nodes) {
node.Checked = false;
if (node.Nodes != null) unselectNodes(node.Nodes);
}
}
}






Re: Windows Forms General treeview - shift key - select multi nodes in same level

hazz

ah simplicity... checkboxes - of course!

I just added the fillrectangle in the DrawNode event and that is exactly what was needed for this newly added user requirement.

Code Snippet

if (e.Node.Checked)

{

nodeFont = new Font(nodeFont, FontStyle.Bold);

e.Graphics.FillRectangle(Brushes.Bisque, e.Node.Bounds);

}

Yes it makes total sense to handle this in the NodeMouseClick event.

Thank you thank you Hans.

-Greg