在C#中实现listbox的项上下移动(winform) 标准
| 
 | 
--------------------------------------------------------------------------------------------------------------------------
//上移节点
        private void btnUP_Click(object sender, EventArgs e)
        {
            int lbxLength = this.listBoxMenu.Items.Count;//listbox的长度   
            int iselect = this.listBoxMenu.SelectedIndex;//listbox选择的索引   
            if (lbxLength > iselect && iselect>0)
            {
                object oTempItem = this.listBoxMenu.SelectedItem;
                this.listBoxMenu.Items.RemoveAt(iselect);
                this.listBoxMenu.Items.Insert(iselect - 1, oTempItem);
                this.listBoxMenu.SelectedIndex = iselect - 1;
            }   
        }
        //下移节点
        private void btnDown_Click(object sender, EventArgs e)
        {
            int lbxLength = this.listBoxMenu.Items.Count;//listbox的长度   
            int iselect = this.listBoxMenu.SelectedIndex;//listbox选择的索引   
            if (lbxLength > iselect && iselect<lbxLength-1)
            {
                object oTempItem = this.listBoxMenu.SelectedItem;
                this.listBoxMenu.Items.RemoveAt(iselect);
                this.listBoxMenu.Items.Insert(iselect + 1, oTempItem);
                this.listBoxMenu.SelectedIndex = iselect + 1;
            }   
        }
原文链接:http://blog.csdn.net/maji9370/article/details/4294032
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GHGD.BLL;
namespace GHGD.UI
{
    public partial class Form1 : Form
    {
        FrmDic_BLL frmDic_BLL = new FrmDic_BLL();
        int strID = 0;
        public Form1()
        {
            InitializeComponent();
}
        private void Form1_Load(object sender, EventArgs e)
        {
            //ListBox lst_frmDic_Type_Property = new ListBox();
            //lst_frmDic_Type_Property.Items.Add("1");
            //lst_frmDic_Type_Property.Items.Add("2");
            //lst_frmDic_Type_Property.Items.Add("3");
            //lst_frmDic_Type_Property.Items.Add("4");
            //lst_frmDic_Type_Property.Items.Add("5");
            
            DataSet dsFrmDic = frmDic_BLL.FrmDic_Dic_GetInfo();
            lst_frmDic_Type.DataSource = dsFrmDic.Tables[0];
            lst_frmDic_Type.DisplayMember = "DICType";
            lst_frmDic_Type.ValueMember = "ID";
            //for (int i = 0; i < 10; i++)
            //    lst_frmDic_Type_Property.Items.Add(i + 1);
        }
        private void lst_frmDic_Type_SelectedValueChanged(object sender, EventArgs e)
        {
            string strLstDicType = lst_frmDic_Type.Text;
            FrmDicTypePropertyOnLoad(strLstDicType);
        }
        private void FrmDicTypePropertyOnLoad(string strLstDicType)
        {
            DataSet dsFrmDicTypeProperty = frmDic_BLL.FrmDic_Dic_GetInfo_Type(strLstDicType);
            lst_frmDic_Type_Property.DataSource = dsFrmDicTypeProperty.Tables[0];
            lst_frmDic_Type_Property.DisplayMember = "DICName";
            lst_frmDic_Type_Property.ValueMember = "ID";
        }
        private void lst_frmDic_Type_Porperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strLstDicTypeProperty = lst_frmDic_Type_Property.Text;
            txt_frmDicTypePorperty_word.Text = strLstDicTypeProperty;
            DataSet ds = frmDic_BLL.FrmDic_Dic_GetTypeProperty_ID(strLstDicTypeProperty);
            strID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
        }
        private void btn_frmDicType_MoveUp_Click(object sender, EventArgs e)
        {
            int position = lst_frmDic_Type_Property.SelectedIndex;
            string value = lst_frmDic_Type_Property.SelectedItem.ToString();
            MessageBox.Show("position: " + position + " _ " + "value: " + value);
            if (position == 0)
            {
                MessageBox.Show("已在当前最顶端,无法再移动...");
                return;
            }
            else
            {
                lst_frmDic_Type_Property.Items.RemoveAt(position);
                lst_frmDic_Type_Property.Items.Insert(position - 1, value);
            }
            lst_frmDic_Type_Property.SetSelected(position - 1, true);
}
        private void btn_frmDicType_MoveDown_Click(object sender, EventArgs e)
        {
            int position = lst_frmDic_Type_Property.SelectedIndex;
            string value = lst_frmDic_Type_Property.SelectedItem.ToString();
            if (position == lst_frmDic_Type_Property.Items.Count - 1)
            {
                MessageBox.Show("已在当前最底端,无法再移动...");
                return;
            }
            else
            {
                lst_frmDic_Type_Property.Items.RemoveAt(position);
                lst_frmDic_Type_Property.Items.Insert(position + 1, value);
            }
            lst_frmDic_Type_Property.SetSelected(position + 1, true);
        }
    }
}
=====================================================
 
                    
                



 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号