Page Index Toggle Pages: [1] 2  Send TopicPrint
Hot Topic (More than 10 Replies) Delete node programmatically (Read 8161 times)
AnthonyBrien
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 11
Joined: Feb 5th, 2008
Delete node programmatically
Mar 7th, 2008 at 5:02pm
Print Post  
I'm not sure if this is a bug or if I'm just doing this the wrong way.

After I load a saved diagram (using LoadFromXml), I analyze the diagram and I want to remove nodes that are considered obsolete.

When I try to do diagram.Nodes.Remove(nodeToDelete), I get the following exception:

[quote][b]ArgumentExceptionOccured[/b]
Cannot remove the specified item because it was not found in the specified Collection.[/quote]

In the debugger diagram.Nodes.Contains(nodeToDelete) returns true.

I thought this might have something to do with groups, so I then tried to remove the node from its parent group. That works, but the node remains dangling in the graph. Then if I try diagram.Nodes.Remove() again, I still get the exception.

Any ideas?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Delete node programmatically
Reply #1 - Mar 7th, 2008 at 5:59pm
Print Post  
Are you deleting any other nodes from the NodeDeleted event? That could lead to problems, for example if combined with the Group.AutoDeleteItems feature.

Stoyan
  
Back to top
 
IP Logged
 
AnthonyBrien
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 11
Joined: Feb 5th, 2008
Re: Delete node programmatically
Reply #2 - Mar 14th, 2008 at 2:36pm
Print Post  
No, I am deleting the nodes directly after doing a LoadFromXML. I dont use the NodeDeleted event or the AutoDeleteItems feature.

Basically, I have a language file (*.st) that represents a stateshart diagram, and I have a .diagram.XML file with the same name. When the user loads the .st, I first do a diagram.LoadFromXml(), and then I call a method to bind my statechart data back with the correct nodes and transitions from the diagram. If during the binding any nodes or links in the diagram are not found in my data, I just want to remove them. But I get these exceptions (Cannot remove the specified item because it was not found in the specified Collection.).

Do I have to call a method to refresh the diagram or something like that after I call LoadFromXml?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Delete node programmatically
Reply #3 - Mar 14th, 2008 at 2:46pm
Print Post  
Is there an exception thrown if you run the binding method without going through SaveToXml and LoadFromXml?
  
Back to top
 
IP Logged
 
AnthonyBrien
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 11
Joined: Feb 5th, 2008
Re: Delete node programmatically
Reply #4 - Mar 14th, 2008 at 8:01pm
Print Post  
No, and if I call the same node deletion code from a context menu on my diagram, it also works. It's as if calling it too fast after LoadFromXml causes exceptions. I will try to delay it with a timer to see.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Delete node programmatically
Reply #5 - Mar 15th, 2008 at 9:04am
Print Post  
This could be related to a problem we have recently fixed. Please, try it with this version and let me know if there is any improvement:
https://mindfusion.eu/_beta/fcnet503.zip

Stoyan
  
Back to top
 
IP Logged
 
mjweyland
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 37
Joined: Apr 2nd, 2007
Re: Delete node programmatically
Reply #6 - Mar 20th, 2008 at 1:04am
Print Post  
I am noticing that the NodeDeleting Event is not being raised. I'd like to prevent users from deleting a shapenode from the delete key on the keyboard.

I have set the DeleteKeyAction to None for the Diagram object.

On the KeyDown event I am prompting the user whether or not to delete the selected item(s), then pass a value to a _delete variable on the form. 

if user chooses to cancel the event the key down event is canceled and not deleted, other wise the deleting event would test on the _delete value on whether or not to delete.

However if the diagram.nodes.remove method is called it seems as if it raises the NodeDeleted event.

I'd like to avoid this as I have logic in place to remove the nodes.  I just want to delete the node from the diagram and not enforce business logic around deleting. If you require clariification on this please let me know.

I am using Ver 5.02, is this issue addressed with Version 5.03?

Any help with this is greatly appreciated.

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Delete node programmatically
Reply #7 - Mar 20th, 2008 at 8:57am
Print Post  
Validation events for creation, removal and modification of items are raised only in response to users actions. If you call a method to apply these operations programmatically, the control reckons you know what you are doing and doesn't ask you for confirmation through these events.

Stoyan
  
Back to top
 
IP Logged
 
mjweyland
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 37
Joined: Apr 2nd, 2007
Re: Delete node programmatically
Reply #8 - Mar 20th, 2008 at 1:07pm
Print Post  
Ok???

So when does the Node Deleting Event raise. It's not being raised when a node is deleted programmatically, nor is it being raised when a user deletes a node from a diagram.  Does the code reckon that when it is deleting a node it should raise the deleting method?

Secondly, why does the NodeDeleted event raise when a node is removed from a collection?  This seems to be contradictory to your previous statement.  The NodeDeleted event is raising whether or not it is a result of a user action.

Thanks.

