In the secondary development of NX, ListBox (column and table box) is a very useful block. This article mainly describes the add and delete functions of ListBox.
As shown in the figure below: Contains two blocks, object selection and list box, to achieve the "+" (add) and "X' (delete) operations on the right side of the list box.
1. Create UIStyler as shown below. Then save as .dlx and .cs
2. Build a listbox demo project in VS2019
3. The linked list uses private static List<NXObject> nXObjectsList = new List<NXObject>();; when initializing, clear the linked list and list box:
nXObjectsList.Clear();
string[] listBoxStrings = new string[0];
_ListBoxUI_ObjectsListBox.SetListItems(listBoxStrings);
4. During initialization, change
_ListBoxUI_ObjectsListBox.SetAddHandler(new NXOpen.BlockStyler.ListBox.AddCallback(AddCallback));
_ListBoxUI_ObjectsListBox.SetDeleteHandler(new NXOpen.BlockStyler.ListBox.DeleteCallback(DeleteCallback));
Uncomment.
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. The code behind
public int AddCallback(NXOpen.BlockStyler.ListBox list_box)
public int DeleteCallback(NXOpen.BlockStyler.ListBox list_box)
Also uncomment and add the code as follows:
//------------------------------------------------------------------------------
//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;
}
}
After the generation is complete, it can be called.
The demonstration is as follows: