将变量从表单传递到C类#

时间:2014-07-13 20:47:02

标签: c# forms class parameter-passing

我正在处理一个简单的表单,它有多个TabPages,每个表单中都有一个列表框。当用户单击OK时,我搜索哪个TabPage处于活动状态,哪个SelectedItem是当前的并设置为变量。没问题。

我的类“cmdObjStyles.cs”调用“frmObjStyles.cs”形式,从用户那里获取结果,然后将结果拉回到cmdObjStyles.cs类中。

问题是变量“curTab和”curItem“(当我关闭表格时,我想要带回我班级的字符串变得脱离了上下文。

所以我尝试将变量设置为public,但在阅读了大量帖子之后,这不是一个好习惯。然后我尝试使用Get,Set但是我无法让它工作,而且我从网上的视频中并没有真正了解它。

必须有一种更简单的方法来做这件事。

我的确定代码:

        private void btnOk_Click(object sender, EventArgs e)
         {
        // Get the currently selected item from the Active Tab and ListBox
        string curTab = tabCntrlObjStyle.SelectedTab.Text;
        string voidCurItem = "-----------------------";
        if (curTab == "General")
        {
            string curItem = lstBoxGen.GetItemText(lstBoxGen.SelectedItem);
            if (curItem.Equals(voidCurItem))
            {
                MessageBox.Show("Please Select a Valid Revit Category", "Re-Select Item from List: ");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }
        if (curTab == "Structural")
        {
            string curItem = lstBoxStruc.GetItemText(lstBoxStruc.SelectedItem);
            if (curItem.Equals(voidCurItem))
            {
                MessageBox.Show("Please Select a Valid Revit Category", "Re-Select Item from List: ");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }
        else
        {
            MessageBox.Show("Revit Category in Invalid", "Invalid Selection: ");
        }


    }

我的简单课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Windows.Forms;


namespace STN_BIM_Manager_Ribbon
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdObjStyles : IExternalCommand
{
    static AddInId appId = new AddInId(new Guid("2E66FC45-F0AC-4212-9BCE-E8C331A8CC66"));
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIDocument uidoc = commandData.Application.ActiveUIDocument;
        Document doc = uidoc.Document;
        string curItem;
        string curItem2;

        // Start User Form and Get Input
        using (frmObjStyles thisForm = new frmObjStyles())
        {
            thisForm.ShowDialog();
            if (thisForm.DialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                return Result.Cancelled;
            }

            curItem2 = thisForm.tabCntrlObjStyle.SelectedTab.ToString();
            curItem = thisForm.lstBoxStruc.SelectedItem.ToString();
            MessageBox.Show("This is your Selected Value: " + 
                Environment.NewLine + curItem + 
                Environment.NewLine + curItem2,
                "Your Data", MessageBoxButtons.OK);
            // Create a Collection of Values a user can Select
            //List<string> structList = new List<string>(structInput);



        }

        return Result.Succeeded;
    }
}
}

1 个答案:

答案 0 :(得分:0)

正如您所说,在用户单击“确定”按钮时将在表单中创建公共属性,同时在Execute方法的cmdObjStyles类中将表单的AcceptButton属性设置为btnOk.Now尝试此操作(在using语句中) :

using (frmObjStyles thisForm = new frmObjStyles())
{
     thisForm.ShowDialog();
     if (thisForm.DialogResult == System.Windows.Forms.DialogResult.OK)
     {
          //curItem2 = thisForm.tabCntrlObjStyle.SelectedTab.ToString();
          curItem2 = thisForm.ThePropertyYouCreated;
          //curItem = thisForm.lstBoxStruc.SelectedItem.ToString();
          curItem = thisForm.TheOtherPropertyYouCreated;
          MessageBox.Show("This is your Selected Value: " +
          Environment.NewLine + curItem +
                        Environment.NewLine + curItem2,
                        "Your Data", MessageBoxButtons.OK);
          // Create a Collection of Values a user can Select
          //List<string> structList = new List<string>(structInput);
          //it would probably be better to place this next here...
          //return Result.Succeeded;
     }
     else
     {
          return Result.Cancelled;
     }


}