【文件属性】:
文件名称:拓展练习代码备注-web教程使用
文件大小:967KB
文件格式:PPT
更新时间:2024-05-16 03:19:33
web
拓展练习:代码备注
ListBox控件的应用 |
太平洋
大西洋
印度洋
北冰洋
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lbxSource.Items.Add("星期日");
lbxSource.Items.Add("星期一");
lbxSource.Items.Add("星期二");
lbxSource.Items.Add("星期三");
lbxSource.Items.Add("星期四");
lbxSource.Items.Add("星期五");
lbxSource.Items.Add("星期六");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int count = lbxSource.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem item = lbxSource.Items[index];
lbxSource.Items.Remove(item);
lbxDest.Items.Add(item);
}
index++;
}
protected void Button2_Click(object sender, EventArgs e)
{
//获取列表框的选项数
int count = lbxDest.Items.Count;
//循环从目的列表框中转移到源列表框中
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem Item = lbxDest.Items[index];
lbxDest.Items.Remove(Item);
lbxSource.Items.Add(Item);
}
//获取下一个选项的索引值
index++;
}
protected void Button3_Click(object sender, EventArgs e)
{
int count = lbxSource.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem Item = lbxSource.Items[index];
if (lbxSource.Items[index].Selected == true)
{
lbxSource.Items.Remove(Item);
lbxDest.Items.Add(Item);
index--;
}
index++;
}
}
protected void Button4_Click(object sender, EventArgs e)
{
//获取列表框的选项数
int count = lbxDest.Items.Count;
int index = 0;
//循环判断各个项的选中状态
for (int i = 0; i < count; i++)
{
ListItem Item = lbxDest.Items[index];
//如果选项为选中状态从目的列表框中删除并添加到源列表框中
if (lbxDest.Items[index].Selected == true)
{
lbxDest.Items.Remove(Item);
lbxSource.Items.Add(Item);
//将当前选项索引值减1
index--;
}
//获取下一个选项的索引值
index++;
}
}
protected void Button6_Click(object sender, EventArgs e)
{
int count = lbxSource.Items.Count;
if (lbxSource.SelectedIndex > 0 && lbxSource.SelectedIndex <= count - 1)
{
string name = lbxSource.SelectedItem.Text;
string value = lbxSource.SelectedItem.Value;
int index = lbxSource.SelectedIndex;
lbxSource.SelectedItem.Text = lbxSource.Items[index - 1].Text;
lbxSource.SelectedItem.Value = lbxSource.Items[index - 1].Value;
lbxSource.Items[index - 1].Text = name;
lbxSource.Items[index - 1].Value = value;
lbxSource.SelectedIndex--;
}
}
protected void Button5_Click(object sender, EventArgs e)
{
int count = lbxSource.Items.Count;
if (lbxSource.SelectedIndex >= 0 && lbxSource.SelectedIndex < count - 1)
{
string name = lbxSource.SelectedItem.Text;
string value = lbxSource.SelectedItem.Value;
int index = lbxSource.SelectedIndex;
lbxSource.SelectedItem.Text = lbxSource.Items[index + 1].Text;
lbxSource.SelectedItem.Value = lbxSource.Items[index + 1].Value;
lbxSource.Items[index + 1].Text = name;
lbxSource.Items[index + 1].Value = value;
lbxSource.SelectedIndex++;
}
}
protected void Button7_Click(object sender, EventArgs e)
{
if (lbxSource.SelectedIndex > 0 && lbxSource.SelectedIndex <= lbxSource.Items.Count - 1)
{
string name = lbxSource.SelectedItem.Text;
string value = lbxSource.SelectedItem.Value;
int index = lbxSource.SelectedIndex;
lbxSource.SelectedItem.Text = lbxSource.Items[index - 1].Text;
lbxSource.SelectedItem.Value = lbxSource.Items[index - 1].Value;
lbxSource.Items[index - 1].Text = name;
lbxSource.Items[index - 1].Value = value;
lbxSource.SelectedIndex--;
}
else
{
ListItem item = lbxSource.Items[0];
lbxSource.Items.Remove(item);
lbxSource.Items.Add(item);
}
}
protected void Button8_Click(object sender, EventArgs e)
{
if (lbxSource.SelectedIndex >= 0 && lbxSource.SelectedIndex < lbxSource.Items.Count - 1)
{
string name = lbxSource.SelectedItem.Text;
string value = lbxSource.SelectedItem.Value;
int index = lbxSource.SelectedIndex;
lbxSource.SelectedItem.Text = lbxSource.Items[index + 1].Text;
lbxSource.SelectedItem.Value = lbxSource.Items[index + 1].Value;
lbxSource.Items[index + 1].Text = name;
lbxSource.Items[index + 1].Value = value;
lbxSource.SelectedIndex++;
}
else
{
ListItem item = lbxSource.Items[lbxSource.Items.Count - 1];
lbxSource.Items.Remove(item);
lbxSource.Items.Insert(0, item);
}
}