• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
我的学习笔记
   首页       联系   管理    订阅  订阅

ListBox--列表框

SelectionMode 组件中条目的选择类型:单选,多选
Row 此组件显示多少行
Selected 检测条目是否被选中
SelectedItem 返回类型是ListItem,获得组件中被选择的条目
Count 组件中条目的总数
SelectIndex 组件中被选择条目的索引值
Items 泛指组件中所有的条目,每个条目的类型都是ListItem

1、动态添加列表框

 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 Button2_Click(object sender, EventArgs e)
    
{
        
if (ListBox2.Items.Count > 0)
        
{
            
            ListBox2.Items.Clear();
            Button2.Enabled 
= false;
        }

   
    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
if (ListBox2.Items.Count > 0)
        
{
            ListBox2.Items.Remove(ListBox2.SelectedItem);  
//移除选定项
        }

        
else
        
{
            Button1.Enabled 
= false;
        }

    }
2、ListBox两级联动

 protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
switch (ListBox3.SelectedValue)
        
{ 
            
case "fz":
                ListBox4.Items.Clear();
                ListBox4.Items.Add(
"鼓山");
                ListBox4.Items.Add(
"西湖");
                ListBox4.Items.Insert(
1,"旗山");
                
break;
            
case "sh":
                ListBox4.Items.Clear();
                ListBox4.Items.Add(
"东方明珠");
                ListBox4.Items.Add(
"浦东");
                
break;
            
case "hk":
                ListBox4.Items.Clear();
                ListBox4.Items.Add(
"维多利亚港");
                
                
break;
            
case "bj":
                ListBox4.Items.Clear();
                ListBox4.Items.Add(
"故宫");
                ListBox4.Items.Add(
"长城");
                
break;
        }
3、列表指定项位移与光标移动

protected void Button3_Click(object sender, EventArgs e)
    
{
        
if (((Button)sender).CommandName == "up" && ListBox5.SelectedIndex > 0 || ((Button)sender).CommandName == "down" && ListBox5.SelectedIndex < ListBox5.Items.Count - 1)
        
{
            
int index;
            
if (((Button)sender).CommandName == "up")
            
{
                index 
= -1;
            }

            
else
            
{
                index 
= 1;
            }

            ListItem lt 
= new ListItem(ListBox5.SelectedItem.Text,ListBox5.SelectedItem.Value);
            ListBox5.Items[ListBox5.SelectedIndex].Text 
= ListBox5.Items[ListBox5.SelectedIndex + index].Text;
            ListBox5.Items[ListBox5.SelectedIndex].Value 
= ListBox5.Items[ListBox5.SelectedIndex + index].Value;
            ListBox5.Items[ListBox5.SelectedIndex 
+ index].Text = lt.Text;
            ListBox5.Items[ListBox5.SelectedIndex 
+ index].Value = lt.Value;
            ListBox5.SelectedIndex 
= ListBox5.SelectedIndex + index;
        }


    }

    
protected void Button5_Click(object sender, EventArgs e)
    
{
        ListBox5.SelectedIndex 
= 0;
    }

    
protected void Button6_Click(object sender, EventArgs e)
    
{
        ListBox5.SelectedIndex 
-= 1;

    }

    
protected void Button7_Click(object sender, EventArgs e)
    
{
        ListBox5.SelectedIndex 
+= 1;
    }

    
protected void Button8_Click(object sender, EventArgs e)
    
{
        ListBox5.SelectedIndex 
= ListBox5.Items.Count - 1;
    }

posted @ 2007-12-27 13:50  吴有鋆  阅读(272)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3