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

  • SEARCH
  • Home
  • 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 8302583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:19:41+00:00 2026-06-08T17:19:41+00:00

I am trying to call panel1 paint method to repaint the panel with a

  • 0

I am trying to call panel1 paint method to repaint the panel with a orange line (it is initiated with a blue line).

I have tried invalidate(), update() and refresh(), but nothing seems to call the paint event of panel1…

The paint event handler have been added to the panel1:

this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);

Can someone please assist?

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form1 testForm = new Form1();
        Application.Run(testForm);

        testForm.drawNewLine();
    }
}

public partial class Form1 : Form
{
    bool blueLine = true;
    bool orangeLine = false;

    public Form1()
    {
        InitializeComponent();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        if (blueLine == true)
        {
            Pen bluePen = new Pen(Color.Blue, 3);
            g.DrawLine(bluePen, 30, 50, 30, 250);
        }
        else if (orangeLine == true)
        {
            Pen orangePen = new Pen(Color.Orange, 3);
            g.DrawLine(orangePen, 30, 50, 30, 250);
        }

        g.Dispose();
    }

    public void drawNewLine()
    {
        blueLine = false;
        orangeLine = true;
        //panel1.Invalidate();
        //panel1.Update();
        panel1.Refresh();
    }
}
  • 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-08T17:19:42+00:00Added an answer on June 8, 2026 at 5:19 pm

    Application.Run(testForm); blocks until form is closed, so when drawNewLine() is called – form does not exist anymore (create a button which calls it on click and check yourself, the code is working). Invalidate() should work just fine.

    Also, you should not dispose Graphics object which is passed to your code in paint event. You were not responsible for creating it, so let the code which created it to destroy it.

    Also, dispose Pen objects since you are creating them.

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

Sidebar

Related Questions

I am trying to call c# function from javascript the code i have tried
I have a ASP.NET 3.5 App and trying to update a Update Panel which
I have an ASP.Net Update Panel which contains controls that I am trying to
I have been trying to work out how to call a method in a
Im trying to call a method that will add or subtract 1 from a
I'm trying to call a method from another class with a simple button in
I am trying to create a Swing panel whose elements have a different font
I'm trying to display a panel to the user when an asynchronous call is
I have a matplotlib figure inside of a wxmpl panel, and am trying to
When trying to call on a method from another class my method as a

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.