12

リスト ボックス 1 で選択した項目をリスト ボックス 2 に、またはその逆に移動しようとしています。と .の 2 つのボタンが>>あり<<ます。listbox1 で項目を選択してから項目をクリックすると、>>listbox1 から listbox2 に移動するはずです。

private void MoveListBoxItems(ListBox source, ListBox destination)
{
    ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
    foreach (var item in sourceItems)
    {
        destination.Items.Add(item);
    }
    while (source.SelectedItems.Count > 0)
    {
        source.Items.Remove(source.SelectedItems[0]);
    }
}

private void button2_Click_1(object sender, EventArgs e)
{
    MoveListBoxItems(listbox , lstActivity);
}
4

7 に答える 7

15

あなたのコードは正常に動作します。私はそれをテストしました。あなたの質問は、「リスト ボックス 1 で選択したアイテムをリスト ボックス 2 に移動しようとしています」です。

あなたのbutton2には問題があると思います.delete button2と以下のコード

private void button2_Click_1(object sender, EventArgs e)
{
    MoveListBoxItems(listbox , lstActivity);
}

次に、他のボタンを作成し、クリックイベントを作成します。

完全なソース:

private void MoveListBoxItems(ListBox source, ListBox destination)
{
    ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
    foreach (var item in sourceItems)
    {
        destination.Items.Add(item);
    }
    while (source.SelectedItems.Count > 0)
    {
        source.Items.Remove(source.SelectedItems[0]);
    }
}

private void first2second_Click(object sender, EventArgs e)
{
    MoveListBoxItems(FirstListbox, LastListbox);
}

private void second2first_Click(object sender, EventArgs e)
{
    MoveListBoxItems(LastListbox, FirstListbox);
}

このコードは仕事です。複数の項目を選択したい場合は、プロパティを変更します SelectionMode = MultiSimpl;

于 2012-11-12T13:38:16.850 に答える
2
private void buttonMoveToListBox1_Click(object sender, EventArgs e)
{
    if(listBox1.SelectedIndex != -1)
    {
        listBox2.Items.Add(listBox1.SelectedValue);
        listBox1.Items.Remove(listBox1.SelectedValue);
    }
}

private void buttonMoveToListBox2_Click(object sender, EventArgs e)
{
    if(listBox2.SelectedIndex != -1)
    {
        listBox1.Items.Add(listBox2.SelectedValue);
        listBox2.Items.Remove(listBox2.SelectedValue);
    }
}
于 2012-11-12T13:19:36.230 に答える
0

削除された行ごとに競合が発生するため、次のコードを使用します。

>>

     for (int intCount = ListBox1.SelectedItems.Count - 1; intCount >= 0; intCount--) 
     {
        ListBox2.Items.Add(ListBox1.SelectedItems[intCount]);
        ListBox1.Items.Remove(ListBox1.SelectedItems[intCount]);
     } 

<<

     for (int intCount = ListBox2.SelectedItems.Count - 1; intCount >= 0; intCount--)
     {
        ListBox1.Items.Add(ListBox2.SelectedItems[intCount]);
        ListBox2.Items.Remove(ListBox2.SelectedItems[intCount]);
     }     

上記の方法でうまくいかない場合は、次の方法を試してください。

while (ListBox1.SelectedItems.Count > 0) 
{ 
    ListBox2.Items.Add(ListBox1.SelectedItems[0].Text);  
    ListBox1.SelectedItems[0].Remove(); 
}

より多くのタイプの回答については、このリンクを使用できます

于 2012-11-12T13:21:56.203 に答える
-2
<script type="text/javascript">
        $(document).ready(function() {
            // > arrow
            $('#SingleRightMove').click(function() {                    
                    $('#fromBox option:selected').remove().appendTo('#toBox');  
                    //$("#tobox option").attr("selected", false);        
                    $('#toBox').find("option").attr("selected", false); 

            });
            // < arrow
            $('#SingleLeftMove').click(function() {
                 $('#toBox option:selected').remove().appendTo('#fromBox');
                 $("#fromBox option").attr("selected", false);
            });
            // >> arrow
            $('#AllRightMove').click(function() {            
                 $('#fromBox option').remove().appendTo('#toBox');
                 $("#toBox option").attr("selected", false);
            });
            // << arrow
            $('#AllLeftMove').click(function() {           
                 $('#toBox option').remove().appendTo('#fromBox');
                 $("#fromBox option").attr("selected", false);
            });




        });



    </script>
于 2013-02-23T10:21:17.980 に答える
-2

以下の listbox2 と 3 のような 2 つのリスト ボックスで作成された >> と << ボタンを取得します... get-content や get-adcomputer などで最初にリスト ボックス 2 の項目を取得します。

$buttonMOVERight_Click={
    foreach ($Srv in $listbox2.selectedItems)
        {$selectedServers=$Srv}
    $listbox3.BeginUpdate()
    foreach($TSrv in $Srv)
        {$listbox3.Items.Add($TSrv);
        $listbox2.Items.Remove($TSrv);}                         
    $listbox3.EndUpdate()
}

$buttonMoveLeft_Click={
        #TODO: Place custom script here
        foreach ($Srvs in $listbox3.selectedItems)
        {$ServersinRt=$Srvs}
    $listbox2.BeginUpdate()
    foreach($rSRv in $Srvs)
        {$listbox2.Items.Add($rSRv);
        $listbox3.Items.Remove($Srvs);}
        $listbox2.EndUpdate()
}
于 2014-03-05T20:16:35.437 に答える