60 likes | 72 Views
Learn how to use ListBox controls in C++/CLI, including setting selection modes, accessing items and their properties, and handling selection change events. This example demonstrates how to display a collection of values in a scrollable list and how to select and manipulate items.
E N D
Controls in C++/CLI (3) • ListBox (Property: SelectionMode, Items, SelectedItem, SelectedIndex, SelectedItems, SelectedIndices; Event: SelectedIndexChanged) • Show a collection of values, each with index, scrollable; • Select one or more items, event • SelectionMode (None, One, MultiSimple, and MultiExtended) • please see http://msdn.microsoft.com/en-us/library/system.windows.forms.selectionmode.aspx
An Example (1) • Draft a design…. • Form1 and myDialog Classes • Variables, methods • Components, event handlers • Collections
An Example (2) • Start from Form1 • Add Label, Text “Name:”, Name “label1” • Add ListBox, Name “nameList”, SelectionMode “one”; • Add three Buttons for “Add”, “Delete” and Edit”; Name bnAdd, bnDelete, bnEdit; add click event handlers, bnAdd_Click, bnDelete_Click, bnEdit_Click; • Add a form for MyDialog • Add Label, Text “Enter Name:”; Add TextBox, Name myTextBox; • Add two buttons for “OK” and “Cancel”, Name as bnOK, bnCancel; add click events of bnOK_Click and bnCancel_Click; Write handler methods; set DialogResult for each; • Set MyDialog’s AcceptButton and CancelButton;
Strings in C++/CLI • http://msdn.microsoft.com/en-us/library/system.string.aspx • public: static String^ Format( String^ format, ... array<Object^>^ args ) • {index[,length][:formatString]} • {0}, {0, 10}, {0, -10}, {0, -10:D6} • May be needed in Project #3
String^ dateString1 = "1"; String^ fmtString = String::Format("{0,10}",dateString1); // -10 to left, 10 to right Console::WriteLine(fmtString); int dateString2 = 1; String^ fmtString = String::Format("{0,-10:D6}",dateString2); Console::WriteLine(fmtString); String^ result = nullptr; result += String::Format("{0,10}", L"X4"); result += String::Format("{0, 10:D6}", dateString2); result += String::Format("{0,10}", L"end"); Console::WriteLine(result);
String^ result1 = L"X4"; String^ result2 = String::Format("{0} ", dateString2); String^ result3 = L"end"; Console::WriteLine("{0,10} {1, 10} {2,10}", result1, result2, result3); fmtString = String::Format("{0,10} {1, 10} {2,10}", result1, result2, result3); Console::WriteLine(fmtString); fmtString = String::Format("{0,10} {1, 10} {2,10}", result1, dateString1, result3); Console::WriteLine(fmtString);