Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Count nodes (Read 2478 times)
PDM.
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 256
Joined: Dec 2nd, 2010
Count nodes
Sep 10th, 2019 at 2:53am
Print Post  
Hello, there is a easy way to count number nodes created, need some method to count nodes, if i delete if i create, if i copy and paste, need know how many nodes exist, just know total amount of nodes exist on a form at any time.
Also need know how many number of nodes exist on a form of a custom node?
Every custom node i use, have a property what is just a number of item.
If I create a node, need asign value 1,  the second node i create should be 2,  then 3, then 4,
If i delete the node number 4, then when create a new one, that need be numbered 4,
If i select 4 and copy and paste 4,   that new pasted 4 need have numbers 5 6 7 8, etc.

There is some easy way?


Huh
  
Back to top
 
IP Logged
 
PDM.
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 256
Joined: Dec 2nd, 2010
Re: Count nodes
Reply #1 - Sep 10th, 2019 at 3:32am
Print Post  
The first issue, i supose i should use event NodeCreated.

I just made a simple check:

private void form_NodeCreated(object sender, NodeEventArgs e)
{
MessageBox.Show("Created!");
}


When create a node, no event fired, or at least i not see the message box. Cheesy


I think the code should be something like this for count specific nodes:


int counter = 0;
private void form_NodeCreated(object sender, NodeEventArgs e)
{

foreach (var node in form.Nodes)
{
var adapter = node as DiagramNodeAdapter;
if (adapter != null && adapter.Control is MyCustomNode)
{
counter++;
// MessageBox.Show(Convert.ToString(counter));
MyCustomNode Prop = (MyCustomNode)adapter.Control;

if (Prop.Number == 0)
{
Prop.Number = counter;
}
}
}
}


There is no event fired when i create node, so the code wont run.
Anyway this basic code is wrong, duet there is some other rules handle all this behaviour.
The form only can contain up to 8 nodes of type MyCustomNode.
All MyCustomNode need be automatic numbered as 1, 2, 3, 4, 5, 6, 7, 8
Now imagine have 8 nodes created, so that nodes are asigned as 1 to 8 and now i delete MyCustomNod number 5, when create again a new one MyCustomNode need number that as 5 again duet all others already exist.


Well i find the way to doit, i think i need check first numbers already asigned, anyway the first thing is NodeCreated fire event when node is created, and that not happen.
Why?

« Last Edit: Sep 10th, 2019 at 4:42am by PDM. »  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3148
Joined: Oct 19th, 2005
Re: Count nodes
Reply #2 - Sep 10th, 2019 at 5:49am
Print Post  
NodeCreated is raised for nodes drawn by the user. For ones created by your code, you can just call a common method both from NodeCreated handler and after adding your instance to diagram.Nodes. There's also an ItemAdded event raised both for interactively and programmatically added items.

Finding available id should look like this -

Code
Select All
var identifiers = diagram.Nodes.
    Where(n => n is MyCustomNode).
    Select(n => (int)n.Id).
    OrderBy(id => id);

int availableId = 0;
foreach (int id in identifiers)
{
    if (id != availableId)
        break;
    availableId++;
}
Debug.WriteLine(availableId); 



Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3148
Joined: Oct 19th, 2005
Re: Count nodes
Reply #3 - Sep 10th, 2019 at 6:10am
Print Post  
something like -

Code
Select All
void SetupNewNode(node) {...}

void OnNodeCreated(sender, e)
{
    SetupNewNode(e.Node);
}

var node = new MyNode();
diagram.Nodes.Add(node);
SetupNewNode(node); 

  
Back to top
 
IP Logged
 
PDM.
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 256
Joined: Dec 2nd, 2010
Re: Count nodes
Reply #4 - Sep 10th, 2019 at 6:13am
Print Post  
Thanks!!
I checking with you previous instructions.


private void form_ItemAdded(object sender, ItemEventArgs e)
{
var identifiers = form.Nodes.Where(n => n is LabelFrameLCD).Select(n => (int)n.Id).OrderBy(id => id);

int availableId = 0;
foreach (int id in identifiers)
{
if (id != availableId)
break;
availableId++;
}

}


Available id is always 0, chekcing what is wrong, some idea?

For sure LabelFrameLCD is my custom node.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3148
Joined: Oct 19th, 2005
Re: Count nodes
Reply #5 - Sep 10th, 2019 at 6:19am
Print Post  
That code is assuming you set node.Id property. You will have to modify the query if assigning your identifiers to another property.
  
Back to top
 
IP Logged
 
PDM.
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 256
Joined: Dec 2nd, 2010
Re: Count nodes
Reply #6 - Sep 10th, 2019 at 6:38am
Print Post  
Thanks for your help.
Now trying to understand how setup ID to a node.
I can show some example, or link I appreciate.

If not no problem!!
As usual thk for the help.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3148
Joined: Oct 19th, 2005
Re: Count nodes
Reply #7 - Sep 10th, 2019 at 7:32am
Print Post  
just set node.Id = availabelId once you found which one is available. Then next diagram.Nodes queries will find the next available ids. I see you mention a custom number property of yours, you should be able to replace Id with Number in the query, with some added type-casting to convert to your node class.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint