For some reason my line wont draw on my windows form. If I place the code in a button event routine it works fine but it won't work when the window loads. I'm really confused! thanks:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace testForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
Pen myPen = new Pen(Color.Red);
Graphics g = this.CreateGraphics();
g.DrawLine(myPen, 20, 30, 10, 50);
//this.Refresh(); //doesn't work if I add this.
}
}
}
Loading

Mahesh ChandRank #1 · 393.3K reputationPosted Mar 20, 2009, 7:38 PM EDT
You need to put this code on On_Paint event handler of the form.
jabir eriyaden30 reputationPosted Mar 20, 2009, 10:01 PM EDT
George Loveless13 reputationPosted Mar 21, 2009, 7:36 AM EDT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace t
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen myPen = new Pen(Color.Red);
Graphics g = this.CreateGraphics();
g.DrawLine(myPen, 20, 30, 10, 50);
}
}
}