1

I am trying to make an application that is drawing Dendrograms like this one.

So I added a PictureBox to the winform, and for start, I wanted to wrote all labels like in the picture with this code:

foreach (var line1 in lines)
{
    i++;
    gpx.DrawString(line1, myFont, Brushes.Green, new PointF(2, 10 * i));
}

But the problem is that I have a lot of labels so it writes only a few of them on 800x600 px. I wanted to add scrolling bars, but it doesn't work at all. It works only when I am setting an Image to PictureBox.

Is there any other way, with or without PictureBox?

Smi
  • 13,850
  • 9
  • 56
  • 64
  • Did you try adding the PictureBox inside a panel ?if not see here http://stackoverflow.com/questions/4710145/how-can-i-get-scrollbars-on-picturebox – Mitz Jan 07 '15 at 10:43
  • I did, I saw every related topic. Like I said - it works fine with an image, but not with written words. – user3548153 Jan 07 '15 at 10:59
  • That is still the way. All you need to take care of is to make the PictureBox large enough to hold the stuff you draw. Do you write __on top__ of the PictureBox (and if so hopefully in the `Paint` event!?!) or __into__ its Image? To find the right `Size` you may need to use `MeasureString` – TaW Jan 07 '15 at 11:05
  • You may want to have a look at [this post](http://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797#27341797) for details on the differences.. - Will the data (text) be dynamic? – TaW Jan 07 '15 at 11:12
  • Yes, on top, now I also moved code into Paint event (thanks a lot for a tip!). Scrollbars have appeared, but they scroll just a little bit, like 5-6 lines so still not all lines are visible. – user3548153 Jan 07 '15 at 11:17
  • That means that the picturebox is not large enough yet, I'd say.. – TaW Jan 07 '15 at 12:34

1 Answers1

2

PictureBox is a very simple control, it is only good to display a picture. The one capability it doesn't have that you need is the ability to scroll the content. So don't use it.

Creating your own control is very simple in Winforms. A basic starting point is to begin with Panel, a control that supports scrolling, and derive your own class for it so you customize it to be suitable for the task. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto a form. Note how you can set the Lines property, either with the designer or your code. Use the Paint event to draw the dendrogram. Or extend the OnPaint() method in the class, you can make it as fancy as you want.

using System;
using System.Drawing;
using System.Windows.Forms;

class DendrogramViewer : Panel {
    public DendrogramViewer() {
        this.DoubleBuffered = this.ResizeRedraw = true;
        this.BackColor = Color.FromKnownColor(KnownColor.Window);
    }

    public override System.Drawing.Font Font {
        get { return base.Font; }
        set { base.Font = value; setSize(); }
    }

    private int lines;
    public int Lines {
        get { return lines; }
        set { lines = value; setSize(); }
    }

    private void setSize() {
        var minheight = this.Font.Height * lines;
        this.AutoScrollMinSize = new Size(0, minheight);
    }

    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
        base.OnPaint(e);
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Hm. Well, a PictureBox also does a good job of zooming or stretching or tiling an Image and caching and saving the Image. Dropping one on an AutoScroll Panel is not hard either, so the real advantage of subcalssing a Panel will only come in when you add some real brains to it. Not sure if that applies here.. But I'll consider it next time around in my projects.. – TaW Jan 07 '15 at 12:41
  • Thank You both very much. Control works perfectly and its really easy to extend. Best regards!! – user3548153 Jan 07 '15 at 13:18