使用VB 2008获取选定的列表框项目值以显示在另一个列表框中 [英] Getting selected listbox items values to display in another listbox using vb 2008

查看:247
本文介绍了使用VB 2008获取选定的列表框项目值以显示在另一个列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个列表框的表单.在这里,listbox1填充有演员和女演员的名字.如果从列表框1中选择了一个名称,则列表框2应该显示涉及该名称的电影的标题.如果选择了其他名称,则listbox2将显示涉及2个名称的电影的标题.

I have a form with a 2 listboxes. Here, listbox1 is populated with names of actors and actresses. If a name is selected from listbox1, listbox2 should show the title(s) of movie(s) where that name is involved. If another name is selected, listbox2 will show title(s) of movie(s) that 2 name is involved.

Call Connect()
    With Me
        STRSQL = "select mTitle from selectmovie where cName = '" & lstNames.SelectedItem & "'"
        Try
            myCmd.Connection = myConn
            myCmd.CommandText = STRSQL
            myReader = myCmd.ExecuteReader
            If (myReader.Read()) Then
                myReader.Close()
                myAdptr.SelectCommand = myCmd
                myAdptr.Fill(myDataTable)
                lstTitle.DisplayMember = "mTitle"
                lstTitle.ValueMember = "mTitle"

                If myDataTable.Rows.Count > 0 Then
                    For i As Integer = 0 To myDataTable.Rows.Count - 1
                        lstTitle.Items.Add(myDataTable.Rows(i)("mTitle"))
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End With

没有错误.当我选择1个项目时,结果是正确的,但它留有很多空间..此处是我的表单的屏幕截图:

There's no error. When I select 1 item the result is correct but it leaves many space..here the screen shot of my form: http://www.flickr.com/photos/92925726@N06/8445945758/in/photostream/

当我选择actor3时,输出会变得更糟: http://www.flickr.com/photos/92925726@N06/8445945724/in/photostream/

The output becomes worse when I selected actor3: http://www.flickr.com/photos/92925726@N06/8445945724/in/photostream/

推荐答案

您的主要问题似乎是在重新加载新选择之前,您没有清除lstTitle控件.因此,每次选择新名称时,都会将该名称的所有标题添加到已加载的现有标题列表中.另外,与其使用整数来迭代所有索引,不如仅使用For Each循环会更容易:

Your main problem seems to be that you do not clear your lstTitle control before re-loading it with the new selection. Therefore, each time you select a new name, it will add all the titles for that name to the existing list of titles that are already loaded. Also, instead of using an integer to iterate all the indexes, it is easier to just use a For Each loop:

lstTitle.Items.Clear()
For Each row As DataRow In myDataTable.Rows
    lstTitle.Items.Add(row("mTitle"))
Next

但是,我还必须提到,您确实还应该在查询中使用参数,而不是像这样动态构建SQL语句,例如:

However, I must also mention that you really should also be using a parameter in your query rather than dynamically building the SQL statement like that, for instance:

myCmd.CommandText = "select mTitle from selectmovie where cName = @name"
myCmd.Parameters.AddWithValue("name", lstNames.SelectedItem)

要选择所有涉及多个选定演员的电影,您需要为每个演员在where子句中添加一个附加条件,例如:

To select all the movies where all of the multiple selected actors are involved, you would need to add an additional condition to your where clause for each actor, for instance:

Dim builder As New StringBuilder()
builder.Append("select distinct mTitle from selectmovie where ")
For i As Integer = 0 to lstNames.SelectedItems.Count - 1
    Dim parameterName As String = "@name" & i.ToString()
    If i <> 0 Then
        builder.Append("and ")
    End If
    builder.Append(parameterName)
    builder.Append(" in (select cName from selectmovie where mTitle = m.mTitle) ")
    myCmd.Parameters.AddWithValue(parameterName, lstNames.SelectedItems(i))
Next
myCmd.CommandText = builder.ToString()

这篇关于使用VB 2008获取选定的列表框项目值以显示在另一个列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