MessageBox pops up with every change on grasshopper canvas

Hey I am trying to make a message box in a C# component to inform the user if some data was recorded or not. However, I keep seeing it over and over with whatever changes that occur in the script, does anyone know how to optimize this so that I can only see it once the data is recorded, the code is as below and I attached the .GH file

private void RunScript(int x, List<string> y, ref object A)
  {

    if (x == 0)
    {
      System.Windows.Forms.MessageBox.Show("This component is happy! you can move on", "01.Material Groups", MessageBoxButtons.OKCancel);
    }

    else if (x == 1)
    {
      System.Windows.Forms.MessageBox.Show("This component has encounterd an error! Please try again", "01.Material Groups", MessageBoxButtons.OKCancel);
    }
    A = y;
  }

MessageBox.gh (7.0 KB)

Hi,

Runscript is called for any recomputation of the component. This usually happends if the input changes or if it is explicitly triggered.
Create a property and override the setter to only change the value if its different

private int _x = int.MaxValue;
public int X
{
   get {return _x;}
   set 
   {
        if (value != _x)
        {
             _x = value;
             NotifyUser(value); 
        }
   }
}
private void NotifyUser(int val)
{
    // ... MessageBox.Show 
}

-> inside Runscript just call: 
X = x;

As an alternative you can do that without a property by just

private int _x = int.MaxValue;

private void NotifyUser_IfValueChanged(int val)
{
      if (val != _x)
      {
             _x = value;
             // ... MessageBox.Show
      }
}
-> inside Runscript just call: 
NotifyUser_IfValueChanged(x);
1 Like

Thank you so much!