Drawing Rectangle in Measurement Studio Chart Using the Cursor

Updated Apr 28, 2023

Environment

Software

  • Measurement Studio

In this article, you will find a tutorial and an example of the C# code for drawing a rectangle using the mouse in Measurement Studio. 
You can modify the example the way it suits your needs more.

Note: The example which was modifyed is one delivered with Measurement Studio.

 

Please follow the steps below: 
  1. Open a standard example sheep by measurement studio 
Name  bin  obj  App  Assemblylnfo.cs  t.j Charting  Charting.sln  licenses  MainForm.cs  MainForm.resx  ReadMe  Date modified  9/17/2021 11:21 AM  9/17/2021 11:21 AM  9/17/2021 11:21 AM  5/9/2019 9:16 PM  5/9/2019 9:16 PM  5/9/2019 9:19 PM  5/9/2019 9:20 PM  5/9/2019 9:16 PM  5/9/2019 9:16 PM  5/9/2019 9:16 PM  5/9/2019 9:16 PM  Size  File folder  File folder  File folder  Icon  Visual Source File  Visual Project file  Visual Studio Solution  LICX File  Visual Source File  Microsoft .NET Manage...  Text Document  378 KB  I KB  I KB  Il KB  572 KB
                                                                                        
  1. Add the code from the attached file under the MainForm       
MainForm.c'  @Charting  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  41  42  43  100%  Output  X MainForm.cs [Designr  N•1ainForm.resx  Nationallnstruments.Examples.Charting.MainForm  nationaaanstruments . Oa .•unaousrorms;  Enamespace National Instruments .Examp1es . Charting  /// Sumrary description for MainForm.  2 references  public class MainForm : System.windows. Forms. Form  private  private  private  private  private  private  private  private  private  private  private  private  DataManager dataManager;  System. Windows . Forms. GroupBox settingsGroupBox;  System. Windows . Forms. Timer timer;  System. Windows . Forms. Rad ioBut ton optionStripChartRadioButton;  System. Windows . Forms. Rad ioButton optionscopeChartRadioButton;  System. Windows . Forms. Checkbox optionVertica1CheckBox;  System. . Forms. ToolTip tool Tip;  Nationallnstruments . UI .WindowsForrns.WaveformGraph samplewavefor«-aph;  Nationallnstruments . UI .WaveformP10t waveformP10t;  Nationallnstruments.LJI .XAxis xAxis;  Nationallnstruments.LJI .YAxis yAxis;  System. Component"odel. IContainer components;  Rectangle EFCt;  Point LocationXY;  Point Location XIYI;  bool Is"ouseDown false;  4 references  private enum Charting'•bdes  Strip,  Scope  1 reference  public MainForm()  // Requi red for Wi ndows Form Designer support  @ No issues found  Show output from:
 
                                      
  1. Navigate all the way down on the code and leave the cursor at the line between 333-335 (add more lines as shown in the picture) 
