component for c# winform project that is a moving horizontal graph

Darryl Hoar 141 Reputation points
2024-01-25T16:19:26.69+00:00

I have a winform project. I would like to add a component that is a moving graphic. Ie a graph with a line that moves across from left to right showing some real time values. Anybody have a recommendation for this? Think heart monitor like moving graph.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,649 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-01-26T01:20:26.25+00:00

    Hi @Darryl Hoar , Welcome to Microsoft Q&A,

    You can operate the interval moving image through the timer control. All you need to do is modify the corresponding speed.

    Real-time values can also be modified in the tick event to achieve real-time effects by modifying the text of controls such as labels.

    In the sample code below, I use a panel and a timer control to demonstrate.

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace xxx
         public partial class Form1 : Form
         {
             private int linePosition = 0;
             private const int lineWidth = 2;
             private const int lineHeight = 20;
             private const int moveSpeed = 5; //Adjust movement speed
             public Form1()
             {
                 InitializeComponent();
                 timer1.Interval = 50; //Adjust the timer interval
                 timer1.Start();
             }
             private void panel1_Paint(object sender, PaintEventArgs e)
             {
                 Graphics g = e.Graphics;
                 Pen pen = new Pen(Color.Black, lineWidth);
    
                 // Draw a moving line
                 g.DrawLine(pen, linePosition, 0, linePosition, lineHeight);
             }
    
             private void timer1_Tick(object sender, EventArgs e)
             {
                 //Update line position
                 linePosition += moveSpeed;
    
                 // Determine whether the right boundary of the Panel has been reached. If so, restart the movement.
                 if (linePosition > panel1.Width)
                 {
                     linePosition = 0;
                 }
    
                 // Refresh the Panel and trigger redrawing
                 panel1.Invalidate();
             }
         }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful