📄 8.5.txt
字号:
Listing 8.5 Moving Items Between Two Multiple-Selection ListBox Objects
private void btnSingleAdd_Click(object sender, System.EventArgs e)
{
// enumerate selected items
foreach( string selectedName in lbContacts.SelectedItems )
{
// add item to other list box
lbRecip.Items.Add( selectedName );
}
// create a copy of all contacts items
// this is done since an exception is thrown if you
// add items to a collection while that
// collection is being enumerated within a foreach loop
ListBox.ObjectCollection newContacts =
new ListBox.ObjectCollection( lbContacts, lbContacts.Items );
foreach( string selectedName in lbContacts.SelectedItems )
{
// remove the item from the temporary collection
newContacts.Remove( selectedName );
}
// clear all items and insert items from temporary collection
lbContacts.Items.Clear();
lbContacts.Items.AddRange( newContacts );
}
private void btnAllAdd_Click(object sender, System.EventArgs e)
{
// add all items from contacts to recipient’s listbox
lbRecip.Items.AddRange( lbContacts.Items );
// clear contacts list box
lbContacts.Items.Clear();
}
private void btnSingleRemove_Click(object sender, System.EventArgs e)
{
foreach( string selectedName in lbRecip.SelectedItems )
{
lbContacts.Items.Add( selectedName );
}
ListBox.ObjectCollection newRecip =
new ListBox.ObjectCollection( lbRecip, lbRecip.Items );
foreach( string selectedName in lbRecip.SelectedItems )
{
newRecip.Remove( selectedName );
}
lbRecip.Items.Clear();
lbRecip.Items.AddRange( newRecip );
}
private void btnAllRemove_Click(object sender, System.EventArgs e)
{
lbContacts.Items.AddRange( lbRecip.Items );
lbRecip.Items.Clear();
}
private void btnNew_Click(object sender, System.EventArgs e)
{
// Note: NewContact class not shown
NewContact contactForm = new NewContact();
contactForm.ShowDialog();
// add new contact to contact list
lbContacts.Items.Add( contactForm.ContactName );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -