Capture Enter-Event on Treenode
Hallo,
I want to display a dialog when i press enter on the treenode. It works
. But when the dialog is displayed and i want to leave the dialog again with enter, the dialog gets again displayed instead of getting closed. When i press enter with dialog open, the treenode is selected even though the focus is on the dialog and this causes the dialog to be dispalyed again... Am i wrong using the KeyPress Event on the tree node? How can i solve this issue? Can anybody please help me?
regards
swingme
[572 byte] By [
swingme] at [2008-1-5]
Hai Dave,
I got the problem solved. Still i have pasted the code which caused the problem.
Code Snippet
private void treeViewKeyUp(object sender, KeyEventArgs e)
{
TreeNode node = tv.SelectedNode;
int level = node.Level;
bool NodeSelected = node.IsSelected;
if (e.KeyCode == Keys.Enter && NodeSelected && level == 1 ){
Refresh(tv.SelectedNode.Text);
}
}
In Refresh then the following section will cause the Dialog to be displayed
DialogResult dlgRes = myDlg.ShowDialog();
if (dlgRes.Equals(DialogResult.OK)) {
do something
}
After displaying dialog and leave with enter i land again in treeViewKeyUp.
Now i have added the following in treeViewKeyUp after calling Refresh.
Code Snippet
e.Handled = true;
With that is my problem solved..
Thanks a lot for your interest
Regards
swingme
hallo dave,
i thought i got the problem solved...but not at all! here one sample code...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void treeView1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
MessageBox.Show(treeView1.SelectedNode.Text);
}
}
}
namespace WindowsApplication5
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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()
{
System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Node1");
System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Node0", new System.Windows.Forms.TreeNode[] {
treeNode1});
System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Node2");
System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Node3");
this.panel1 = new System.Windows.Forms.Panel();
this.treeView1 = new System.Windows.Forms.TreeView();
this.panel2 = new System.Windows.Forms.Panel();
this.textBox1 = new System.Windows.Forms.TextBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.treeView1);
this.panel1.Location = new System.Drawing.Point(46, 22);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 233);
this.panel1.TabIndex = 0;
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(23, 3);
this.treeView1.Name = "treeView1";
treeNode1.Name = "Node1";
treeNode1.Text = "Node1";
treeNode2.Name = "Node0";
treeNode2.Text = "Node0";
treeNode3.Name = "Node2";
treeNode3.Text = "Node2";
treeNode4.Name = "Node3";
treeNode4.Text = "Node3";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
treeNode2,
treeNode3,
treeNode4});
this.treeView1.Size = new System.Drawing.Size(121, 192);
this.treeView1.TabIndex = 0;
this.treeView1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.treeView1_KeyUp);
//
// panel2
//
this.panel2.Controls.Add(this.textBox1);
this.panel2.Location = new System.Drawing.Point(46, 314);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(200, 100);
this.panel2.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(26, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(409, 443);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.TextBox textBox1;
}
}
how can i avoid getting the message box displayed again and again? is this the right way to handle enter event on tree node? i have implemented some logic (not in sample code)which myself won't understand to get rid of this problem. i got it solved almost...but one more case remains unsolved. so i just want to know if there is any other way to handle this issue..please help me...
regards
swingme