-1

My problem is how to draw a simple line in a child window in C#, i.e. :

  • I have a parent window, with a button. Clicking the button, a child window shows,and a line is drawn on it.

Well, how do I do that ? This is my code for child window:

public partial class Form2 : Form
{
    Pen pen;
    public Form2()
    {
        InitializeComponent();
        pen = new Pen(Color.Black);
    }


    private void Form2_Paint(object sender, PaintEventArgs e)
    {
        Graphics g;
        g = this.CreateGraphics();
        e.Graphics.DrawLine(pen, 10, 10, 100, 100);
    }

}

Thanks.

E B
  • 59
  • 1
  • 8
  • What exactly does not work for you ? Child window does not show up, the line does not show up, etc. – Adrian Fâciu Jan 07 '14 at 07:48
  • @Adrian Faciu The child window shows up, but the line is not drawn. – E B Jan 07 '14 at 08:00
  • Your code from above should work as it is. Please post the part where you handle the button click and show Form2. – Adrian Fâciu Jan 07 '14 at 08:10
  • @Adrian Falciu This is the code: – E B Jan 07 '14 at 08:13
  • namespace Test { public partial class Form1 : Form { Form f2; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { f2 = new Form(); f2.ShowDialog(); } } } – E B Jan 07 '14 at 08:14
  • You can edit the question and add the additional info there. Also look at CzBiX question from the comments. I still don't see any error with your code. – Adrian Fâciu Jan 07 '14 at 08:30

1 Answers1

4

The following code is better:

private void Form2_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    using (Pen p = new Pen(Color.Black))
    {
        g.DrawLine(pen, 10, 10, 100, 100);
    }
}

use the graphics instance from paint event, and using syntax to auto dispose pen object

Remeber call Show method in parent window like this:

private void button_Click(object sender, EventArgs e)
{
    Form form2 = new Form2();
    form2.ShowDialog();
}
CzBiX
  • 72
  • 6
  • 1
    @EB Does you write **Form2_Paint** method by hand? you may not register event in Designer file by GUI Designer. – CzBiX Jan 07 '14 at 08:08
  • Did not write by hand. I registered the event in the usual way. – E B Jan 07 '14 at 08:33
  • @EB how about try draw line in parent window, so make sure `DrawLine` is worked as you want. – CzBiX Jan 07 '14 at 08:52
  • I guess I've found a solution: I create "new Pen" and Graphics in parent window (Form1). In child window class (Form2), I declare a Pen and Graphics variables, I add a constructor and I pass the variables Pen and Graphics from the parent window. And it works. Sorry, I can't edit the whole code because my reputation (:-)) is to low. Thanks to everybody. – E B Jan 07 '14 at 09:10
  • And I forgot to write that in the child window class I added Form_Load like this : private void Form2_Load(object sender, EventArgs e) { this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form2_Paint); } – E B Jan 07 '14 at 09:22
  • The key point is you aren't registered paint event before, so the `Form2_Paint` is never called. look here, [How to add events](http://stackoverflow.com/questions/1135299/microsoft-visual-studio-and-c-how-to-visually-add-events-to-controls) – CzBiX Jan 07 '14 at 09:34
  • Thank you. I used the tip and it saved me a lot of time. – E B Jan 07 '14 at 10:33
  • @EB please mark my answer as accepted if you think it's worked for you. see [accepted answer](http://stackoverflow.com/help/accepted-answer) – CzBiX Jan 08 '14 at 12:58