Options

AddIn crashes RTC

wilk_uwilk_u Member Posts: 94
edited 2011-06-22 in NAV Three Tier
Hello, I've made a simple calculator addin to launch it in nav, here is the code
using System;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Dynamics.Framework.UI.Extensibility;
using Microsoft.Dynamics.Framework.UI.Extensibility.WinForms;

namespace Calc_AddIn
{

    [ControlAddInExport("CalcAddIn")]

    public class SimpleCalc : StringControlAddInBase
    {
        #region object_declarations
        private double valHolder1;
        private double valHolder2;
        private double tempVal;  
        private bool hasDecimal = false;
        private bool inputStatus = true;
        private string calcFunc;
        public TextBox InputBox;
        public Button button0;
        public Button button1;
        public Button button2;
        public Button button3;
        public Button button4;
        public Button button5;
        public Button button6;
        public Button button7;
        public Button button8;
        public Button button9;
        public Button buttondiv;
        public Button buttonmult;
        public Button buttonplus;
        public Button buttonmin;
        public Button buttonc;
        public Button buttonce;
        public Button buttonequal;
        public Button buttondec;
        #endregion

        #region object_definitions
        protected override Control CreateControl()
        {

            Button button0 = new Button();
            button0.Location = new System.Drawing.Point(20, 172);
            button0.Name = "button0";
            button0.Size = new System.Drawing.Size(35, 23);
            button0.TabIndex = 0;
            button0.Text = "0";
            button0.UseVisualStyleBackColor = true;
            button0.Click += new System.EventHandler(button0_Click);

            Button button1 = new Button();
            button1.Location = new System.Drawing.Point(20, 143);
            button1.Name = "button1";
            button1.Size = new System.Drawing.Size(35, 23);
            button1.TabIndex = 0;
            button1.Text = "1";
            button1.UseVisualStyleBackColor = true;
            button1.Click += new System.EventHandler(button1_Click);

            Button button2 = new Button();
            button2.Location = new System.Drawing.Point(61, 143);
            button2.Name = "button2";
            button2.Size = new System.Drawing.Size(35, 23);
            button2.TabIndex = 0;
            button2.Text = "2";
            button2.UseVisualStyleBackColor = true;
            button2.Click += new System.EventHandler(button2_Click);

            Button button3 = new Button();
            button3.Location = new System.Drawing.Point(102, 143);
            button3.Name = "button3";
            button3.Size = new System.Drawing.Size(35, 23);
            button3.TabIndex = 0;
            button3.Text = "3";
            button3.UseVisualStyleBackColor = true;
            button3.Click += new System.EventHandler(button3_Click);

            Button button4 = new Button();
            button4.Location = new System.Drawing.Point(20, 114);
            button4.Name = "button4";
            button4.Size = new System.Drawing.Size(35, 23);
            button4.TabIndex = 0;
            button4.Text = "4";
            button4.UseVisualStyleBackColor = true;
            button4.Click += new System.EventHandler(button4_Click);

            Button button5 = new Button();
            button5.Location = new System.Drawing.Point(61, 114);
            button5.Name = "button5";
            button5.Size = new System.Drawing.Size(35, 23);
            button5.TabIndex = 0;
            button5.Text = "5";
            button5.UseVisualStyleBackColor = true;
            button5.Click += new System.EventHandler(button5_Click);

            Button button6 = new Button();
            button6.Location = new System.Drawing.Point(102, 114);
            button6.Name = "button6";
            button6.Size = new System.Drawing.Size(35, 23);
            button6.TabIndex = 0;
            button6.Text = "6";
            button6.UseVisualStyleBackColor = true;
            button6.Click += new System.EventHandler(button6_Click);

            Button button7 = new Button();
            button7.Location = new System.Drawing.Point(20, 85);
            button7.Name = "button7";
            button7.Size = new System.Drawing.Size(35, 23);
            button7.TabIndex = 0;
            button7.Text = "7";
            button7.UseVisualStyleBackColor = true;
            button7.Click += new System.EventHandler(button7_Click);


            Button button8 = new Button();
            button8.Location = new System.Drawing.Point(61, 85);
            button8.Name = "button8";
            button8.Size = new System.Drawing.Size(35, 23);
            button8.TabIndex = 0;
            button8.Text = "8";
            button8.UseVisualStyleBackColor = true;
            button8.Click += new System.EventHandler(button8_Click);

            Button button9 = new Button();
            button9.Location = new System.Drawing.Point(102, 85);
            button9.Name = "button9";
            button9.Size = new System.Drawing.Size(35, 23);
            button9.TabIndex = 0;
            button9.Text = "9";
            button9.UseVisualStyleBackColor = true;
            button9.Click += new System.EventHandler(button9_Click);

            Button buttondiv = new Button();
            buttondiv.Location = new System.Drawing.Point(143, 85);
            buttondiv.Name = "buttondiv";
            buttondiv.Size = new System.Drawing.Size(35, 23);
            buttondiv.TabIndex = 0;
            buttondiv.Text = "/";
            buttondiv.UseVisualStyleBackColor = true;
            buttondiv.Click += new System.EventHandler(buttondiv_Click);

            Button buttonmult = new Button();
            buttonmult.Location = new System.Drawing.Point(143, 114);
            buttonmult.Name = "buttonmult";
            buttonmult.Size = new System.Drawing.Size(35, 23);
            buttonmult.TabIndex = 0;
            buttonmult.Text = "*";
            buttonmult.UseVisualStyleBackColor = true;
            buttonmult.Click += new System.EventHandler(buttonmult_Click);

            Button buttonmin = new Button();
            buttonmin.Location = new System.Drawing.Point(143, 143);
            buttonmin.Name = "buttonmin";
            buttonmin.Size = new System.Drawing.Size(35, 23);
            buttonmin.TabIndex = 0;
            buttonmin.Text = "-";
            buttonmin.UseVisualStyleBackColor = true;
            buttonmin.Click += new System.EventHandler(buttonmin_Click);

            Button buttonplus = new Button();
            buttonplus.Location = new System.Drawing.Point(143, 172);
            buttonplus.Name = "buttonplus";
            buttonplus.Size = new System.Drawing.Size(35, 23);
            buttonplus.TabIndex = 0;
            buttonplus.Text = "+";
            buttonplus.UseVisualStyleBackColor = true;
            buttonplus.Click += new System.EventHandler(buttonplus_Click);

            Button buttonequal = new Button();
            buttonequal.Location = new System.Drawing.Point(102, 172);
            buttonequal.Name = "buttonequal";
            buttonequal.Size = new System.Drawing.Size(35, 23);
            buttonequal.TabIndex = 0;
            buttonequal.Text = "=";
            buttonequal.UseVisualStyleBackColor = true;
            buttonequal.Click += new System.EventHandler(buttonequal_Click);

            Button buttondec = new Button();
            buttondec.Location = new System.Drawing.Point(61, 147);
            buttondec.Name = "buttondec";
            buttondec.Size = new System.Drawing.Size(35, 23);
            buttondec.TabIndex = 0;
            buttondec.Text = ".";
            buttondec.UseVisualStyleBackColor = true;
            buttondec.Click += new System.EventHandler(buttondec_Click);

            Button buttonc = new Button();
            buttonc.Location = new System.Drawing.Point(124, 44);
            buttonc.Name = "buttonc";
            buttonc.Size = new System.Drawing.Size(54, 23);
            buttonc.TabIndex = 0;
            buttonc.Text = "C";
            buttonc.UseVisualStyleBackColor = true;
            buttonc.Click += new System.EventHandler(buttonc_Click);

            Button buttonce = new Button();
            buttonce.Location = new System.Drawing.Point(62, 44);
            buttonce.Name = "buttonce";
            buttonce.Size = new System.Drawing.Size(54, 23);
            buttonce.TabIndex = 0;
            buttonce.Text = "CE";
            buttonce.UseVisualStyleBackColor = true;
            buttonce.Click += new System.EventHandler(buttonce_Click);

            TextBox Inputbox = new TextBox();
            Inputbox.Location = new System.Drawing.Point(6, 16);
            Inputbox.Name = "Inputbox";
            Inputbox.Size = new System.Drawing.Size(170, 20);

            Panel mainpanel = new Panel();
            mainpanel.Controls.Add(button1);
            mainpanel.Controls.Add(button2);
            mainpanel.Controls.Add(button3);
            mainpanel.Controls.Add(button4);
            mainpanel.Controls.Add(button5);
            mainpanel.Controls.Add(button6);
            mainpanel.Controls.Add(button7);
            mainpanel.Controls.Add(button8);
            mainpanel.Controls.Add(button9);
            mainpanel.Controls.Add(button0);
            mainpanel.Controls.Add(buttonc);
            mainpanel.Controls.Add(buttonce);
            mainpanel.Controls.Add(buttonequal);
            mainpanel.Controls.Add(buttondiv);
            mainpanel.Controls.Add(buttonmult);
            mainpanel.Controls.Add(buttonplus);
            mainpanel.Controls.Add(buttonmin);
            mainpanel.Controls.Add(Inputbox);
            mainpanel.Location = new System.Drawing.Point(12, 12);
            mainpanel.Name = "outlookGroupBox1";
            mainpanel.Padding = new System.Windows.Forms.Padding(3, 3, 3, 3);
            mainpanel.Size = new System.Drawing.Size(235, 232);
            mainpanel.TabIndex = 0;


            return mainpanel;
        }
        #endregion

