电竞比分网-中国电竞赛事及体育赛事平台

分享

在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. }  

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多