Question

I am designing a windows forms control for Syntax Editing. I know there are already a lot out there, like Scintilla, FastColoredTextBox, Actipro Syntax Editor, Avalon Edit etc. I have reasons for designing my own, so that's not the issue.

So far I have been designing the look and feel of the control. It will need to be able to control vertically, and horizontally.

The options I have come across are:

  1. My control extends ScrollableControl or ContainerControl
  2. My control instantiates a HScrollBar and VScrollBar control and places them accordingly
  3. My control uses ScrollBarRenderer to custom draw the scrollbars

I am not sure which of these options would be best practice for my control.

I tried using ScrollableControl and ContainerControl, but this had some very undesired results, probably because the controls DisplayRectangle was scrolling...I don't want this. I want to scroll a custom drawn rectangle which holds the text.

I tried instantiating HScrollBar and VScrollBar, but this seemed very buggy, and didn't work well with focus and I couldn't work out how to properly capture VScroll and HScroll events.

I tried using ScrollBarRenderer, but this seems like a hell of a lot of effort just to implement a scrollbar, and with this approach, I would still have to capture and handle the events appropriately.

So which of these approaches should I be using, or indeed an approach I may have overlooked?

Was it helpful?

Solution

A ScrollBarRenderer is only used to draw a scrollbar, it doesn't actually perform any scroll calculations nor actions.

Personally, I find the HScrollBar and VScrollBar rather clumsy controls to work.

Inheriting from the ScrollableControl (or Panel to get a built-in windows control border) is probably the easiest. You use the AutoScrollMinSize property to set the size of the interior surface, and then perform a TranslateTransform on the graphics object using the AutoScrollPosition property to handle the "where" to draw part of the control:

public class ScrollControl : ScrollableControl {

  public ScrollControl() {
    this.DoubleBuffered = true;
    this.ResizeRedraw = true;
    this.AutoScrollMinSize = new Size(0, 600);
  }

  protected override void OnPaint(PaintEventArgs e) {
    base.OnPaint(e);

    e.Graphics.Clear(Color.White);
    e.Graphics.TranslateTransform(this.AutoScrollPosition.X, 
                                  this.AutoScrollPosition.Y);
    e.Graphics.FillRectangle(Brushes.Red, new Rectangle(16, 32, 64, 32));
  }
}

Be careful though, a syntax text editor is a different beast that a drawing control. I would advise using a RichTextBox control for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top