    #region events

        private void button1_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button1.Text;
            }
            else
            {
                InputBox.Text = button1.Text;
                inputStatus = true;
            }
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button2.Text;
            }
            else
            {
                InputBox.Text = button2.Text;
                inputStatus = true;
            }
        }

        private void button3_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button3.Text;
            }
            else
            {
                InputBox.Text = button3.Text;
                inputStatus = true;
            }
        }

        private void button4_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button4.Text;
            }
            else
            {
                InputBox.Text = button4.Text;
                inputStatus = true;
            }
        }

        private void button5_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button5.Text;
            }
            else
            {
                InputBox.Text = button5.Text;
                inputStatus = true;
            }
        }

        private void button6_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button6.Text;
            }
            else
            {
                InputBox.Text = button6.Text;
                inputStatus = true;
            }
        }

        private void button7_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button7.Text;
            }
            else
            {
                InputBox.Text = button7.Text;
                inputStatus = true;
            }
        }

        private void button8_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button8.Text;
            }
            else
            {
                InputBox.Text = button8.Text;
                inputStatus = true;
            }
        }

        private void button9_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                InputBox.Text += button9.Text;
            }
            else
            {
                InputBox.Text = button9.Text;
                inputStatus = true;
            }
        }

        private void button0_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                if (InputBox.Text.Length >= 1)
                {
                    InputBox.Text += button0.Text;
                }
            }
        }

        private void buttonplus_Click(object sender, System.EventArgs e)
        {
            if(InputBox.Text.Length != 0)
            {
                if (calcFunc == string.Empty)
                {
                    valHolder1 = System.Double.Parse(InputBox.Text);
                    InputBox.Text = string.Empty;
                }
                else
                {
                    CalculateTotals();
                }
                calcFunc = "Add";
                hasDecimal = false;
            }
        }

        private void buttonmin_Click(object sender, System.EventArgs e)
        {
            if (InputBox.Text.Length != 0)
            {
                if (calcFunc == string.Empty)
                {
                    valHolder1 = System.Double.Parse(InputBox.Text);
                    InputBox.Text = string.Empty;
                }
                else
                {
                    CalculateTotals();
                }
                calcFunc = "Minus";
                hasDecimal = false;
            }
        }

        private void buttondiv_Click(object sender, System.EventArgs e)
        {
            if (InputBox.Text.Length != 0)
            {
                if (calcFunc == string.Empty)
                {
                    valHolder1 = System.Double.Parse(InputBox.Text);
                    InputBox.Text = string.Empty;
                }
                else
                {
                    CalculateTotals();
                }
                calcFunc = "Divide";
                hasDecimal = false;
            }
        }

        private void buttonmult_Click(object sender, System.EventArgs e)
        {
            if (InputBox.Text.Length != 0)
            {
                if (calcFunc == string.Empty)
                {
                    valHolder1 = System.Double.Parse(InputBox.Text);
                    InputBox.Text = string.Empty;
                }
                else
                {
                    CalculateTotals();
                }
                calcFunc = "Multiply";
                hasDecimal = false;
            }
        }

        private void buttonequal_Click(object sender, System.EventArgs e)
        {
            if (InputBox.Text.Length != 0 && valHolder1 != 0)
            {
                CalculateTotals();
                calcFunc = string.Empty;
                hasDecimal = false;
            }
        }

        private void buttonce_Click(object sender, System.EventArgs e)
        {
            InputBox.Text = string.Empty;
            hasDecimal = false;
        }

        private void buttonc_Click(object sender, System.EventArgs e)
        {
            InputBox.Text = string.Empty;
            valHolder1 = 0;
            valHolder2 = 0;
            calcFunc = string.Empty;
            hasDecimal = false;
        }

        private void CalculateTotals()
        {
            valHolder2 = System.Double.Parse(InputBox.Text);
            switch (calcFunc)
            {
                case "Add":
                    valHolder1 = valHolder1 + valHolder2;
                    break;
                case "Minus":
                    valHolder1 = valHolder1 - valHolder2;
                    break;
                case "Divide":
                    valHolder1 = valHolder1 / valHolder2;
                    break;
                case "Multiply":
                    valHolder1 = valHolder1 * valHolder2;
                    break;
            }
            InputBox.Text = valHolder1.ToString();
            inputStatus = false;
        }

        private void buttondec_Click(object sender, System.EventArgs e)
        {
            if (inputStatus)
            {
                if (!hasDecimal)
                {
                    if (InputBox.Text.Length != 0)
                    {
                        if (InputBox.Text != "0")
                        {
                            InputBox.Text += buttondec.Text;
                            hasDecimal = true;
                        }
                    }
                    else
                    {
                        InputBox.Text = "0.";
                    }
                }
            }
        }
    #endregion
    }
}
I also attached the dll file below. It should, let to calculate some sums and put it in the i.e price excluding vat field. It launches on page, but for now the problem is it crashes RTC when any button is clicked. Is the problem that i store the values in c# coded variables and read them from calc text box? I coded it like I wolud do that normally in c#, maybe I do something wrong?
Did any of You do something similar? I need some clues how to deal with that..

