Hide and Show Layers of the view with GoControls

I've created GoButtonCtrl inherited from GoControl with a Button control encapsulated in it:

public class GoButtonCtrl: GoControl

{

Button button;

public GoButtonCtrl()

{

this.Size = new SizeF(16, 16);

}

public override Control CreateControl(GoView view)

{

if (button == null)

{

button = new Button();

button.Text = "";

}

return button;

}

}


Then I've created a ButtonNode inherited from GoBoxNode with my button control body in it :

public class ButtonNode : GoBoxNode

{

GoButtonCtrl btn;

protected override GoObject CreateBody()

{

if (btn == null)

btn = new GoButtonCtrl ();

return btn;

}

}

Then I throw my ButtonNode on the GoDoc, connected to my GoView.
That's good. It's created, I can see it, and it can be zoomed, okay.
But. When I use my view, inherited from GoView, that uses overriden DocScale, where I hide and show layers again, it has a strange behavior.
this is my View:

public class View: GoView

{

protected override void OnPropertyChanged(PropertyChangedEventArgs evt)

{

base.OnPropertyChanged(evt);

switch (evt.PropertyName)

{

case "DocScale":

{

HideShowLayers();

break;

}

}

}

protected virtual void HideShowLayers()

{

GoLayer defaultLayer = this.Layers.Default;

foreach (GoLayer layer in this.Layers.CopyArray())

{

if (!layer.IsInView)

{

layer.AllowView = false; // making Invisible

// this.Layers.Remove(layer);

// another option - removing layer from View's Layers - not Document's!!

}

}

foreach (GoLayer layer in this.Document.Layers)

{

layer.AllowView = true;

//this.Layers.InsertDocumentLayerAfter(null, layer); //another option - adding layer

}

}

defaultLayer.AllowView = true;

//this.Layers.InsertDocumentLayerAfter(null, defaultLayer);

}

Why do I need this? I need to hide some layers if they are out of scale factor. And show them in the other case.
I've got two cases:
1. When I make all layers invisible here, but not making them visible, all goObjects are hidden, but the button as a control
still presents
2. When I delete layers, adding them back again - this makes the button invisible. All GoObjects are visible then except a button.
I've tested different controls - all the same.
Is it a bug or am I doing something wrong?
The right behavior appears only when I hide layers and then make Button Control Invisible, and back again - make layers visible and make button Visible.

for something as simple as a button, it’s probably easier to create your own out of
a GoRoundedRectangle and GoText than to deal with the complexities of gluing another
Windows control on top of the GoView Control.

Is there some reason that isn’t an option?

Actually, that is why the GoButton class exists.

GoButton is a little behind the times visually… this will give you a better looking button:

[Serializable]
public class RenderedButton : GoButton
{
  protected override void PaintButton(Graphics g, GoView view) {
  float sc = view.DocScale;
  RectangleF b = this.Bounds;
  RectangleF boundsF = new RectangleF(b.X * sc, b.Y * sc, b.Width * sc, b.Height * sc);
  Rectangle bounds = Rectangle.Round(boundsF);
  System.Windows.Forms.VisualStyles.PushButtonState state = myState;
  if (this.ActionActivated) state = System.Windows.Forms.VisualStyles.PushButtonState.Pressed;
  //string text = this.Label.Text;
  //Font font = SystemFonts.IconTitleFont;
  ButtonRenderer.DrawButton(g, bounds, "", null, false, state);
  // for some reason, text comes out above button with DrawButton, so use Label to paint text.
  this.Label.Paint(g, view);
  }

  public override bool OnEnterLeave(GoObject from, GoObject to, GoView view) {
    myState = System.Windows.Forms.VisualStyles.PushButtonState.Normal;
    if (to == this) myState = System.Windows.Forms.VisualStyles.PushButtonState.Hot;
    this.InvalidateViews();
    return base.OnEnterLeave(from, to, view);
  }
  System.Windows.Forms.VisualStyles.PushButtonState myState = System.Windows.Forms.VisualStyles.PushButtonState.Normal;
}

First of all thanks for your quick answer!

The solution with ButtonRenderer looks good.

The mechanism of putting windows Controls over the GoView is clear.
I see that it’s not so good and stable when doing extra things like zooming and disabling layers
and I understand that it’s not so easy to control their behavior.

And, as GoControl class exists and supported, there’s a wish to use it.

For more complex controls than buttons (if there was a way to render any control to vector graphics there wouldn’t be a need to use a GoControl). And to get expected behavior.

On the other hand I understand that native Go Objects are more stable to use, and in most cases I will use them.