◎以上程式範例AspNet07.aspx,如在頁框下不能操作,請開新視窗操作
◎如果有問題歡迎您提出,dnowba很需要有人和我一起討論
Partial Class AspNet07
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
' ===== 關於ListBox中子項目產生的方式 =====
' 動態產生ListBox的,會有「重建子項目」的問題,
' 用DataSource的方式繫結, 每次要需重新databind,
' 用DataTable的方法應該是最好的
' 製作二個ListBox並增加子項目
ListBox1.Items.Add("左A")
ListBox1.Items.Add("左B")
ListBox1.Items.Add("左C")
ListBox2.Items.Add("右A")
ListBox2.Items.Add("右B")
ListBox2.Items.Add("右C")
' 製作二個Button
Button1.Text = "向右移"
Button2.Text = "向左移"
' 製作一個Label
Label1.Text = ""
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' ===== 應用二個ListBox 控制項,左右搬移子選項(待修正)=====
' 用.Add() 和.Remove()二種方式來生成
' 目前只能移動最後一項而已,我想是因為for next的迴圈寫法不正確
' 另外如何「複選」可能也要等解決這個問題
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'For i As Integer = 0 To (ListBox1.Items.Count - 1)
' If ListBox1.Items(i).Selected = True Then
' ' 邏輯上順序是(1)把所選的子選項移到右邊ListBox (2)把原來ListBox的子項目刪除 (3)寫下移動成功的訊息
' ' 如下面的寫法,不過因為移動成功的訊息有綁著 item(i) 所以執行時會有「索引錯誤」問題,所以要修正寫法
' ' ListBox2.Items.Add(ListBox1.Items(i).Text)
' ' ListBox1.Items.Remove(ListBox1.Items(i).Text)
' ' Label1.Text = Label1.Text & ListBox1.Items(i).Text & "移動成功!"
' ListBox2.Items.Add(ListBox1.Items(i).Text)
' Label1.Text = Label1.Text & ListBox1.Items(i).Text & "移動成功!"
' ListBox1.Items.Remove(ListBox1.Items(i).Text)
' Else
' ' 迴圈最常犯的毛病(我自已),還加了無言的判別式,這樣子的判別最後「else」一定會成立
' 'Label1.Text = "你未點選任何一個子選項"
' End If
'Next
Dim a As Integer = 0
For i As Integer = 0 To (ListBox1.Items.Count - 1)
If ListBox1.Items(i).Selected = True Then
ListBox2.Items.Add(ListBox1.Items(i).Text)
a = a + 1
ListBox1.Items.Remove(ListBox1.Items(i).Text)
End If
Next
If a = 0 Then
' 用label可帶css語法的特性,來區別提示語的顏色
Label1.Text = "<font color=red>警告!您尚未點選任何一項子項目</font>"
Else
Label1.Text = "<font color=blue>移動成功</font>"
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
For j As Integer = 0 To ListBox2.Items.Count - 1
If ListBox2.Items(j).Selected = True Then
ListBox1.Items.Add(ListBox2.Items(j).Text)
Label1.Text = Label1.Text & ListBox2.Items(j).Text & "移動成功!"
ListBox2.Items.Remove(ListBox2.Items(j).Text)
Else
Label1.Text = "你未點選任何一個子選項"
End If
Next
End Sub
End Class
沒有留言:
張貼留言