在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. C++_运算符重载 总结

    什么是运算符的重载? 运算符与类结合,产生新的含义. 为什么要引入运算符重载? 作用:为了实现类的多态性(多态是指一个函数名有多种含义) 怎么实现运算符的重载? 方式:类的成员函数 或 友元函数(类外 ...

  2. wparam , lparam 传递消息

    01.WM_PAINT消息 LOWORD(lParam)是客户区的宽,HIWORD(lParam)是客户区的高 02.滚动条WM_VSCROLL或WM_HSCROLL消息 LOWORD(wParam) ...

  3. Xamarin绑定ios静态库

    以下是官方的步骤介绍,我就不再一步步解释了 https://docs.microsoft.com/zh-cn/xamarin/ios/platform/binding-objective-c/walk ...

  4. python logger日志

    直接上代码 import logging import logging.handlers import datetime import time import threading from conf. ...

  5. SAS,SATA普及文档

    目前所能见到的硬盘接口类型主要有IDE.SATA.SCSI.SAS.FC等等. IDE是俗称的并口,SATA是俗称的串口,这两种硬盘是个人电脑和低端服务器常见的硬盘.SCSI是"小型计算机系 ...

  6. 创建和获取cookie

    创建和获取cookie 制作人:全心全意 cookie:在互联网中,cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器.通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用 ...

  7. change legend layout from 'vertical' to 'horizontal' in Paraview

    ********** # get color legend/bar for 'vLUT' in view 'renderView1'vLUTColorBar = GetScalarBar(vLUT, ...

  8. String类的判断功能

    /* * Object:是类层级结构中的根类,所有的类都直接或间接的继承自该类. * 如果一个方法的形式参数是Object,那么这里我们就可以传递它的任意的子类对象. * * String类的判断功能 ...

  9. TensorFlow Ops

    TensorFlow Ops 1. Fun with TensorBoard In TensorFlow, you collectively call constants, variables, op ...

  10. [K/3Cloud]K3Cloud平台开发之Python插件

    有时候我们的表单可能很简单,只是一个简单交互的表单,但有可能还是要劳师动众的给它建个工程写个插件,是不是很不爽?例如我有如下一个表单: 功能很简单就是选个业务对象,收集绑定几个字段名,然后确定返回一个 ...