开发者

How to add/move multiselected items from one listbox to another listbox?

I have a small prototype that allows to add selected item from one listbox to another. I need to be able to select multiple item from listbox and move them to another and backward - from second listbox to first one. I am wondering if someone has good sample or can modify the code I already have. Thank you in advance.

Two listboxes and buttons XAML:

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>    
    <Border Margin="0,40,225,20">
        <ListBox x:Name="LeftListBox" SelectionMode="Multiple"/>
    </Border>
    <Border Margin="0,40,20,20" BorderThickness="1" BorderBrush="#FFCECECE" HorizontalAlignment="Right" Width="169" Padding="5" >
        <ListBox x:Name="RightListBox" BorderThickness="0" />
    </Border>
    <Button x:Name="AddButton" Height="40" Margin="0,3,198,0" VerticalAlignment="Center" Click="AddButton_Click" HorizontalAlignment="Right" Width="15" Content="▶" />
    <Button x:Name="RemoveButton" Height="40" Margin="0,94,198,0" VerticalAlignment="Center" Click="RemoveButton_Click" HorizontalAlignment="Right" Width="15" Content="R" />
</Grid>

Code behind:

public partial class SelectServersUC : UserControl
{

    private ArrayList myDataList = null;
    string currentItemText ;
    int currentItemIndex ;

    public SelectServersUC()
    {
        this.InitializeComponent();
        myDataList = LoadListBoxData();
        LeftListBox.ItemsSource = myDataList;  
    }
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        myDataList = LoadListBoxData();
        LeftListBox.ItemsSource = myDataList;  
    }
    private ArrayList LoadListBoxData()
    {
        ArrayList itemsList = new ArrayList();
        itemsList.Add("Item1");
        itemsList.Add("Item2");
        itemsList.Add("Item3");
        itemsList.Add("Item4");
        itemsList.Add("Item5");
        itemsList.Add("Item6");
        itemsList.Add("Item7");
        itemsList.Add("Item8");
        itemsList.Add("Item9");
        itemsList.Add("Item10");
        return itemsList;
    }

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        // Find the right item and it's value and index
        currentItemText = LeftListBox.SelectedValue.ToString();
        currentItemIndex = LeftListBox.SelectedIndex;

        RightListBox.Items.Add(currentItemText);
        if (myDataList != null)
        {
            myDataList.RemoveAt(currentItemIndex);
        }

        // Refresh data binding
        ApplyDataBinding();
    }

    private void RemoveButton_Click(object sender, RoutedEventArgs e)
    {
        // Find the right item and it's value and index
        currentItemText = RightListBox.SelectedValue.ToString();
        currentItemIndex = RightListBox.SelectedIndex;
        // Add RightListBox item to the ArrayList
        myDataList.Add(currentItemText);

       // LeftListBox.Items.Add(RightListBox.SelectedItem);
        RightListBox.Items.RemoveAt(RightListBox.Items.IndexOf(RightListBox.SelectedItem));

        // Refresh data binding
        ApplyDataBinding();
    }

    /// <summary>
    /// Refreshes data binding
    /// </summary>
    private开发者_Python百科 void ApplyDataBinding()
    {
        LeftListBox.ItemsSource = null;
        // Bind ArrayList with the ListBox
        LeftListBox.ItemsSource = myDataList;
    }


just have a look at this

Move items from one listbox to another

http://www.c-sharpcorner.com/UploadFile/mahesh/WPFListBoxDataTransfer07272008130032PM/WPFListBoxDataTransfer.aspx


public partial class listtolist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox1.Items[i].Selected)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox2.Items[i].Selected)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