How to sumulate "Click" event w/ TreeView control (.Net CF)

G

Guest

I would like to implement the following functionality:

When a user taps a (child) treeNode, an event fires which allows me to get
the selected child treeNode's TAG object, determine it's values, and then
"do something else"...

Intellisense shows the "Click" event, however, this does nothing. I assume
it's not supported in .Net CF?

I see via Design Mode in VS 2003 that the only feasible event is
"AfterSelected". This works, however, it loops through all of the the
treeNodes. I had thought that i could cancel this event via the
TreeViewCancelEventArgs class, however, i cannot manage to make this work.
"Consuming Events" is slightly beyond my understanding. Can
someone provide an example on how to make this event "cancel" rather than
letting it loop through all of my nodes?

Currently, when the AfterSelect event loops though nodes which have not been
selected, i "avoid" exit the event by testing:

if (e.Action ==TreeViewAction.Unknown) return;

If there's a better way to simulate the "click" event, other than
AfterSelect, i am all "ears".

Thanks,

:
 
D

Daniel Moth

There is no Click event; AfterSelect is your only option unfortunately. Vote
for NodeMouseClick to be supported in CF 2.0:
http://lab.msdn.microsoft.com/produ...edbackid=8bb0f504-037b-4c81-aea0-dfcd25185a43

AfterSelect will fire once for a node when it gets selected. The only
problem is that once it is selected you get no more events when the user
taps on it again.

I don't see the "loop through all of my nodes" behaviour you describe. Can
you post a sample that exhibits this behaviour?

Cheers
Daniel
 
G

Guest

Dan, thanks. The fact that AfterSelect fires once for a node, renders this
option useless for my needs. i've attached the gist of my code so that you
can see (maybe) why the SelectAfter event loops through all nodes...

/// <summary>
/// Routine for loading CRF Form.
/// </summary>
/// <param name="sSubjectId"></param>
private void LoadCRFSelectionForm()
{
// Dispose of previously created controls
DisposeDynamicControls();
panelMain.Controls.Clear();
panelMain.BackColor = System.Drawing.Color.White;

// Set Bool to False: Default (Impossible to reach this form w/out having
selected a subject previously)
bIsNewSubject = false;

// Define Contorls
Label lblCode = new Label();
TreeView treeVisits = new TreeView();
Label lblDiagnostic = new Label();
Label lblDesc = new Label();
ImageButton btnSubmit = new ImageButton();
ImageButton btnBack = new ImageButton();
ImageList imgList = new ImageList();


// Set the properties for the controls
lblCode.Size = new System.Drawing.Size(200, kLabelMinHeight);
lblCode.Location = new System.Drawing.Point(kLeftMargin, kTopMargin);
lblCode.Text = "Subject: " + sSubjectId;
lblCode.Font = kFontSmallBold;
lblCode.ForeColor = kDarkRed;

lblDesc.Size = new System.Drawing.Size(panelMain.Width, kLabelMaxHeight);
lblDesc.Location = new System.Drawing.Point(kLeftMargin, lblCode.Bottom +
kControlVSpacing);
lblDesc.Text = "Tap a visit to continue:";
lblDesc.Font = kFontSmall;

