This example demonstrates how to create a simple list box with text only list box items and how to add/remove items. You can use the buttons on the right to add/remove items from the list box's Items collection.

| Language: C# |
using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Dom;
using Nevron.Nov.Editors;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;
namespace NovWinFormsProject
{
public partial class Form1 : Form
{
#region Constructors
public Form1()
{
InitializeComponent();
// Set form title
Text = "NOV WinForms - NListBoxItemsManipulationExample";
// Create the NOV example and its controls
NWidget exampleContent = CreateExampleContent();
NWidget exampleControls = CreateExampleControls();
if (exampleControls != null)
{
exampleControls.PreferredWidth = 300;
}
// Place the example and its controls in a pair box
NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
pairBox.FillMode = ENStackFillMode.First;
pairBox.FitMode = ENStackFitMode.First;
pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);
// Host the example in the form
NNovWidgetHost host = new NNovWidgetHost(pairBox);
host.Dock = DockStyle.Fill;
Controls.Add(host);
}
#endregion
#region Example
public NWidget CreateExampleContent()
{
// Create a list box
m_ListBox = new NListBox();
m_ListBox.HorizontalPlacement = ENHorizontalPlacement.Left;
m_ListBox.PreferredSize = new NSize(200, 400);
// Add some items
for (int i = 0; i < 10; i++)
{
NListBoxItem item = new NListBoxItem("Item " + i.ToString());
item.Tag = i;
m_ListBox.Items.Add(item);
}
// Hook to list box selection events
m_ListBox.Selection.Selected += OnListBoxItemSelected;
m_ListBox.Selection.Deselected += OnListBoxItemDeselected;
return m_ListBox;
}
public NWidget CreateExampleControls()
{
NStackPanel stack = new NStackPanel();
stack.FillMode = ENStackFillMode.Last;
stack.FitMode = ENStackFitMode.Last;
// Create the commands group box
stack.Add(CreateCommandsGroupBox());
// Create the events log
m_EventsLog = new NExampleEventsLog();
stack.Add(m_EventsLog);
return stack;
}
public string GetExampleDescription()
{
return @"
This example demonstrates how to create a simple list box with text only list box items and how to add/remove items.
You can use the buttons on the right to add/remove items from the list box's Items collection.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("List Box", "https://www.nevron.com/docs/openvision/ui-list-box.html"),
("Widget Elements", "https://www.nevron.com/docs/openvision/ui-widget-elements.html"),
("User Input", "https://www.nevron.com/docs/openvision/ui-user-input.html"),
};
}
#endregion
#region Implementation
private NGroupBox CreateCommandsGroupBox()
{
NStackPanel commandsStack = new NStackPanel();
// Create the command buttons
m_AddButton = new NButton("Add Item");
m_AddButton.Click += OnAddButtonClick;
commandsStack.Add(m_AddButton);
m_RemoveSelectedButton = new NButton("Remove Selected");
m_RemoveSelectedButton.Enabled = false;
m_RemoveSelectedButton.Click += OnRemoveSelectedButtonClick;
commandsStack.Add(m_RemoveSelectedButton);
m_RemoveAllButton = new NButton("Remove All");
m_RemoveAllButton.Click += OnRemoveAllButtonClick;
commandsStack.Add(m_RemoveAllButton);
// Create the commands group box
NGroupBox commmandsGroupBox = new NGroupBox("Commands");
commmandsGroupBox.Content = commandsStack;
return commmandsGroupBox;
}
#endregion
#region Event Handlers
private void OnListBoxItemSelected(NItemEventArgs args)
{
m_RemoveSelectedButton.Enabled = true;
NListBoxItem item = args.Item;
m_EventsLog.LogEvent("Selected Item: " + item.Tag.ToString());
}
private void OnListBoxItemDeselected(NItemEventArgs args)
{
NListBoxItem item = args.Item;
m_EventsLog.LogEvent("Deselected Item: " + item.Tag.ToString());
}
private void OnAddButtonClick(NEventArgs args)
{
int index;
string value = "0";
if (m_ListBox.Items.Count > 0)
{
NListBoxItem lastItem = m_ListBox.Items[m_ListBox.Items.Count - 1];
NLabel label = (NLabel)lastItem.GetDescendants(new NInstanceOfSchemaFilter(NLabel.NLabelSchema))[0];
value = label.Text;
value = value.Remove(0, value.LastIndexOf(' ') + 1);
}
// Add an item with the calculated index
index = Int32.Parse(value) + 1;
NListBoxItem item = new NListBoxItem("Item " + index);
item.Tag = index;
m_ListBox.Items.Add(item);
if (m_ListBox.Items.Count == 1)
{
m_RemoveAllButton.Enabled = true;
}
}
private void OnRemoveSelectedButtonClick(NEventArgs args)
{
NList selected = m_ListBox.Selection.SelectedItems;
for (int i = 0; i < selected.Count; i++)
{
m_ListBox.Items.Remove(selected[i]);
}
if (m_ListBox.Items.Count == 0)
{
m_RemoveAllButton.Enabled = false;
m_RemoveSelectedButton.Enabled = false;
}
}
private void OnRemoveAllButtonClick(NEventArgs args)
{
m_ListBox.Items.Clear();
m_RemoveAllButton.Enabled = false;
m_RemoveSelectedButton.Enabled = false;
}
#endregion
#region Fields
private NListBox m_ListBox;
private NExampleEventsLog m_EventsLog;
private NButton m_AddButton;
private NButton m_RemoveSelectedButton;
private NButton m_RemoveAllButton;
#endregion
#region Static Methods
#endregion
#region Constants
#endregion
#region Nested Types
#endregion
}
}