About linkage seletion in eventhandler RhinoDoc.SelectObjects += OnSelectObjects

My code refers to SampleCsEventWatcher.
I want to select another object B when object A is selected,so I call RhinoDoc.ActiveDoc.Select(B.Id) in eventhandler OnSelectObjects, both objects are highlighted. Then I press delete on keyboard, expect to delete both objects, but nothing happened, OnDeleteRhinoObject event can’t be invoked.

If select A and no linkage selection for B,then press delete key, object A will be deleted.

What is the linkage failure reason, can you give me a method to acomplish the funtion? Thank you

I think you should not modify the state do the document (this includes selections) in the event handler. What you maybe can do in the event handler is subscribe to the RhinoApp.Idle event, and when that occurs, i.e. when Rhino is ready for it, select the object B.

see below for answer

I tried Menno’s method, and the result was the same as that of Select(B.Id) in eventhandler OnSelectObjects, problem unsolved.
Maybe I should use another method instead of RhinoDoc.ActiveDoc.Select(B.Id) , I don’t know what it is, anyone can help me?

I have tried the following approach and that works as expected. Contrary to what I wrote earlier, RhinoApp.OnIdle is not needed:

private Guid idA = Guid.Empty;
private Guid idB = Guid.Empty;

public Result RunCommand(RhinoDoc doc, RunMode mode)
  if (idA == Guid.Empty)
    RhinoApp.WriteLine("Activating selection event handler.");
    Result res = RhinoGet.GetOneObject("Object A", true, ObjectType.AnyObject, out var aRef);
    if (res != Result.Success) return res;

    res = RhinoGet.GetOneObject("Object B", true, ObjectType.AnyObject, out var bRef);
    if (res != Result.Success) return res;
    idA = aRef.ObjectId;
    idB = bRef.ObjectId;
    RhinoDoc.SelectObjects += OnObjectSelected;
    RhinoDoc.DeleteRhinoObject += OnObjectDeleted;
    RhinoApp.WriteLine("Deactivating selection event handler.");
    idA = Guid.Empty;
    idB = Guid.Empty;
    RhinoDoc.SelectObjects -= OnObjectSelected;
    RhinoDoc.DeleteRhinoObject -= OnObjectDeleted;

  return Result.Success;

private void OnObjectDeleted(object sender, RhinoObjectEventArgs e)
  if (e.ObjectId == idA) 
    RhinoApp.WriteLine("object A deleted");
  if (e.ObjectId == idB)
    RhinoApp.WriteLine("object B deleted");

private void OnObjectSelected(object sender, RhinoObjectSelectionEventArgs e)
  if (e.Selected)
    if (e.RhinoObjects.Any(o => o.Id == idA))
      RhinoApp.WriteLine("object A selected. Now selecting object B too");

When you run the command, it identifies objects A and B. After the command, when you select object A, object B is also selected. When you then press the Delete key, both objects are deleted and the object deletion event handler is invoked for both objects.

Dear Menno and Dale,

Thank you Menno for your example.
My program doesn’t work because I have a popup window form. After I execute the OnObjectSelected(…), my program let the focus turns to that window, so then delete key doesn’t work at all.
Now I have the new problem, how to bring the focus back to rhino view by program, I had tried RhinoApp.SetFocusToMainWindow(), it doesn’t work, maybe it only works at MAC system.
Imagine the case of grasshopper window. We put a Point battery on its canvas, Right Click and select ‘Set a point’, then we can click in rhino view, just one click, to define the point, there is no extra click which aimed to get focus on the rhino view, how does grasshopper do it?

Hi @chen_manhong,

It’s always best to post simple code, that we can all run, that repeats the problem you are having.

– Dale

Hi, Dale and Menno
I posted a new topic [How to add an object to display it differently in different viewports? and another question ], with my code uploaded there.
Thank you!