1

I am quite new to programming using C#. I have some experience with simple VBA.

I am writing a addin for Autodesk Inventor to use in our company. The addin is a custom property editor.

So far my code is working. The trouble I have now is that when the method CreateDescription() is called from an event from the form, the strings in the method are filled. But, when I do the same from when the OK or Apply button is clicked, the strings stay empty.

Probably there is something I'm overlooking, but since I'm a newbie I don't know what it is.

Thank you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Inventor;


namespace XXX_Property_Editor
{
    public partial class FormPropertyEditor : Form
    {

        public FormPropertyEditor()
        {
            InitializeComponent();

            string progId = "Inventor.Application";
            Type inventorApplicationType = Type.GetTypeFromProgID(progId);

            Inventor.Application invApp = (Inventor.Application)Marshal.GetActiveObject(progId);

            Document oDoc = (Document)invApp.ActiveDocument;
            PropertySets oPropertySets = oDoc.PropertySets;

        }

        #region Buttons

        private void buttonOK_Click(object sender, EventArgs e)
        {
            InitializeComponent();

            string progId = "Inventor.Application";
            Type inventorApplicationType = Type.GetTypeFromProgID(progId);

            Inventor.Application invApp = (Inventor.Application)Marshal.GetActiveObject(progId);

            //Get the active document in Inventor
            Document oDoc = (Document)invApp.ActiveDocument;

            //Get corrent Windows username
            string userName = System.Environment.UserName;

            string txtDescription = CreateDescription();

            SetProperty setProperty = new SetProperty();
            setProperty.EditDesignTrackingProperty("Description", txtDescription, oDoc);
            setProperty.EditSummaryProperty("Title", txtDescription, oDoc);
            setProperty.EditDocumentSummaryProperty("Company", "MHS", oDoc);
            setProperty.EditSummaryProperty("Author", userName, oDoc);
            setProperty.EditDesignTrackingProperty("Designer", userName, oDoc);
            setProperty.EditDesignTrackingProperty("Engineer", userName, oDoc);

            //setProperty.CreateAndEditCustomProperty("Loation", "xxx, xxx", oDoc);
            //setProperty.EditDesignTrackingProperty("Project", "GPA0012345", oDoc);

            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            InitializeComponent();

            string progId = "Inventor.Application";
            Type inventorApplicationType = Type.GetTypeFromProgID(progId);

            Inventor.Application invApp = (Inventor.Application)Marshal.GetActiveObject(progId);

            //Get the active document in Inventor
            Document oDoc = (Document)invApp.ActiveDocument;

            //Get corrent Windows username
            string userName = System.Environment.UserName;

            string txtDescription = CreateDescription();
            MessageBox.Show(txtDescription);

            SetProperty setProperty = new SetProperty();
            setProperty.EditDesignTrackingProperty("Description", txtDescription, oDoc);
            setProperty.EditSummaryProperty("Title", txtDescription, oDoc);
            setProperty.EditDocumentSummaryProperty("Company", "XXX", oDoc);
            setProperty.EditSummaryProperty("Author", userName, oDoc);
            setProperty.EditDesignTrackingProperty("Designer", userName, oDoc);
            setProperty.EditDesignTrackingProperty("Engineer", userName, oDoc);
        }

        #endregion


        #region Description

        private void comboBoxPrefix_SelectionChangeCommitted(object sender, EventArgs e)
        {
            this.labelDescription.Text = CreateDescription();
        }

        private void comboBoxDescription1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            this.labelDescription.Text = CreateDescription();
        }

        private void comboBoxDescription2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.labelDescription.Text = CreateDescription();
        }

        private void textBoxFreeText_TextChanged(object sender, EventArgs e)
        {
            this.labelDescription.Text = CreateDescription();
        }

        private string CreateDescription()
        {
            string txtPrefix = this.comboBoxPrefix.GetItemText(comboBoxPrefix.SelectedItem);
            string txtDescription1 = this.comboBoxDescription1.GetItemText(comboBoxDescription1.SelectedItem);
            string txtDescription2 = this.comboBoxDescription2.GetItemText(comboBoxDescription2.SelectedItem);
            string txtFree = this.textBoxFreeText.Text;
            string txtDescription = txtPrefix + "_" + txtDescription1 + " " + txtDescription2 + " " + txtFree;

            MessageBox.Show(txtPrefix);
            MessageBox.Show(txtDescription1);
            MessageBox.Show(txtDescription2);
            MessageBox.Show(txtFree);

            return txtDescription;
        }
        #endregion

    }
}
lonnebol
  • 59
  • 6

1 Answers1

3

Remove All

InitializeComponent(); 

From your functions. You just need to call it from your ctor.

Farshad
  • 347
  • 2
  • 13