Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:01:28+00:00 2026-06-13T18:01:28+00:00

I have a Windows Forms application in C# with drawing panel and a button

  • 0

I have a Windows Forms application in C# with drawing panel and a button – for drawing a line.

When you click the button, you can draw a line for 2 random points.

Pen p = new Pen(Color.Black, 5);
//point for start 
Point ps = new Point();
//point for end 
Point pe = new Point();

private void drawPanel_MouseDown(object sender, MouseEventArgs e)
{
  ps.X = e.X;
  ps.Y = e.Y;
  pe = ps;
}

private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
  // when button is clicked for drawing draw = true;
  if (draw)
  {
    if (e.Button == MouseButtons.Left)
    { 
      pe = new Point(e.X, e.Y);
    }
  }
}

private void drawPanel_MouseUp(object sender, MouseEventArgs e)
{
  onMouseUpFlag = true;
}

private void drawPanel_Paint(object sender, PaintEventArgs e)
{
  Graphics g = drawPanel.CreateGraphics();
  if (onMouseUpFlag)
  {
    g.DrawLine(p, ps, pe); 
    g.Dispose(); 
  }
} 

The program has some flaws :

  • When you draw a line it shows it, only if the main window is moved
    somewhere(usually when I hide it)
  • It can draw only 1 line.

Any suggestions how to fix these bugs ?

EDIT

I’ve read you answers and made some changes :

Pen p = new Pen(Color.Black, 5); 
Point ps = new Point();
Point pe = new Point();

List<Point> linesStart= new List<Point>();
List<Point> linesEnd= new List<Point>();

private void drawPanel_MouseDown(object sender, MouseEventArgs e)
{
  ps.X = e.X;
  ps.Y = e.Y;

  linesStart.Add(ps);

  pe = ps;
}

private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Left)
  {
    pe = new Point(e.X, e.Y);

    //adding end point .. actually adds a lot of points 
    linesEnd.Add(pe);
  }
}

bool onMouseUpFlag = false;

private void drawPanel_MouseUp(object sender, MouseEventArgs e)
{
  onMouseUpFlag = true;
  drawPanel.Invalidate();
}

private void drawPanel_Paint(object sender, PaintEventArgs e)
{ 
  if (onMouseUpFlag)
  {
    for (int i = 0; i < linesStart.Count; i++)
    {
      e.Graphics.DrawLine(p, linesStart[i], linesEnd[i]);
    } 
  }
} 

Now I’m trying to fix the DrawLine for multiple lines. Paint event can do multiple line but only the starting point is fine. Somehow the end point is not very correct. Where I can set exactly the last point of the MouseMove event ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:01:29+00:00Added an answer on June 13, 2026 at 6:01 pm

    You have to call the Invalidate method on the panel:

    private void drawPanel_MouseUp(object sender, MouseEventArgs e)
    {
      onMouseUpFlag = true;
      drawPanel.Invalidate();
    }
    

    Also, use the Graphic object from the PaintEvent:

    private void drawPanel_Paint(object sender, PaintEventArgs e)
    {
      if (onMouseUpFlag)
      {
        e.Graphics.DrawLine(p, ps, pe); 
      }
    } 
    

    For multiple lines, you would have to hold the points in a collection object.

    Per your updated code, here is a working example of what I think you are trying to do:

    private class Line {
      public Point Starting { get; set; }
      public Point Ending { get; set; }
    
      public Line(Point starting, Point ending) {
        this.Starting = starting;
        this.Ending = ending;
      }
    }
    List<Line> lines = new List<Line>();
    
    private Point downPoint = Point.Empty;
    private Point movePoint = Point.Empty;
    private bool movingLine = false;
    
    public Form1() {
      InitializeComponent();
    
      panel1.Paint += panel1_Paint;
      panel1.MouseDown += panel1_MouseDown;
      panel1.MouseMove += panel1_MouseMove;
      panel1.MouseUp += panel1_MouseUp;
    }
    
    void panel1_MouseDown(object sender, MouseEventArgs e) {
      if (e.Button == MouseButtons.Left) {
        downPoint = e.Location;
      }
    }
    
    void panel1_MouseMove(object sender, MouseEventArgs e) {
      if (e.Button == MouseButtons.Left) {
        movingLine = true;
        movePoint = e.Location;
        panel1.Invalidate();
      }
    }
    
    void panel1_MouseUp(object sender, MouseEventArgs e) {
      if (e.Button == MouseButtons.Left) {
        movingLine = false;
        lines.Add(new Line(downPoint, e.Location));
        panel1.Invalidate();
      }
    }
    
    void panel1_Paint(object sender, PaintEventArgs e) {
      e.Graphics.Clear(Color.White);
      foreach (Line l in lines) {
        e.Graphics.DrawLine(Pens.Black, l.Starting, l.Ending);
      }
    
      if (movingLine) {
        e.Graphics.DrawLine(Pens.Black, downPoint, movePoint);
      }
    
    }
    

    Use an inherited panel to turn on the DoubleBuffer property to avoid flickering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows forms application in .NET 3.5. There's one form with 20
I have a windows forms application that reads and updates an XML file with
I have a Windows Forms application that on it I have a RichTextBox, like
I have a Windows Forms application in C# .NET. It contains a user-drawn control
I have a Windows Forms application that uses some Application.Idle handlers to change the
I have a windows forms application, where I have declared some static variables. On
The Scenario I have a windows forms application containing a MAINFORM with a listbox
Scenario I have a Windows Forms Application. Inside the main form there is a
Scenario I have a windows forms application. I want to use two different WCF
If you have a windows service and a windows forms application that uses the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.