在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. R语言学习 - 线图一步法

    首先把测试数据存储到文件中方便调用.数据矩阵存储在line_data.xls和line_data_melt.xls文件中 (直接拷贝到文件中也可以,这里这么操作只是为了随文章提供个测试文件,方便使用. ...

  2. php redis使用 常用方法

    基本 $redis = new Redis();//创建对象 $redis->connect('127.0.0.1',6379);//建立连接 $redis->delete('test') ...

  3. 浅谈FFC

    FFC(Flexible Formatting Context) CSS3引入了一种新的布局模型——flex布局(之前有文章介绍过).flex是flexible box的缩写,一般称之为弹性盒模型.和 ...

  4. java基础学习日志---File方法分析

    package FunDemo; import java.io.File; import java.io.IOException; import java.util.Arrays; public cl ...

  5. LCS(HDU_5495 循环节)

    传送门:LCS 题意:给出两个序列an和bn,想在给出一个序列pn,问经过a[p1],,,,a[pn]和b[p1],,,b[pn]变换后序列a和序列b的最长公共子序列的长度是多少. 思路:对a[i]- ...

  6. Find The Multiple POJ - 1426 (BFS)

    题目大意 给定一个整数,寻找一个只有0,1构成的十进制数使得这个数能够整除这个整数 解法 直接bfs第一位放入1,之后每一位放入1或者0 代码 #include <iostream> #i ...

  7. 洛谷 3979 BZOJ 3083 遥远的国度

    [题解] 这道题除去根操作就是普通的树链剖分了.但是有换根操作怎么处理呢? 我们可以发现如果现在的根不在查询的点的子树里,那么对本次查询没有影响.如果现在的跟在查询的点x的子树里,那么答案将变为整棵树 ...

  8. 关于datanode多磁盘存储策略

    目的: 节点内各存储磁盘均衡 相关参数: dfs.datanode.fsdataset.volume.choosing.policy=org.apache.hadoop.hdfs.server.dat ...

  9. 【Codeforces 584C】Marina and Vasya

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 设cnt表示s1和s2不同的字符的个数 如果cnt>2t 因为这cnt个位置肯定至少有一边不同 显然肯定会有一个f(s,S)的值大于t的 ...

  10. nyoj 5 Binary String Matching(string)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...