兩個 listbox 左右互移
大家好 小弟來問個問題
程式碼我已經 打好了 在下面 不過會出錯 大家幫我看要如何改才能正確
為了怕大家看不懂 我先把題目打出來一下
我的作業是
----
1.請設計一Dropdownlist內容包含:前菜、主餐、甜點、飲料等4種類別。
2.請設計前菜、主餐、甜點、飲料等四類別,各類別中各有三種選擇,
例如前菜有:生菜沙拉、水果沙拉、雞肉沙拉等三種選擇(各類別內容由同學自訂),當使用者更換dropdownlist之項目時,請自動更新選擇清單之項目。
3.請設計兩個listbox,一為選擇清單,一為結果清單。使用者可以由選擇清單中點選所要選擇的項目
4.畫面中-> <- 為當使用者由listbox中選擇所要的項目後,由->將資料轉到結果清單中(此時,選擇清單中應清除該項目)
5.使用者可以點選結果清單中項目,由<- 清除使用者選擇之項目(此時選擇清單中應加入此項目)
-----
問題點在於 我偵錯時 我把左邊其中一個項目移到右邊去 按下拉式選單選其他選項時 會出現錯誤
我想是因為我用了ListBox1.Items.Remove()的關係 不知道要怎麼改才行呢
請各位幫幫忙吧ˊˋ
Public Partial Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged
If DropDownList1.Items(0).Selected Then
ListBox1.Items(0).Text = "水果沙拉"
ListBox1.Items(1).Text = "生菜沙拉"
ListBox1.Items(2).Text = "雞肉沙拉"
End If
If DropDownList1.Items(1).Selected Then
ListBox1.Items(0).Text = "牛排"
ListBox1.Items(1).Text = "羊排"
ListBox1.Items(2).Text = "雞排"
End If
If DropDownList1.Items(2).Selected Then
ListBox1.Items(0).Text = "冰淇淋"
ListBox1.Items(1).Text = "果凍"
ListBox1.Items(2).Text = "蛋糕"
End If
If DropDownList1.Items(3).Selected Then
ListBox1.Items(0).Text = "汽水"
ListBox1.Items(1).Text = "咖啡"
ListBox1.Items(2).Text = "茶類"
End If
End Sub
Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If ListBox1.Items(0).Selected Then
ListBox2.Items.Add(ListBox1.Items(0).Text)
ListBox1.Items.Remove(ListBox1.SelectedItem)
ElseIf ListBox1.Items(1).Selected Then
ListBox2.Items.Add(ListBox1.Items(1).Text)
ListBox1.Items.Remove(ListBox1.SelectedItem)
ElseIf ListBox1.Items(2).Selected Then
ListBox2.Items.Add(ListBox1.Items(2).Text)
ListBox1.Items.Remove(ListBox1.SelectedItem)
End If
End Sub
Protected Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox2.SelectedIndexChanged
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
If ListBox2.Items(0).Selected Then
ListBox1.Items.Add(ListBox2.Items(0).Text)
ListBox2.Items.Remove(ListBox2.SelectedItem)
ElseIf ListBox2.Items(1).Selected Then
ListBox1.Items.Add(ListBox2.Items(1).Text)
ListBox2.Items.Remove(ListBox2.SelectedItem)
ElseIf ListBox2.Items(2).Selected Then
ListBox1.Items.Add(ListBox2.Items(2).Text)
ListBox2.Items.Remove(ListBox2.SelectedItem)
End If
End Sub
End Class --------------------编程问答-------------------- 一个删除、一个添加!! --------------------编程问答-------------------- 客户端js实现比较好 --------------------编程问答-------------------- DropDownList1_SelectedIndexChanged里
ListBox1.Items.Clear()
再绑定数据
string listBox1 = this.ListBox1.SelectedItem.Text;
this.ListBox2.Items.Add(listBox1);
this.ListBox1.Items.Remove(listBox1);
<asp:ListBox ID="lst_left" runat="server" SelectionMode="Multiple" Height="203px"
Width="130px"></asp:ListBox>
<asp:ListBox ID="lst_center" runat="server" SelectionMode="Multiple" Height="203px" Width="130px"></asp:ListBox>
<input type="button" id="btn_leftToCenter" value="右移" onclick="MoveOption(document.getElementById('lst_left'),document.getElementById('lst_center'))" />
<input type="button" id="btn_centerToLeft" value="左移" onclick="MoveOption(document.getElementById('lst_center'),document.getElementById('lst_left'))" />
function MoveOption(fromSel,toSel)
{
var fromOp=fromSel.options;
var toOp=toSel.options;
var selectedFlag=false;
for(var i=0;i<fromOp.length;i++)
{
if(fromOp[i].selected)
{
selectedFlag=true;
var selectedOp=document.createElement("option");
selectedOp.text=fromOp[i].text;
selectedOp.value=fromOp[i].value;
toSelLength=toOp.length;
toSel.options.add(selectedOp,toSelLength);
fromSel.options.remove(i);
i--;
}
}
if(!selectedFlag)
{
alert("请选择一项!");
return;
}
} --------------------编程问答--------------------
請問這是什麼語法 我軟體不能用 --------------------编程问答-------------------- 哥们上面的你要分开写 --------------------编程问答-------------------- 完全错误的答案是:
用for循环从ListBox的第1个也就是index=0的开始移动.
完全正确的答案是:
用for循环从最后一个开始移动
int count=ListBox1.Items.Count;
for(int i=count;i>0;i--)
{
.....
}
这样做才不会出错!!
请相信我完全正确的答案.
polomeng@gmail.com --------------------编程问答-------------------- winform放俩个listbox1,listbox2
四个button,
设置listbox1的selectedmode为multible extended
// just move one content from listbox1 to listbox2
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
listBox2.Items.Add(listBox1.SelectedItems[listBox1.SelectedIndex].ToString());
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}
// just move one content from listbox2 to listbox1
private void button3_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex != -1)
{
listBox1.Items.Add(listBox2.SelectedItems[listBox2.SelectedIndex].ToString());
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
}
}
//move multible content from listbox1 to listbox2
private void button2_Click(object sender, EventArgs e)
{
List<object> oblist = new List<object>();
foreach (object ob in listBox1.SelectedItems)
{
listBox2.Items.Add(ob);
oblist.Add(ob);
}
foreach (object ob in oblist)
{
listBox1.Items.Remove(ob);
}
}
//move multible content from listbox2 to listbox1
private void button4_Click(object sender, EventArgs e)
{
List<object> oblist = new List<object>();
foreach (object ob in listBox2.SelectedItems)
{
listBox1.Items.Add(ob);
oblist.Add(ob);
}
foreach (object ob in oblist)
{
listBox2.Items.Remove(ob);
}
} --------------------编程问答-------------------- 请用简体字! --------------------编程问答--------------------
呵呵,学习,对称嘛,一边一个LISTBOX,两个BUTTON, --------------------编程问答-------------------- VB没有用过,不过我想用C#不难实现这个的 --------------------编程问答-------------------- 这个应该是前台的东西的,JS --------------------编程问答-------------------- 我也没有用过VB 帮顶 --------------------编程问答-------------------- C# JAVASCRIPT语言
<asp:ImageButton ID="ibtnRight" runat="server" ImageUrl="../../../IMG/RightButton.gif" TabIndex="7" OnClientClick="fun_left();return false" /> (从左选向右)
function fun_left()
{
var arrCID = new Array();
var arrColName = new Array();
var arrState = new Array();
var var_sel = false;
var obj = document.getElementById("lstGroupLeft");
var p = document.getElementById("lstGroupRight");
var j=0;
for(var i = 0 ; i<obj.options.length ; i++)
{
if(obj.options[i].selected == true)
{
if(p.options.length > 0)
{
for(var h = 0 ; h<p.options.length ; h++)
{
if(obj.options[i].value == p.options[h].value && obj.options[i].text == p.options[h].text)
{
var_sel = true;
}
}
if(!var_sel)
{
arrCID[j] = obj.options[i].value;
arrColName[j] = obj.options[i].text;
arrState[j] = i;
j++;
}
}
else
{
arrCID[j] = obj.options[i].value;
arrColName[j] = obj.options[i].text;
arrState[j] = i;
j++;
}
}
}
for(var k=0; k<arrCID.length; k++)
{
var opt = new Option();
opt.value = arrCID[k];
opt.text = arrColName[k];
p.options[p.options.length] = opt;
}
} --------------------编程问答-------------------- <asp:ImageButton ID="ibtnLeft" runat="server" ImageUrl="../../../IMG/LeftButton.gif" TabIndex="8" OnClientClick="fun_right();return false" />(从右选向左)
function fun_right()
{
var arrCID = new Array();
var arrColName = new Array();
var arrState = new Array();
var j=0;
var p = document.getElementById("lstGroupRight");
for(var i=0; i<p.options.length; i++)
{
if(p.options[i].selected == true)
{
arrCID[j] = p.options[i].value;
arrColName[j] = p.options[i].text;
arrState[j] = i;
j++;
}
}
for(var n=arrState.length-1; n>=0; n--)
{
p.options.remove(arrState[n]);
}
}
补充:.NET技术 , ASP.NET