Regards

P.S I didn't attach, mibuso doesn't allow dll attachments :/

Comments

  • Options
    kinekine Member Posts: 12,562
    Have you checked the event log for more info?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    thmartinthmartin Member Posts: 90
    My guess is that your code runs into a NULL reference error. Try to do some exception handling with try and catch.
    That will at least prevent the RTC from crashing.
    Thomas Martin
    NAV Developer
  • Options
    wilk_uwilk_u Member Posts: 94
    Event log:

    Type: System.NullReferenceException
    Message: Object reference not set to an instance of an object.
    StackTrace:
    at Microsoft.Dynamics.Nav.Client.ExceptionHandler.<>c__DisplayClass2.<DoRethrowWithCatchException>b__0()
    at Microsoft.Dynamics.Nav.Client.ExceptionHandler.ExecuteAndCatchExceptions(Func`1 execute)
    at Microsoft.Dynamics.Nav.Client.ExceptionHandler.DoExecute(Func`1 execute)
    at Microsoft.Dynamics.Nav.Client.ExceptionHandler.DoRethrowWithCatchException(Exception exception)
    Source: Microsoft.Dynamics.Nav.Client
    Type: System.NullReferenceException
    Message: Object reference not set to an instance of an object.
    StackTrace:
    at Calc_AddIn.SimpleCalc.button1_Click(Object sender, EventArgs e)
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    Source: Calc_AddIn

    You're right thmartin.

    Any ideas what is wrong in here? Seeing at c# code it should work..?
  • Options
    kinekine Member Posts: 12,562
      protected override Control CreateControl()
            {
    
                Button button0 = new Button();
    
    It looks like you are creating new controls, but as local variables...thus any work with these variables leads to this exception... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    wilk_uwilk_u Member Posts: 94
    Thank You :)
    I got used to declare object and initialize them when I'm about to use them, I would have to review my habits..
  • Options
    wilk_uwilk_u Member Posts: 94
    Hello, one more thing..
    I displays ok, but for testing i wanted to display value by message and it gives me an error: the dotnet variable has not been instantied.

    I made a test function. declared public global variable in dotnet.

    public static int x = 0;
    //...
    public static int test(){
    x = 5;
    return x;
    }

    but when I write MESSAGE(FORMAT(dotnetVariable.test())) have the same error as above

    any solutions?

    Thank You
  • Options
    mogniemognie Member, Microsoft Employee Posts: 3
    Is this still regarding the control-addin ?
    ________________
    “This posting is provided "AS IS" with no warranties, and confers no rights.”

    Mogens Nielsen
    Senior Development Lead
    Dynamics NAV
    MSFT
  • Options
    wilk_uwilk_u Member Posts: 94
    Yes. Sorry if that wasn't clear.
  • Options
    SogSog Member Posts: 1,023
    I don't know if it is related, but my C# is very rusty.
    But why is x declared as a static int? Same question for the function.
    Maybe NAV doesn't know these static vars.
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • Options
    mogniemognie Member, Microsoft Employee Posts: 3
    How are you accessing the Add-in from C/AL ?
    It looks like you have also created a .net variable with the add-in onto the page ?
    ________________
    “This posting is provided "AS IS" with no warranties, and confers no rights.”

    Mogens Nielsen
    Senior Development Lead
    Dynamics NAV
    MSFT
  • Options
    wilk_uwilk_u Member Posts: 94
    Yes, that's right.

    I made a .net variable, because I want to get access to method i.e. showResult and put the output in message. Form with buttons etc launches ok, but when i try to do above, I'm getting an error

    I've tried static/ non static. Static because no constructor is called.
  • Options
    wilk_uwilk_u Member Posts: 94
    No ideas guys?

    Now I have some time to get back to this..
Sign In or Register to comment.