NX二次开发(C#)-UIStyler-ListBox(列表框)的应用

在NX二次开发中,ListBox(列和表框)是一个很有用的块。本文主要讲述ListBox的添加和删除功能。

如下图所示:包含了对象选择和列表框两个块,实现列表框的右侧“+”(添加)和"X'(删除)操作。

1、建立UIStyler如下图。然后保存为.dlx和.cs

2、在VS2019中建立listbox演示项目

3、链表采用private static List nXObjectsList = new List();;在初始化的时候,将链表和列表框清空:

  nXObjectsList.Clear();
  string[] listBoxStrings = new string[0];
  _ListBoxUI_ObjectsListBox.SetListItems(listBoxStrings);

 

NX二次开发(C#)-UIStyler-ListBox(列表框)的应用_第1张图片

 

4、在初始化中,将

               _ListBoxUI_ObjectsListBox.SetAddHandler(new NXOpen.BlockStyler.ListBox.AddCallback(AddCallback));

                _ListBoxUI_ObjectsListBox.SetDeleteHandler(new NXOpen.BlockStyler.ListBox.DeleteCallback(DeleteCallback));

取消注释。

 

public void initialize_cb()
        {
            try
            {
                group = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group");
                _ListBoxUI_ObjectsSelect = (NXOpen.BlockStyler.SelectObject)theDialog.TopBlock.FindBlock("_ListBoxUI_ObjectsSelect");
                group1 = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group1");
                _ListBoxUI_ObjectsListBox = (NXOpen.BlockStyler.ListBox)theDialog.TopBlock.FindBlock("_ListBoxUI_ObjectsListBox");
                //------------------------------------------------------------------------------
                //Registration of ListBox specific callbacks
                //------------------------------------------------------------------------------
                _ListBoxUI_ObjectsListBox.SetAddHandler(new NXOpen.BlockStyler.ListBox.AddCallback(AddCallback));

                _ListBoxUI_ObjectsListBox.SetDeleteHandler(new NXOpen.BlockStyler.ListBox.DeleteCallback(DeleteCallback));

                //------------------------------------------------------------------------------
            }
            catch (Exception ex)
            {
                //---- Enter your exception handling code here -----
                theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
            }
        }

 

5、在后面的代码

 public int AddCallback(NXOpen.BlockStyler.ListBox list_box)

 public int DeleteCallback(NXOpen.BlockStyler.ListBox list_box)

也取消注释,添加代码如下:

 //------------------------------------------------------------------------------
        //ListBox specific callbacks
        //------------------------------------------------------------------------------
        public int AddCallback(NXOpen.BlockStyler.ListBox list_box)
        {
            if(list_box==_ListBoxUI_ObjectsListBox)
            {
                TaggedObject[] taggedObjects = _ListBoxUI_ObjectsSelect.GetSelectedObjects();
                TaggedObject[] restTaggenObjects = new TaggedObject[0];
                foreach(var to in taggedObjects)
                {
                    NXObject nXObject = (NXObject)to;
                    bool isExsit = false;
                    nXObjectsList.ForEach(a =>
                    {
                        if (a.Tag == nXObject.Tag)
                            isExsit = true;
                    });
                    if(isExsit==false)
                    {
                        nXObjectsList.Add(nXObject);
                    }
                }
                string[] listBoxStrings = new string[0];
                foreach(var st in nXObjectsList)
                {
                    Array.Resize(ref listBoxStrings, listBoxStrings.Length + 1);
                    listBoxStrings[listBoxStrings.Length - 1] = st.JournalIdentifier;
                }

                _ListBoxUI_ObjectsListBox.SetListItems(listBoxStrings);
                TaggedObject[] taggedObjects1 = new TaggedObject[0];
                _ListBoxUI_ObjectsSelect.SetSelectedObjects(taggedObjects1);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public int DeleteCallback(NXOpen.BlockStyler.ListBox list_box)
        {
            if(list_box==_ListBoxUI_ObjectsListBox)
            {
                string []listBoxStrings = _ListBoxUI_ObjectsListBox.GetSelectedItemStrings();
                foreach(var lv in listBoxStrings)
                {
                    nXObjectsList.ForEach(a =>
                    {
                        if (a.JournalIdentifier == lv)
                            nXObjectsList.Remove(a);
                });
                }
                listBoxStrings = new string[0];
                foreach (var st in nXObjectsList)
                {
                    Array.Resize(ref listBoxStrings, listBoxStrings.Length + 1);
                    listBoxStrings[listBoxStrings.Length - 1] = st.JournalIdentifier;
                }

                _ListBoxUI_ObjectsListBox.SetListItems(listBoxStrings);

                return 1;
            }
            else
            {
                return 0;
            }
        }
        

生成完成,就可以调用了。

演示如下:

NX二次开发(C#)-UIStyler-ListBox(列表框)的应用_第2张图片

 

你可能感兴趣的:(NX二次开发,UIStyler,c#)