vs2005控件演示之 列表框 《ListBox》

第一次发表到首页,我觉得这应该还是值得,如果DUDU审后觉得不合适,帮我还是放新手里面就是了,谢谢!

列表框 ListBox  我也不知道怎么形容这个,反正网上用得很多!他的大概功能就是把下拉列表给长高了,然后不能从下面伸一截出来了,但是他一样可以装很多东西,而且有一优势,可以同时选择多个!,下面分别做几个演示!!

属性列表

SelectionMode组件中条目的选择的类型即:多选、单选。Single,Multiple
Rows 此组件显示总共多少行
Selected检测条目十分被选中
SelectedItem返回的类型是ListItem,获得组件中被选择的条目
Count组件中条目的总数
SelectedIndex组件中被选择的条目的索引值
Items泛指组件中所有的条目,每一个条目的类型都是ListItem

  演示一: 响应列表框改变的事件(多事件)
演示二: 动态添加列表框中的项,并且移出指定项

演示三 : 列表框里的值可以一次选择多个
演示四 ,两级联动菜单

演示五 : 如何实现组件中的指定条目的移位和移动指针到指定位置

因为代码太多,一次传不上来(我试了 4次了,都整成系统忙了,郁闷,所以后台CS代码我就放评论里面了)

前台HTML代码

后台CS代码见评论
<script type="text/javascript"> // </script>

Feedback

# re: vs2005控件演示之 列表框 《ListBox》   回复  引用  查看    

2006-05-31 12:25 by 天轰穿
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public  partial  class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    
{//演示一的事件
        Label1.Text = ListBox1.SelectedValue.ToString();//把Label的文字赋值为列表框中被选中的值
        if (ListBox1.SelectedValue == "洪川医药")//当选定项的值等于 洪川医药的时候就转到hc115.com 去
        {
            Response.Redirect(
"http://www.hc115.com/qyml/");
        }

    }

    
protected void TextBox1_TextChanged(object sender, EventArgs e)
    
{//懒得用按纽了,演示二的添加列表项动作
        ListBox2.Items.Add(TextBox1.Text);//给列表项添加一项
        Button1.Text = "移除";//防止有人先在点击了移除按纽,所以下面做了处理,这里如果是添加了,当然按纽就还原了撒
        Button1.Enabled = true;
        Button2.Text 
= "清空";//防止有人先在点击了移除按纽,所以下面做了处理,这里如果是添加了,当然按纽就还原了撒
        Button2.Enabled = true;
    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{//移出事件
        if (ListBox2.Items.Count > 0)//如果列表项的索引值大与0
        {
            ListBox2.Items.Remove(ListBox2.SelectedItem);
//就移除指定项
        }

        
else
        
{//否则
            Button1.Text = "你疯了哇,都没有东西,你叫我删什么,有本事你自己去删嘛";
            Button1.Enabled 
= false;
        }

    }

    
protected void Button2_Click(object sender, EventArgs e)
    
{//原理基本同上
        if (ListBox2.Items.Count > 0)//如果列表项的索引值大与0
        {
            ListBox2.Items.Clear();
//就清空所有项
        }

        
else
        
{//否则
            Button2.Text = "你疯了哇,都没有东西,你叫我清空什么,有本事你自己去清空嘛";
            Button2.Enabled 
= false;
        }

    }

 
    
protected void ListBox4_SelectedIndexChanged(object sender, EventArgs e)
    
{//两级联动菜单事件
        switch (ListBox4.SelectedValue)//判断一级列表中被选中的值
        
            
case "洪川医药"://如果是这个,那二级就添加下面这些
                ListBox5.Items.Add("医院评价");
                ListBox5.Items.Add(
"医院名录");
                ListBox5.Items.Add(
"假药暴光");
                ListBox5.Items.Add(
"医药黑幕");
                
break;
            
case "天轰穿的博客"://如果是这个,那二级就添加下面这些
                ListBox5.Items.Add("Vs2005系列控件");
                ListBox5.Items.Add(
"学习笔记");
                
break;
        }

    }

    
protected void Button7_Click(object sender, EventArgs e)
    
{//向上下移动一条 事件
        if (((Button)sender).CommandName == "up" && ListBox6.SelectedIndex > 0 || ((Button)sender).CommandName == "down" && ListBox6.SelectedIndex < ListBox6.Items.Count - 1)
        
//判断传来的命令名必须是 up并且所选条目的索引必须大于0 或者 down并且所选条目必须小于最大项
           
            
int index;//为了减少代码,这里做一个对变量的判断,以后就直接调用变量,
            if (((Button)sender).CommandName == "up")
            
{
                index 
= -1;//以后的索引本来就是在当前的条目上加一或者减,所以这个方法很不错 
            }

            
else
            
{
                index 
= 1;
            }

            ListItem lt 
= new ListItem(ListBox6.SelectedItem.Text,ListBox6.SelectedValue);//将当前条目的文本以及值都保存到一个临时变量里面
            ListBox6.Items[ListBox6.SelectedIndex].Text = ListBox6.Items[ListBox6.SelectedIndex + index].Text;//被选中项的值等于上一条或者下一条的值
            ListBox6.Items[ListBox6.SelectedIndex].Value = ListBox6.Items[ListBox6.SelectedIndex + index].Value;//被选中项的值等于上一条或者下一条的值
            ListBox6.Items[ListBox6.SelectedIndex + index].Text = lt.Text;//把被选中项的上一条或者下一条的值用临时变量中的取代
            ListBox6.Items[ListBox6.SelectedIndex + index].Value = lt.Value;//把被选中项的上一条或者下一条的值用临时变量中的取代
            ListBox6.SelectedIndex = ListBox6.SelectedIndex + index;//把鼠标指针放到移动后的那条上
        }

    }

    
protected void Button4_Click(object sender, EventArgs e)
    
{//移至首条
        ListBox6.SelectedIndex = 0;//将被选中项的索引设置为0就可以啦,
    }

    
protected void Button9_Click(object sender, EventArgs e)
    
{//移至尾条
        ListBox6.SelectedIndex = ListBox6.Items.Count-1;//因为C#里面默认的索引都是从0开始,所以最大项必须减一才是真实的
    }

    
protected void Button5_Click(object sender, EventArgs e)
    
{//上一条
        ListBox6.SelectedIndex = ListBox6.SelectedIndex - 1;//用当前被选中的索引去减一
    }

    
protected void Button6_Click(object sender, EventArgs e)
    
{//下一条
        ListBox6.SelectedIndex = ListBox6.SelectedIndex + 1;//用当前被选中的索引去加一
    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值