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

 
在C#中实现listbox的项上下移动(winform)
收藏人:梅毛子360   2013-10-02 | 阅:1  转:2  |  分享 
  |    来源
 
 
 
  
 
 
  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. }  

--------------------------------------------------------------------------------------------------------------------------


C# winform listBox中的项上下移动

晨曦之光 发表于 2012-5-16 17:15 1年前, 0回/136阅
 

开源中国 5 周年,史上最牛定制开源马克杯!

//上移节点
        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);
}
}
}

=====================================================

原文地址:https://www.cnblogs.com/meimao5211/p/3349274.html