Thanks.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Delete node programmatically
Reply #9 - Mar 20th, 2008 at 1:12pm
Print Post  
I meant that for validation events - where you can set e.Cancel = true to prevent the user from doing something.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Delete node programmatically
Reply #10 - Mar 20th, 2008 at 1:21pm
Print Post  
Del is pressed by the user indeed, and you would get NodeDeleting raised if you didn't set DelKeyAction to None. However if you handle Del yourself, it's up to you to decide whether to call Nodes.Remove or not.
  
Back to top
 
IP Logged
 
mjweyland
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 37
Joined: Apr 2nd, 2007
Re: Delete node programmatically
Reply #11 - Mar 20th, 2008 at 3:30pm
Print Post  
I think we're misscommunicating here...

On the keydownevent I am just prompting users whether or not to delete, then if they choose to delete the node is deleted
Code
Select All
diagram.deleteobject(selectednode) 

(ver 4)

I'm trying to replicate this with version 5 and am having some issues.
There is code in the keydown event to prevent users from deleting particular nodes. However, the NodeDeleted event is still being raised even though the DeleteKeyAction is set to None.  So, nodes that were once prevented from being deleted are now being deleted. 

Secondly.  On a separate method I am programmatically going through and deleting all of the nodes on a diagram.
Code
Select All
diagram.nodes.clearall() 



For every node in the nodes collection the NodeDeleted event is raising.  It seems to me that this shouldn't be the case. This was not the case in version 4. 

Thanks.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Delete node programmatically
Reply #12 - Mar 20th, 2008 at 4:14pm
Print Post  
Quote:
So, nodes that were once prevented from being deleted are now being deleted.


Do you mean that the control deletes nodes without you calling the Remove method, even if DelKeyAction is set to None? What does your KeyDown code look like?
  
Back to top
 
IP Logged
 
mjweyland
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 37
Joined: Apr 2nd, 2007
Re: Delete node programmatically
Reply #13 - Mar 20th, 2008 at 6:03pm
Print Post  
My KeyDown code is the following:
Code
Select All
 If e.KeyCode <> Keys.Delete Then Exit Sub
	  If _parentList.IsLocked Then
		_delete = False
		Exit Sub
	  End If

	  'work around because the delete method only deletes the active box
	  Dim  _i As Integer = MsgBox("Are you sure you want to delete the selected item(s)?", MsgBoxStyle.YesNo, "Delete Component(s)")
	  If _i = MsgBoxResult.Yes Then
		_delete = True
	  ElseIf _i = MsgBoxResult.No Then
		_delete = False
		Exit Sub
	  End If

	  'if component is copied then we wait to delete these last
	  Dim _toDelete As New Generic.List(Of NodeBase)
	  Try
		While (fcMain.Selection.Objects.Count > 0)
		    Dim _o As Object = fcMain.Selection.Objects(0)
		    If _o.GetType.Name = "Box" Then
			  'attempt to delete the item from the collection
			  Dim _b As Box = DirectCast(_o, Box)
			  If Not _parentList.Nodes.Contains(_b) Then
				fcMain.Selection.RemoveObject(fcMain.Selection.Objects(0))
				_delete = False
			  Else
				Dim nb As NodeBase = _parentList.Nodes.Item(_b)
				If nb.IsCopied Then
				    'Add it to the toDelete collection and remove it from the selection collection
				    _toDelete.Add(nb)
				    fcMain.Selection.RemoveObject(TryCast(_b, ChartObject))
				Else
				    'Component was not copied and can be deleted
				    fcMain.DeleteObject(TryCast(_o, ChartObject))
				    _delete = True
				End If
			  End If
		    Else
			  fcMain.DeleteObject(fcMain.Selection.Objects(0))
		    End If
		End While

		'Delete all Copied Components last
		Do Until _toDelete.Count = 0
		    'Remove item from parent container
		    _parentList.Nodes.Remove(_toDelete.Item(0))
		    'Remove image box item from flowchart
		    fcMain.DeleteObject(TryCast(_toDelete.Item(0).Box, ChartObject))
		    'Remove label box item from flowchart
		    fcMain.DeleteObject(TryCast(_toDelete.Item(0).Label, ChartObject))

		    'remove item from toDelete collection
		    _toDelete.RemoveAt(0)
		Loop

	  Catch ex As Exception
		MessageBox.Show(ex.Message, "Error deleting object", MessageBoxButtons.OK, MessageBoxIcon.Error)
		My.Application.Log.WriteException(ex)
	  End Try
 



My problem is when the node is removed the node.deleted event raises and any code around this event is also executed.

This didn't used to be the case.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Delete node programmatically
Reply #14 - Mar 21st, 2008 at 10:59am
Print Post  
Box/TableDeleted events were raised too when calling the DeleteObject method. Now NodeDeleted is raised in the same way when calling Nodes.Remove. Since you are deleting the items yourself, you do you need NodeDeleted event handler at all?
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send TopicPrint