-1

I created a new UserControl in the designer I added a richTextBox. Then I did in the UserControl constructor:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScrollLabelTest
{
    public partial class ScrollText : UserControl
    {
        Font drawFonts1 = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);
        Point pt = new Point(50, 50);

        public ScrollText()
        {
            InitializeComponent();

            System.Drawing.Font f = new System.Drawing.Font("hi",5);
            Graphics e = richTextBox1.CreateGraphics();
            e.DrawString("hello",drawFonts1,new SolidBrush(Color.Red),pt);
            this.Invalidate();
        }
    }
}

Then I dragged the new UserControl into the form1 designer but it's empty. I don't see the word "hello".

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52

1 Answers1

0

One way is to extend the RichTextBox control and implement your custom drawing in OnPaint method. But normally RichTextBox control does NOT call the OnPaint method so you have to call the method manually by hooking in WndProc method.

Example:

class ExtendedRTB : System.Windows.Forms.RichTextBox
{
    // this piece of code was taken from pgfearo's answer
    // ------------------------------------------
    // https://stackoverflow.com/questions/5041348/richtextbox-and-userpaint
    private const int WM_PAINT = 15;
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_PAINT)
        {
            // raise the paint event
            using (Graphics graphic = base.CreateGraphics())
                OnPaint(new PaintEventArgs(graphic,
                 base.ClientRectangle));
        }

    }
    // --------------------------------------------------------

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.DrawString("hello", this.Font, Brushes.Black, 0, 0);
    }
}
Community
  • 1
  • 1
sallushan
  • 1,134
  • 8
  • 16
  • sallushan i tried your solution strange: when i drag the control in form1 designer around on form1 designer i see the text hello but once i leave the control on place and not drag it i dont see the hello text just white back color on the control. And i used a breakpoitn and it's getting to the paint event but only when i drag the usercontrol i see the hello. – user3756594 Jun 25 '14 at 18:16
  • sallushan i found the problem. Your solution is drawing the string on the UserControl not on the richTextBox. My richTextBox is on the UserControl designer. Maybe im missing here something. – user3756594 Jun 25 '14 at 18:19
  • You can see in my updated question i edited it with a screenshot. The "hello" is drawed on the UserControl but not on the richTextBox1 in the UserControl designer ! – user3756594 Jun 25 '14 at 18:21
  • Are you trying to see "Hello" on design mode? Even if you see on design mode what benefit it will give you? The solution is for run-time. – sallushan Jun 26 '14 at 12:26