0

I am working with sage 200 summer edition and I have an issue with a piece of code I need to be able to do a null check on the Sage.Controls.Button but even if I place it into a var it will still crash its what is called an amendable button in windows forms.

While true most people will stright away suggest this is a null issue and how to solve it its not that quite simple casue I am dealing with sage objects I have searched for existing refersnces for the underlyingcontrol function and cannot find any documenation on the object or how to handle the null

If I just do the following

var control = (Sage.Common.Controls.Button)Form.FindControlByName("saveButton").UnderlyingControl;

And do an if statement around that it will still crash. Any Ideas how to handle this.

public override void ConfigureForm()
{
    var sopOrderReturnLine = (Sage.Accounting.SOP.SOPOrderReturnLine) Form.BoundObjects[0];

    _itemDescription = sopOrderReturnLine.ItemDescription;
    if (sopOrderReturnLine.SOPOrderReturn.SpareBit1)
    {
                var button = new Button();
                button.Name = "extraInfoButton";
                button.Text = "Extra Info...";
                button.Alignment = ButtonAlignmentInContainer.Left;   

                var buttonPanel = (ControlPanel) Form.FindControlByName("buttonPanel").UnderlyingControl;
                buttonPanel.Controls.Add(button);

                button.Click += new EventHandler(button_Click);
            }
            var control = (Sage.Common.Controls.Button)Form.FindControlByName("saveButton").UnderlyingControl;
            if (control !=null)
            {
                var saveButton = (Sage.Common.Controls.Button)Form.FindControlByName("saveButton").UnderlyingControl;
                saveButton.BeforeClick += new System.ComponentModel.CancelEventHandler(saveButton_BeforeClick);
            }
}
Samuel Lelièvre
  • 3,212
  • 1
  • 14
  • 27
Dave
  • 233
  • 2
  • 12
  • You have to check the result of `FindControlByName`, if that returns `null`, your `.UnderlyingControl` will throw a `NullReferenceException`. So store the result of calling that method in a variable, check that for null, and if it isn't, then read `UnderlyingControl`. – Lasse V. Karlsen Feb 26 '19 at 12:28
  • I tried that but it didnt allow me to cause of the cast to sage button would i just use find control by name by itself ? – Dave Feb 26 '19 at 13:04
  • 1
    `var buttonPanel = Form.FindControlByName("buttonPanel"); if (buttonPanel != null) { ((ControlPanel)buttonPanel).UnderlyingControl.Controls.Add(button); }` – Lasse V. Karlsen Feb 27 '19 at 07:59

0 Answers0