Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法中的C#字符串为空_C# - Fatal编程技术网

方法中的C#字符串为空

方法中的C#字符串为空,c#,C#,我对使用C语言编程很陌生。我有一些简单VBA的经验 我正在为Autodesk Inventor编写一个加载项,以便在我们公司中使用。外接程序是一个自定义属性编辑器 到目前为止,我的代码正在运行。我现在遇到的问题是,当从表单中的事件调用方法CreateDescription()时,方法中的字符串被填充。但是,当我从单击OK或Apply按钮开始执行相同操作时,字符串保持为空 也许有什么我忽略了,但因为我是一个新手,我不知道它是什么 多谢各位 using System; using System.Co

我对使用C语言编程很陌生。我有一些简单VBA的经验

我正在为Autodesk Inventor编写一个加载项,以便在我们公司中使用。外接程序是一个自定义属性编辑器

到目前为止,我的代码正在运行。我现在遇到的问题是,当从表单中的事件调用方法
CreateDescription()
时,方法中的字符串被填充。但是,当我从单击OK或Apply按钮开始执行相同操作时,字符串保持为空

也许有什么我忽略了,但因为我是一个新手,我不知道它是什么

多谢各位

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

    }
}
全部删除

InitializeComponent(); 
从你的功能。 您只需要从您的ctor调用它。

删除所有

InitializeComponent(); 
从你的功能。
您只需要从ctor调用它。

不需要使用InitializeComponent();在每个函数和事件中。不需要使用InitializeComponent();在每个函数和事件中。当我删除所有''''InitializeComponent();''时表单保持为空。@lonnebol,但ctor除外。我是说“FormPropertyEditor”。是的,我在发帖后就这么想了。现在字符串也不再是空的。伟大的谢谢。当我删除所有“InitializeComponent();”时表单保持为空。@lonnebol,但ctor除外。我是说“FormPropertyEditor”。是的,我在发帖后就这么想了。现在字符串也不再是空的。伟大的谢谢