玩转C科技.NET

每天都在学习,每天都在退步 为什么?世界发展太快! 怎么办?加快学习速度! 如何做?关注.NET社区 进阶中……

导航

<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

统计

公告

Subscribe to this feed Join My Community at MyBloglog!
Contact volnet online!

MSN群MyMSDN技术讨论群
群号:www.msdn@hotmail.com
Windows Live Alerts
欢迎大家踊跃加入讨论任何与技术有关的问题。
————————————
欢迎给我发送邮件:
volnet@tom.com
[标题格式]:[TO玩转C科技]<您的用户名/匿名>[<主题>]
————————————

 
您可以直接Gmail联系我噢!(Gtalk/Mail)
开机自启动,天天都在线哦!

LiveMessenger:
<My Library>
These postings are provided "AS IS" with no warranties, and confer no rights.The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

与我联系

常用链接

留言簿(4)

我参与的团队

我的标签

随笔分类(117)

随笔档案(103)

文章分类(14)

文章档案(15)

相册

家园建设

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

【代码保留】WinForm ListBox上下移动选中项(扩展)

一直都在做asp.net的东西,WinForm好久没碰过了,近乎陌生。今天同事说他的Winform中的ListBox无法上下移动项,让我感觉好奇怪,怎么可能,不就是交替选项么,换换位置应该就可以搞定。看了同事的代码,只觉得一片混沌,实在不忍心再读下去,就自己操刀写一下了。(下面的代码使用了扩展方法,需要编译器版本>=3.0,也可以根据相关语法自行修改成2.0以下的版本)
代码功能:比较简单,就是当选中ListBox中的项的时候,点击上移按钮,项向上移动,点击下移按钮,项向下移动。
[使用:建立cs文件,并COPY以下代码置于其中,即可按照示例所用的方式进行使用了]

public static class ListBoxExtension
{
    
public static bool MoveSelectedItems(this ListBox listBox, bool isUp, Action noSelectAction)
    
{
        
if (listBox.SelectedItems.Count > 0)
        
{
            
return listBox.MoveSelectedItems(isUp);
        }

        
else
        
{
            noSelectAction();
            
return false;
        }

    }


    
public static bool MoveSelectedItems(this ListBox listBox, bool isUp)
    
{
        
bool result = true;
        ListBox.SelectedIndexCollection indices 
= listBox.SelectedIndices;
        
if (isUp)
        
{
            
if (listBox.SelectedItems.Count > 0 && indices[0!= 0)
            
{
                
foreach (int i in indices)
                
{
                    result 
&= MoveSelectedItem(listBox, i, true);
                }

            }

        }

        
else
        
{
            
if (listBox.SelectedItems.Count > 0 && indices[indices.Count - 1!= listBox.Items.Count - 1)
            
{
                
for (int i = indices.Count - 1; i >= 0; i--)
                
{
                    result 
&= MoveSelectedItem(listBox, indices[i], false);
                }

            }

        }

        
return result;
    }


    
public static bool MoveSelectedItem(this ListBox listBox, bool isUp, Action noSelectAction)
    
{
        
if (listBox.SelectedItems.Count > 0)
        
{
            
return MoveSelectedItem(listBox, listBox.SelectedIndex, isUp);
        }

        
else
        
{
            noSelectAction();
            
return false;
        }

    }


    
public static bool MoveSelectedItem(this ListBox listBox, bool isUp)
    
{
        
return MoveSelectedItem(listBox, listBox.SelectedIndex, isUp);
    }


    
private static bool MoveSelectedItem(this ListBox listBox, int selectedIndex, bool isUp)
    
{
        
if (selectedIndex != (isUp ? 0 : listBox.Items.Count - 1))
        
{
            
object current = listBox.Items[selectedIndex];
            
int insertAt = selectedIndex + (isUp ? -1 : 1);

            listBox.Items.RemoveAt(selectedIndex);
            listBox.Items.Insert(insertAt, current);
            listBox.SelectedIndex 
= insertAt;
            
return true;
        }

        
return false;
    }

}

[示例]

        private void btnUp_Click(object sender, EventArgs e)
        
{
            
this.listBox1.MoveSelectedItems(true, () => {
                MessageBox.Show(
"请选择");
            }
);
        }


        
private void btnDown_Click(object sender, EventArgs e)
        
{
            
this.listBox1.MoveSelectedItems(false, () => {
                MessageBox.Show(
"请选择");
            }
);
        }

怎么样,代码是不是足够简洁和优雅?基本上可以达到预期的效果了,大家可以根据自己的需求稍做修改。有任何问题和疑问可以留言告诉我!

Tag标签: Winform,ListBox

posted on 2008-05-07 13:46 volnet(可以叫我大V) 阅读(256) 评论(0)  编辑 收藏 所属分类: C# Controls.NET Framework

使用Live Messenger联系我
关闭