By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,088 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,088 IT Pros & Developers. It's quick & easy.

Problem with TreeView CheckBox..

P: n/a

I cratee a form to meet the fallowing requirement.
1. If any node is selected the corresponding checkbox should be checked
2. If any checkbox is clicked the corresponding node should be selected

1st requirement is working ,but, 2nd requirement is not wokring properly.
when I click a checkbox the coresponding node is being selected , but
checkbox is not in checked mode. To get the check box in checked mode i have
to click two times or perform double click. I submitted the code here
please find.
HOw can I get the checkbox should be checked in first attempt and node
shuld also be selected?

Regards
Kiran Kumar

-------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TreeView3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.CheckBoxes = true;
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(56, 48);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("Node0"),
new System.Windows.Forms.TreeNode("Node1"),
new System.Windows.Forms.TreeNode("Node2"),
new System.Windows.Forms.TreeNode("Node3"),
new System.Windows.Forms.TreeNode("Node4"),
new System.Windows.Forms.TreeNode("Node5"),
new System.Windows.Forms.TreeNode("Node6"),
new System.Windows.Forms.TreeNode("Node7"),
new System.Windows.Forms.TreeNode("Node8")});
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(312, 312);
this.treeView1.TabIndex = 0;
this.treeView1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.treeVi ew1_MouseDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(440, 462);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
TreeNode node = treeView1.GetNodeAt(new Point(e.X,e.Y));
if( node != null )
{
treeView1.SelectedNode = node;
treeView1.SelectedNode.Checked = true;
}
}
}
}

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Kiran,

you should register the AfterSelect event of the TreeView
and it will be easy to select and unselect the Checkboxes
of the node.

private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if(!this.treeView1.IsAccessible)
return;
if(e.Node.Checked)
e.Node.Checked = false;
else
e.Node.Checked = true;
}

Cheers
Lars Behrmann

_________________
Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net
kiran schrieb:
I cratee a form to meet the fallowing requirement.
1. If any node is selected the corresponding checkbox should be checked
2. If any checkbox is clicked the corresponding node should be selected

1st requirement is working ,but, 2nd requirement is not wokring properly.
when I click a checkbox the coresponding node is being selected , but
checkbox is not in checked mode. To get the check box in checked mode i have
to click two times or perform double click. I submitted the code here
please find.
HOw can I get the checkbox should be checked in first attempt and node
shuld also be selected?

Regards
Kiran Kumar

-------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TreeView3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.CheckBoxes = true;
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(56, 48);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("Node0"),
new System.Windows.Forms.TreeNode("Node1"),
new System.Windows.Forms.TreeNode("Node2"),
new System.Windows.Forms.TreeNode("Node3"),
new System.Windows.Forms.TreeNode("Node4"),
new System.Windows.Forms.TreeNode("Node5"),
new System.Windows.Forms.TreeNode("Node6"),
new System.Windows.Forms.TreeNode("Node7"),
new System.Windows.Forms.TreeNode("Node8")});
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(312, 312);
this.treeView1.TabIndex = 0;
this.treeView1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.treeVi ew1_MouseDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(440, 462);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
TreeNode node = treeView1.GetNodeAt(new Point(e.X,e.Y));
if( node != null )
{
treeView1.SelectedNode = node;
treeView1.SelectedNode.Checked = true;
}
}
}
}


Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.