imgList.Images.Add(((System.Drawing.Image)(new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.status_tobecompleted.gif")))));
imgList.Images.Add(((System.Drawing.Image)(new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.status_incomplete.gif")))));
imgList.Images.Add(((System.Drawing.Image)(new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.status_check.gif")))));
imgList.Images.Add(((System.Drawing.Image)(new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.subject_data.gif")))));
imgList.ImageSize = new System.Drawing.Size(16, 16);

treeVisits.Size = new System.Drawing.Size(200, 100);
treeVisits.Location = new System.Drawing.Point(kLeftMargin, lblDesc.Bottom);
treeVisits.Font = kFontSmall;
treeVisits.ImageList = imgList;
treeVisits.ShowLines = true;
treeVisits.ShowPlusMinus = false;
treeVisits.ShowRootLines = true;
treeVisits.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(AfterSelect);

// Generate list of Visits & bind to TreeView control
DataSet ds_Visit = utilities.fGetSubjectVisits(iStudyId, sSubjectId);
DataSet ds_CRF = utilities.fGetSubjectCRFs(iStudyId, sSubjectId);

int iPN = 0; // Counter for Parent Node
foreach (DataRow rV in ds_Visit.Tables["SubjectVisits"].Rows)
{
// code to create notes
}

// Add Tree Node for New patient Data
treeVisits.Nodes.Insert(0, (new TreeNode("Subject Data")));
treeVisits.Nodes[0].ImageIndex = 3;
treeVisits.Nodes[0].SelectedImageIndex = 3;

// Buttons continuar y volver.
btnSubmit.Image = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.continuar.gif"));
btnSubmit.Image_On = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.continuar_click.gif"));
btnSubmit.Size = new Size(78, 16);
btnSubmit.Location = new Point(treeVisits.Right - btnSubmit.Width, 160);
btnSubmit.Click += new EventHandler(LoadNewVisitForm);

btnBack.Image = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.buscar.gif"));
btnBack.Image_On = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.buscar_click.gif"));
btnBack.Size = new Size(65, 16);
btnBack.Location = new Point(btnSubmit.Left - btnBack.Width, 160);
btnBack.Click += new EventHandler(LoadSearchForm);

// Set Panel Main & PicBox
panelMain.Height = 190;
picBox.Image = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.vivo_edc.gif"));

// Set Nav Bar
lblTopNavBar.Text = "Clinical Data";
lblTopNavBar.Visible = true;

// Set Help
panelHelp.Visible = true;
iHelpMsg = 1;

// panel main
panelMain.Controls.Add(lblCode);
panelMain.Controls.Add(treeVisits);
panelMain.Controls.Add(btnSubmit);
panelMain.Controls.Add(btnBack);
panelMain.Controls.Add(lblDesc);
panelMain.Controls.Add(btnNewVisit);

}


/// <summary>
/// Tap event - executed after selecting a CRF in the TreeView control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if (e.Action == TreeViewAction.Unknown) return;

// Edit Subject Data (Load Subject Data Form)
if (((TreeViewEventArgs)e).Node.Text == "Subject Data")
{
LoadSubjectDataForm();
return;
}
// Get the string name of the actual study exam
else if (((TreeViewEventArgs)e).Node.Text.IndexOf("Visit") < 0)
{
// Declare local vars
string[] ar_Tag = new string[5];

// string sVisitName = ((TreeView)ctr).SelectedNode.Parent.Text;
ar_Tag = (string[])((TreeView)sender).SelectedNode.Tag;
// do something...

}

// TreeViewCancelEventArgs tvc = new TreeViewCancelEventArgs(e.Node, true,
e.Action);
}
 
D

Daniel Moth

The code you posted seems to be missing the LoadSubjectDataForm method. Does
that do anything with the tree? As much as I like to think of myself as a
human compiler <joke>, I'd rather you posted a small *complete* sample that
I can run (not just code for me to read) :)

On a more serious note, if you need an event of the treeview when an already
selected node is touched you are simply out of luck (and so am I) hence I
pointed you to ladybug.

Note that clearing a treeview and adding/removing treenodes can change the
selected node and will result in AfterSelect being raised multiple times. If
you have a sample that demonstrates a treenode being tapped once by the user
and as a result the AfterSelect firing more than once, please post it.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


charliewest said:
Dan, thanks. The fact that AfterSelect fires once for a node, renders this
option useless for my needs. i've attached the gist of my code so that you
can see (maybe) why the SelectAfter event loops through all nodes...

/// <summary>
/// Routine for loading CRF Form.
/// </summary>
/// <param name="sSubjectId"></param>
private void LoadCRFSelectionForm()
{
// Dispose of previously created controls
DisposeDynamicControls();
panelMain.Controls.Clear();
panelMain.BackColor = System.Drawing.Color.White;

// Set Bool to False: Default (Impossible to reach this form w/out having
selected a subject previously)
bIsNewSubject = false;

// Define Contorls
Label lblCode = new Label();
TreeView treeVisits = new TreeView();
Label lblDiagnostic = new Label();
Label lblDesc = new Label();
ImageButton btnSubmit = new ImageButton();
ImageButton btnBack = new ImageButton();
ImageList imgList = new ImageList();


// Set the properties for the controls
lblCode.Size = new System.Drawing.Size(200, kLabelMinHeight);
lblCode.Location = new System.Drawing.Point(kLeftMargin, kTopMargin);
lblCode.Text = "Subject: " + sSubjectId;
lblCode.Font = kFontSmallBold;
lblCode.ForeColor = kDarkRed;

lblDesc.Size = new System.Drawing.Size(panelMain.Width, kLabelMaxHeight);
lblDesc.Location = new System.Drawing.Point(kLeftMargin, lblCode.Bottom +
kControlVSpacing);
lblDesc.Text = "Tap a visit to continue:";
lblDesc.Font = kFontSmall;

imgList.Images.Add(((System.Drawing.Image)(new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.status_tobecompleted.gif")))));
imgList.Images.Add(((System.Drawing.Image)(new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.status_incomplete.gif")))));
imgList.Images.Add(((System.Drawing.Image)(new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.status_check.gif")))));
imgList.Images.Add(((System.Drawing.Image)(new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.subject_data.gif")))));
imgList.ImageSize = new System.Drawing.Size(16, 16);

treeVisits.Size = new System.Drawing.Size(200, 100);
treeVisits.Location = new System.Drawing.Point(kLeftMargin,
lblDesc.Bottom);
treeVisits.Font = kFontSmall;
treeVisits.ImageList = imgList;
treeVisits.ShowLines = true;
treeVisits.ShowPlusMinus = false;
treeVisits.ShowRootLines = true;
treeVisits.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(AfterSelect);

// Generate list of Visits & bind to TreeView control
DataSet ds_Visit = utilities.fGetSubjectVisits(iStudyId, sSubjectId);
DataSet ds_CRF = utilities.fGetSubjectCRFs(iStudyId, sSubjectId);

int iPN = 0; // Counter for Parent Node
foreach (DataRow rV in ds_Visit.Tables["SubjectVisits"].Rows)
{
// code to create notes
}

// Add Tree Node for New patient Data
treeVisits.Nodes.Insert(0, (new TreeNode("Subject Data")));
treeVisits.Nodes[0].ImageIndex = 3;
treeVisits.Nodes[0].SelectedImageIndex = 3;

// Buttons continuar y volver.
btnSubmit.Image = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.continuar.gif"));
btnSubmit.Image_On = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.continuar_click.gif"));
btnSubmit.Size = new Size(78, 16);
btnSubmit.Location = new Point(treeVisits.Right - btnSubmit.Width, 160);
btnSubmit.Click += new EventHandler(LoadNewVisitForm);

btnBack.Image = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.buscar.gif"));
btnBack.Image_On = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.buscar_click.gif"));
btnBack.Size = new Size(65, 16);
btnBack.Location = new Point(btnSubmit.Left - btnBack.Width, 160);
btnBack.Click += new EventHandler(LoadSearchForm);

// Set Panel Main & PicBox
panelMain.Height = 190;
picBox.Image = new
Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("VivoSC.images.vivo_edc.gif"));

// Set Nav Bar
lblTopNavBar.Text = "Clinical Data";
lblTopNavBar.Visible = true;

// Set Help
panelHelp.Visible = true;
iHelpMsg = 1;

// panel main
panelMain.Controls.Add(lblCode);
panelMain.Controls.Add(treeVisits);
panelMain.Controls.Add(btnSubmit);
panelMain.Controls.Add(btnBack);
panelMain.Controls.Add(lblDesc);
panelMain.Controls.Add(btnNewVisit);

}


/// <summary>
/// Tap event - executed after selecting a CRF in the TreeView control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if (e.Action == TreeViewAction.Unknown) return;

// Edit Subject Data (Load Subject Data Form)
if (((TreeViewEventArgs)e).Node.Text == "Subject Data")
{
LoadSubjectDataForm();
return;
}
// Get the string name of the actual study exam
else if (((TreeViewEventArgs)e).Node.Text.IndexOf("Visit") < 0)
{
// Declare local vars
string[] ar_Tag = new string[5];

// string sVisitName = ((TreeView)ctr).SelectedNode.Parent.Text;
ar_Tag = (string[])((TreeView)sender).SelectedNode.Tag;
// do something...

}

// TreeViewCancelEventArgs tvc = new TreeViewCancelEventArgs(e.Node, true,
e.Action);
}


Daniel Moth said:
There is no Click event; AfterSelect is your only option unfortunately.
Vote
for NodeMouseClick to be supported in CF 2.0:
http://lab.msdn.microsoft.com/produ...edbackid=8bb0f504-037b-4c81-aea0-dfcd25185a43

AfterSelect will fire once for a node when it gets selected. The only
problem is that once it is selected you get no more events when the user
taps on it again.

I don't see the "loop through all of my nodes" behaviour you describe.
Can
you post a sample that exhibits this behaviour?

Cheers
Daniel
 

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