AutoCAD.Net/C#.Net QQ群:193522571 同一套窗体代码,同时用在Winform、PvBox和PvTools中

1.在Test文件夹中增加新的Winform项目P1,并且进行前期的窗体调试;

注意需要把tlpMain设置为public:

public System.Windows.Forms.TableLayoutPanel tlpMain;

2.在PressureVessel项目中链接P1;

修改P1项目代码:

namespace PressureVessel
{
    public partial class FrmGasketMy : Form
    {
        public FrmGasketMy()
        {
#if !PVBOX
            InitializeComponent();
#endif
        }

        DataTable dtMain;
        string sGasketType, sMatType;
        string sM, sY;
        string sImageName;

#if PVBOX
        public void FormLoad()
        {
            InitializeComponent();
            btnExit.Visible = false;
#else
        private void FrmGasketMy_Load(object sender, EventArgs e)
        {
#endif

修改P1.designer.cs中的代码:

        private void InitializeComponent()
        {
#if !PVBOX
            this.tlpMain = new System.Windows.Forms.TableLayoutPanel();
#endif
#if !PVBOX
            this.Controls.Add(this.tlpMain);
            this.Load += new System.EventHandler(this.FrmGasketMy_Load);
#endif

3.在PvBox项目中按如下:

//新增加ToolName枚举值
        public enum ToolName
        {
            MaterialAllowanceASME = 1,
            MaterialAllowanceGB,
            GbCategory,
            ToxicityDegree,
            Pipe,
            Flange,
            Fitting,
            Upipe,
            ExpansionJoint,
            Saddle,
            SupportLug,
            BracketSupport,
            HeatExchanger,
            Head,
            PipeLengthCal,
            BoltForce,
            Sanitory,
            LiquidLevel,
            CalWorld,
            Shape,
            HgFastener,
            Fastener,
            ReinforcePlate,
            GasketMy,
        }
        /// <summary>
        /// 垫片MY值
        /// </summary>
        TableLayoutPanel tlpGasketMy = new TableLayoutPanel();
        bool isGasketMyLoad = false;
        FrmGasketMy GasketMy;
                    case ToolName.GasketMy:
                        //垫片MY值
                        UpdateUserRecord();
                        if (!isGasketMyLoad)
                        {
                            scMain.Panel2.Controls.Add(tlpGasketMy);
                            GasketMy.tlpMain = tlpGasketMy;
                            GasketMy.FormLoad();

                            isGasketMyLoad = true;
                            tlpGasketMy.Dock = DockStyle.Fill;
                        }
                        else
                        {
                            scMain.Panel2.Controls.Add(tlpGasketMy);
                        }
                        break;

4.PvTools中:

SearchOut.cs

        /// <summary>
        /// 查垫片MY
        /// <summary>
        public void CDPMY()
        {
            FrmGasketMy frm = new FrmGasketMy();
            frm.SetFrmCenter();
            var result = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm);
        }

Command.cs

        /// <summary>
        ///查垫片MY:查询垫片的M、Y值
        /// <summary>
        [CommandMethod("CDPMY")]
    public static void CDPMY()
    {
      if (IsModelOk("CDPMY"))
      {
        SearchOut.CDPMY();
      }
    }

这样同一个Form就可以在多个项目中使用了,如下图:

a、Winform中:

b、PvBox中:

 c、PvTools中:

原文地址:https://www.cnblogs.com/swtool/p/14950290.html