在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. namespace WindowsFormsApplication1
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. try
  20. {
  21. string item = listBox1.SelectedItem.ToString();
  22. int i = listBox1.SelectedIndex;
  23. if (i == 0)
  24. return;
  25. listBox1.Items.Remove(listBox1.SelectedItem.ToString());
  26. listBox1.Items.Insert(i - 1, item);
  27. listBox1.SelectedIndex = i - 1;
  28. }
  29. catch (Exception)
  30. {
  31. MessageBox.Show("未选择项!");
  32. }
  33. }
  34. private void button2_Click(object sender, EventArgs e)
  35. {
  36. try
  37. {
  38. string item = listBox1.SelectedItem.ToString();
  39. int i = listBox1.SelectedIndex;
  40. if (i == listBox1.Items.Count - 1)
  41. return;
  42. listBox1.Items.Remove(listBox1.SelectedItem.ToString());
  43. listBox1.Items.Insert(i + 1, item);
  44. listBox1.SelectedIndex = i + 1;
  45. }
  46. catch (Exception)
  47. {
  48. MessageBox.Show("未选择项!");
  49. }
  50. }
  51. }
  52. }

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

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);
}
}
}

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

在C#中实现listbox的项上下移动(winform) 标准的更多相关文章

  1. WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid

    WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...

  2. asp.net中的ListBox控件添加双击事件

    问题:在Aspx页里的ListBox A中添加双击事件,将选中项添加到另一个ListBox B中,双击ListBox B中的选中项,删除当前选中项 页面: <asp:ListBox ID=&qu ...

  3. "不能在 DropDownList 中选择多个项。"其解决办法及补充

    探讨C#.NET下DropDownList的一个有趣的bug及其解决办法 摘要: 本文就C#.Net 环境下Web开发中经常使用的DropDownList控件的SelectedIndex属性进行了详细 ...

  4. 不能在DropDownList 中选择多个项

    在绑定DropDownList时如果出现多次绑定,会出错以下错误: “不能在DropDownList 中选择多个项” 经了解,只需要在选中值是清空选择即可:xxDropDownList.ClearSe ...

  5. Windows Phone 7 ListBox 列表项渐显加载动画学习笔记

    在wp7程序中,当程序功能越来越复杂时,性能问题是我们不得不考虑的一个问题.在聊天列表中,如果聊天项过多,而且项目UI组件足够复杂时, 我们不得不想尽办法让UI尽快加载.所以有一种可行的方案,就是像Q ...

  6. php 查找数组中是否存在某项,并返回指定的字符串,可用于检查复选,单选等

    /** * 查找数组中是否存在某项,并返回指定的字符串,可用于检查复选,单选等 * @param $id * @param $ids * @param string $returnstr * @ret ...

  7. .net中不能在DropDownList中选中多个项的解决方法

    页面中放有多个DropDownList,点击修改时候,需要根据值来设置两个DropDownList的选中项,当值为空时则需要选中默认值. 页面报错:不能在DropDownList中选中多个项. 直接粘 ...

  8. WPF中反转3D列表项

    原文:WPF中反转3D列表项 WPF中反转3D列表项                                                         周银辉记得在苹果电脑中有一个很酷的 ...

  9. Win10系列:JavaScript 项目模板中的文件和项模板文件

    通过上面内容的学习,相信读者已经对各种项目模板和项模板有了大致的了解,本节将进一步介绍项目模板中默认包含的项目文件以及项模板文件,首先讲解这些文件中的初始内容以及作用,然后介绍在一个页面中如何添加控件 ...

随机推荐

  1. Linux终端常用快捷操作

    命令或文件名自动补全:在输入命令或文件名的前几个字母后,按Tab键,系统会自动补全或提示补全 上下箭头:使用上下箭头可以回溯之前的命令,增加命令的重用,减少输入工作量 !加之前输入过的命令的前几个字母 ...

  2. CAD使用GetXData读数据(com接口)

    主要用到函数说明: MxDrawEntity::GetXData 返回实体的扩展数据. c#代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...

  3. CAD如何设置系统变量

    主要用到函数说明: MxDrawXCustomFunction::Mx_SetSysVar 设置系统变量.详细说明如下: 参数 说明 CString sVarName 系统变量名 Value 需要设置 ...

  4. JavaScipt30(第八个案例)(主要知识点:canvas)

    承接上文,这是第8个案例,要实现的效果是按住鼠标不放,进行拖动时可以在画布上画出不同粗细不同颜色的曲线. 附上项目链接: https://github.com/wesbos/JavaScript30 ...

  5. Java基础——面向对象(封装——继承——多态 )

    对象 对象: 是类的实例(实现世界中 真 实存在的一切事物 可以称为对象) 类: 类是对象的抽象描述 步骤: 1.定义一个类 (用于 描述人:) ( * 人:有特征和行为) 2.根据类 创建对象 -- ...

  6. 2019西安多校联训 Day2

    试题链接:http://www.accoders.com/contest.php?cid=1894   考试密码请私信; T1 残忍WA 0,明明就是一道非常菜的字符串QAQ 思路:一共找四种东西,A ...

  7. HEVC-HM16.9源码学习(1)TEncCu::xCompressCU

    函数入口:Void TEncSlice::compressSlice的m_pcCuEncoder->compressCtu( pCtu );调用xCompressCU( m_ppcBestCU[ ...

  8. openpyxl操作excel文件

    https://blog.csdn.net/hunter_wyh/article/details/78498323

  9. 洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall

    [题解] 其实是个二分图最大匹配的模板题,直接上匈牙利算法就好了. #include<cstdio> #include<algorithm> #define N 1010 #d ...

  10. Hashing - Hard Version

    Hashing - Hard Version Given a hash table of size N, we can define a hash function . Suppose that th ...