MainForm.cf  @Charting  3ø4  305  306  307  3e8  3Ø9  31ø  311  312  313  314  315  316  317  318  319  320  321  322  323  324  325  327  328  329  330  331  332  333  334  336  337  338  339  100%  Output  X  MainForm.cs [Designr  2 references  MainForm.resx  Nationallnstruments.Examples.Charting.MainForm  a,  private void SetAxisModes (Chartingmodes mode)  Axis chartingAxis, scaleAxis;  if (!dataManager . IsVertica1)  chartingAxis = xAxis;  scaleAxis = yAxis;  else  chartingAxis yAxis;  scaleAxis xAxis;  scaleAxis.Mode = Axist•bde. AutoSca1eLoose;  if (mode  Cha rtingmodes. Scope)  chartingAxis -Mode = Axismode . Scopechart;  else  chartingAxis -Mode = AxisMode . StripChart;  sampleWa veformGraph . Cle arDat a ( ) ;  dataManager . Reset( ) ;  @ No issues found  Show output from:
  1. Go to the ManiForm.cs[Design]*, we now need to add events so please choose the Event button from the Properties section in the right lower corner: 
@stnp Chan  C) Scope Chan  toolTip  Output  Show output from:  Error List Output  tv1ainForm.c'  10  Chart Seings  @t'mer  MainForm.cs [Designr  x  MainForm.resx  10  C] Vertcal  Solution Explorer  Search Solution Explorer (Ctrl* é)  Solution •Charting' (1 of 1 project)  References  App.ico  c* Assemblylnfo.cs  MainForm.cs  MainForm.resx  Solution Explorer Git Changes  'rope rues  sampleWaveformGraph Nationallnstruments.Ul.WindowsF( •  Events  Drag Drop  DragDrop  DragEnter  DragLeave  Dragover  GiveFeedback  QueryContinueDrag  Focus  Enter  Leave  Validated  Validating  PlotDataChanged  Occurs when a new set of data is plotted on a plot that  is contained in the graph plot collection.
  1. We are going to manually add events MouseDown, MouseMove, MouseUp, Paint. After double-clicking on the event it will show up on the code lines. 
MainForm.cf  Charting  334  335  336  337  338  339  340  341  342  343  344  345  346  347  343  349  350  352  354  LocationXIY1 = e. Location;  355  356  357  358  359  36ø  X  MainForm.cs [Designr  MainForm.resx*  else  chartingAxis .Mode  Nationallnstrurnents.Examples.Charting.MainForm  = AxisMode . StripChart;  sampleWa veformGraph . Cle arData ( ) ;  dat amanager. et();  1 reference  private void ect sender, MouseEventArgs e)  IsmouseDown  Location XY  1 reference  = true; //lf this event has occured so this variable is true  = e.Location; //Ge the starting location of point X and Y  private void sender,  MouseEventArgs e)  if (Ist•buseDown  true)
 
MainForm.cf  @Charting  358  359  36B  362  3 64  365  366  367  368  369  370  372  373  374  375  376  377  MainForm.cs [Designr  1 reference  MainForm.resf  Nationallnstruments.Exarrvples.Charting.MainForm  G'. GetRectO  private void (Obj ect sender,  if (Ist•buseDown true)  MouseEventArgs e)  Loc ation XIYI  IsMouseDown  reference  = e. Location;  = false;  private void ect sender, PaintEventArgs e)  if (rect null)//Check if rectangle is not a null  e. Graphics .DrawRectang1e(Pen s. Green, GetRect()) ;
  1. After, just add the GetRect() for getting the rectangle on the plot: 
MainForm.cf  @Charting  358  359  36B  361  362  3 64  365  366  367  368  369  370  371  372  374  375  376  377  378  379  331  332  383  384  385  336  387  388  389  39ø  391  392  393  394  100%  MainForm.cs [Designr  1 reference  MainForm.resf  Nationallnstruments.Examples.Charting.MainForm  G'. GetRectO  a,  private void (Obj ect sender,  if (Ist•buseDown true)  MouseEventArgs e)  Loc ation XIYI  IsMouseDown  reference  = e. Location ;  = false;  private void sender, PaintEventArgs e)  if (rect null)//Check if rectangle is not a null  e. Graphics .DrawRectang1e(Pen s. Green, GetRect()) ;  1 reference  rect new Rectangle();  rect.X = Math.min(LocationXY.Xs  rect. Y = Math.min(LocationXY.Y,  rect. Width  rect. Height  return rect;  = Math.Abs(LocationXY.X  = math.Abs (LocationXY.Y -  LocationXIY1. X);  LocationXIY1. Y) ;  - LocationXIY1.X);  Loc ationXIY1. Y) ;  @ No issues found
  1. Build the code and start the program. 
  2. Now you'd be able to draw the rectangle using the cursor. 
  3. If you would need more rectangles there would be a need on modifying the code for it.
 

Rectangle rect;
Point LocationXY;
Point LocationX1Y1;

bool IsMouseDown = false;


private void sampleWaveformGraph_MouseDown(object sender, MouseEventArgs e)
        {
            IsMouseDown = true; //If this event has occured so this variable is true

            LocationXY = e.Location; //Ge the starting location of point X and Y
        }

        private void sampleWaveformGraph_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown == true)
            {
                LocationX1Y1 = e.Location;
            }
        }

        private void sampleWaveformGraph_MouseUp(object sender, MouseEventArgs e)
        {
            if (IsMouseDown == true)
            {
                LocationX1Y1 = e.Location;

                IsMouseDown = false;
            }
        }

        private void sampleWaveformGraph_Paint(object sender, PaintEventArgs e)
        {
            if (rect != null)//Check if rectangle is not a null
            {
                e.Graphics.DrawRectangle(Pens.Green, GetRect());
            }
        }

        Rectangle GetRect()
        {
            rect = new Rectangle();

            rect.X = Math.Min(LocationXY.X, LocationX1Y1.X);

            rect.Y = Math.Min(LocationXY.Y, LocationX1Y1.Y);

            rect.Width = Math.Abs(LocationXY.X - LocationX1Y1.X);

            rect.Height = Math.Abs(LocationXY.Y - LocationX1Y1.Y);

            return rect;
        }

Next Steps

1) If you want to delate the rectangles you've drawn you can add a chart event for example to the delete button, and then make the top left corner equal to the bottom left corner, hence making the rectangle 0 in size which will make it disappear:
 Image_2021-10-11_14-51-15.png
2) If you need multiple rectangles you can create a list or an array of rectangles. For a certain event (mouse double click, button press, whatever) he can add a new rectangle to the list.
A nice example of the code can be found in the  [EXTERNAL]Drawing Multiple Rectangles c#  question.

3)We would recommend dedicating a keypress event for selecting the active rectangle which you can draw, delete, resize, etc. So only the active rectangle is affected by any mouse or keyboard event like deletion, but every rectangle in the array gets drawn at each iteration. 
For example for each press of the TAB key, you can select the i+1 rectangle in your array or list it as active. In order to distinguish the active rectangle you can use a separate color to draw it: 
Image_2021-10-11_15-03-57.png
Or you can use Annotations (each rectangle will have a label, e.g. 1, 2, 3...) and a numeric indicator to tell you which rectangle is the active one.