ListBox的简单应用

     刚看完教程,之前很少用到ListBox这个控件,就是在WinForm编程中我也基本上没有用过,都是用DropDownList来代替了,其实这个控件功能比较强大,至少我是这么认为。如果再配合上AJAX的技术,做成联动一样的菜单,用处就更大了!
    在做到删除选定的项目,并且全部删除的时候,出现了个问题,每次都会剩几条,不知道是我算法有问题还是怎么,希望能有人帮我指点指点。谢谢了!我把代码和源文件帖出来。
   天轰穿的教程做的真的很不错,按照自己的理解又把注释更加详细化了,做为一个菜鸟,注释是很重要的东西,把自己的思路归纳总结,是个好事情!

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//将选中的项目放入Laber中,如果选择的项为Mgod,则跳转到我的博客园的地址。
InBlock.gif
        Label1.Text = ListBox1.SelectedValue.ToString();
InBlock.gif        
if (ListBox1.SelectedValue == "Mgod")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Redirect(
"http://mgod.cnblogs.com");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void TextBox1_TextChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ListBox2.Items.Add(TextBox1.Text);
InBlock.gif        Button1.Enabled 
= true;
InBlock.gif        Button2.Enabled
=true;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//点清除,先判断里面是否有值,如果有就清除,如果没有就提示
InBlock.gif
        if (ListBox2.Items.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ListBox2.Items.Remove(ListBox2.SelectedItem);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Button1.Enabled = false; //做了屏蔽,按常理说,如果已经没有任何值的话,按钮应该是不可见的或者是不可选的
InBlock.gif            
//Button2.Enabled = false;
InBlock.gif
            Response.Write("<script>alert('哥们,你已经删干净了!');</script>");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button2_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//点清空,先判断里面是否有值,如果有就清空,如果没有就提示
InBlock.gif
        if (ListBox2.Items.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ListBox2.Items.Clear();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Button1.Enabled = false;  //做了屏蔽,按常理说,如果已经没有任何值的话,按钮应该是不可见的或者是不可选的
InBlock.gif            
//Button2.Enabled = false;
InBlock.gif
            Response.Write("<script>alert('哥们,你已经清空了!');</script>");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button3_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//循环所有的LISTBOX项,将选中的删除
InBlock.gif
        
InBlock.gif        
for (int i=1; i <= ListBox3.Items.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ListBox3.Items.Remove(ListBox3.SelectedItem);           
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//不知道为什么不能全部删除,是我的算法不对吗?请高人指点!
InBlock.gif

ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void ListBox4_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//联动菜单
InBlock.gif
        switch (ListBox4.SelectedValue)//判断选中的值
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif
InBlock.gif            
case "赤色火焰的BLOG":
InBlock.gif                ListBox5.Items.Clear();
InBlock.gif                ListBox5.Items.Add(
"ASP.NET 2.0");
InBlock.gif                ListBox5.Items.Add(
"ASP.NET AJAX");
InBlock.gif                ListBox5.Items.Add(
"C#.NET");
InBlock.gif                ListBox5.Items.Add(
"业界信息");
InBlock.gif                ListBox5.Items.Add(
"个人随笔");
InBlock.gif                
break;
InBlock.gif            
case "个人资料":
InBlock.gif                ListBox5.Items.Clear();
InBlock.gif                ListBox5.Items.Add(
"出生日期");
InBlock.gif                ListBox5.Items.Add(
"性格爱好");
InBlock.gif                ListBox5.Items.Add(
"喜欢的女生类型");
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button4_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//向上下移动一条
InBlock.gif        
//这种临时存储的方法,跟冒泡排序法有很多类似,只是少了很多循环。如果多一个移动到首位的功能的话,还需要加For循环,重新整理一下思路
InBlock.gif

InBlock.gif        
if (((Button)sender).CommandName == "up" && ListBox6.SelectedIndex > 0 || ((Button)sender).CommandName == "down" && ListBox6.SelectedIndex < ListBox6.Items.Count - 1 && ListBox6.SelectedIndex >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif//判断传来的命令是UP,并且所选条目必须大于0,如果是DOWN,则所选条目必须小于最大项
InBlock.gif
            int Index;//为了减少代码,这里做了一个变量判断
InBlock.gif
            if (((Button)sender).CommandName == "up")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Index 
= -1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Index 
= 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            ListItem lt 
= new ListItem(ListBox6.SelectedItem.Text, ListBox6.SelectedValue);//把选中的条目放入lt中,用来做临时保存
InBlock.gif
            ListBox6.Items[ListBox6.SelectedIndex].Text = ListBox6.Items[ListBox6.SelectedIndex + Index].Text;//把所选条目的上一条或者下一条的TEXT赋值给选中的条目
InBlock.gif
            ListBox6.Items[ListBox6.SelectedIndex].Value = ListBox6.Items[ListBox6.SelectedIndex + Index].Value;//把所选条目的上一条或者下一条的Value赋值给选中的条目
InBlock.gif
            ListBox6.Items[ListBox6.SelectedIndex + Index].Text = lt.Text;//把临时存储的选中的项目的内容赋值给选中条目的上一条或者下一条
InBlock.gif
            ListBox6.Items[ListBox6.SelectedIndex + Index].Value = lt.Value;//把临时存储的选中的项目的内容赋值给选中条目的上一条或者下一条
InBlock.gif
            ListBox6.SelectedIndex = ListBox6.SelectedIndex + Index;//设置选中的位置
InBlock.gif

InBlock.gif            ((Button)sender).Focus();
//设置焦点,否则每次都显示的是页头
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button6_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//
InBlock.gif
        if (ListBox6.SelectedIndex >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ListBox6.SelectedIndex 
= 0;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('请选择后再进行操作');</script>");
ExpandedSubBlockEnd.gif        }

InBlock.gif        ((Button)sender).Focus();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button7_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//向上
InBlock.gif
        if (ListBox6.SelectedIndex >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ListBox6.SelectedIndex 
= ListBox6.SelectedIndex - 1;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('请选择后再进行操作');</script>");
ExpandedSubBlockEnd.gif        }

InBlock.gif        ((Button)sender).Focus();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button8_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//向下
InBlock.gif
        if (ListBox6.SelectedIndex >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ListBox6.SelectedIndex 
= ListBox6.SelectedIndex + 1;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('请选择后再进行操作');</script>");
ExpandedSubBlockEnd.gif        }

InBlock.gif        ((Button)sender).Focus();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button9_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//
InBlock.gif
        if (ListBox6.SelectedIndex >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ListBox6.SelectedIndex 
= ListBox6.Items.Count - 1;
InBlock.gif           
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('请选择后再进行操作');</script>");
ExpandedSubBlockEnd.gif        }

InBlock.gif        ((Button)sender).Focus();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


点此下载代码

转载于:https://www.cnblogs.com/mgod/archive/2007/03/31/694868.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值