分享

在C#中实现listbox的项上下移动(winform)

 昵称10504424 2013-02-20
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Linq;  
    7. using System.Text;  
    8. using System.Windows.Forms;  
    9.   
    10. namespace WindowsFormsApplication1  
    11. {  
    12.     public partial class Form1 : Form  
    13.     {  
    14.         public Form1()  
    15.         {  
    16.             InitializeComponent();  
    17.   
    18.         }  
    19.   
    20.         private void button1_Click(object sender, EventArgs e)  
    21.         {  
    22.             try  
    23.             {  
    24.                 string item = listBox1.SelectedItem.ToString();  
    25.                 int i = listBox1.SelectedIndex;  
    26.                 if (i == 0)  
    27.                     return;  
    28.                 listBox1.Items.Remove(listBox1.SelectedItem.ToString());  
    29.                 listBox1.Items.Insert(i - 1, item);  
    30.                 listBox1.SelectedIndex = i - 1;     
    31.             }  
    32.             catch (Exception)  
    33.             {  
    34.                 MessageBox.Show("未选择项!");  
    35.             }  
    36.   
    37.         }  
    38.   
    39.         private void button2_Click(object sender, EventArgs e)  
    40.         {  
    41.             try  
    42.             {  
    43.                 string item = listBox1.SelectedItem.ToString();  
    44.                 int i = listBox1.SelectedIndex;  
    45.                 if (i == listBox1.Items.Count - 1)  
    46.                     return;  
    47.                 listBox1.Items.Remove(listBox1.SelectedItem.ToString());  
    48.                 listBox1.Items.Insert(i + 1, item);  
    49.                 listBox1.SelectedIndex = i + 1;    
    50.             }  
    51.             catch (Exception)  
    52.             {  
    53.   
    54.                 MessageBox.Show("未选择项!");  
    55.             }  
    56.       
    57.         }  
    58.     }  
    59. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多