将变量从窗体传递到c#类

本文关键字:变量 窗体 | 更新日期: 2023-09-27 18:12:48

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

我的类"cmdObjStyles.cs"调用表单"frmObjStyles.cs,从用户获取结果,并将结果拉回类cmdObjStyles.cs。

问题是变量"curTab"answers"curItem"(这是我想带回我的类的字符串)在表单关闭时脱离了上下文。

所以我尝试将变量设置为公共,但通过阅读一堆帖子后,这不是一个很好的做法。然后我试着使用Get,Set,但是我不能让它工作,而且我从网上关于它们的视频中并没有真正理解它。

一定有更简单的方法…

My From OK code:

        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;
    }
}
}

将变量从窗体传递到c#类

正如您所说的,在您的表单中创建公共属性,当用户单击OK按钮时将得到设置,还将表单AcceptButton属性设置为btnOk。现在在你的cmdObjStyles类的Execute方法中试试这个(在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;
     }

}