Reputation: 857
I want to draw a margin line at 80 characters in a WinForms TextBox. Here is what I've tried, in my TextBox subclass:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
const int WM_PAINT = 0x00F;
if (m.Msg == WM_PAINT) {
DrawMargin();
}
}
void DrawMargin()
{
using (Pen pen = new Pen(Color.Gray, 1)) {
using (Graphics graphics = CreateGraphics()) {
float charWidth = graphics.MeasureString("M", Font).Width;
graphics.DrawLine(pen, charWidth * 80, 0, charWidth * 80, Height);
}
}
}
There are at least three problems with this:
I notice that TED Notepad (which uses a Win32 EDIT control) is able to draw a margin without any problems, so it seems that it's possible to do what I want. Could anyone advise me how?
Upvotes: 2
Views: 1438
Reputation: 857
As far as I can tell, the best way of doing this is simply to place a WinForms.Panel over the TextBox:
class FooTextBox : TextBox
{
public FooTextBox()
{
margin = new Panel();
margin.Enabled = false;
margin.BackColor = Color.LightGray;
margin.Top = 0;
margin.Height = ClientSize.Height;
margin.Left = <whatever>;
margin.Width = 1;
Controls.Add(margin);
}
Panel margin;
}
Since the panel is not Enabled, it doesn't take mouse input.
Upvotes: 1
Reputation: 23831
I am not sure about this method. But one thing you could look at trying is inserting an image into the text box. The image would of course be your margin, and the text would automatically start after the picture. To include a picture inside a text box see How can I insert an image into a RichTextBox?
Edit: I have also found this article http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/ which seems to facilitate painting in the background of a text box. The methods described here seems to take you a long way towards what you require.
Hope this helps.
Upvotes: 1