【问题标题】:VB.NET listbox item switchVB.NET 列表框项切换
【发布时间】:2012-10-06 13:03:48
【问题描述】:

我正在处理这个项目,我在列表框中有 IP 地址,并且我已经获得了一种将列表框项目传输到文本框的方法,但我需要通过按钮更改 IP 地址。

比如我有这三个IP地址:

  • 123.456.78
  • 891.23.45.6
  • 789.123.12

文本框中的当前 IP 地址是 123.456.78,但是当我单击一个按钮时,它也更改了第二行,即 891.23.45.6,它只能是文本框中的那个(不像 123.456.78 被推到边)。

这就是我需要的代码。

全部放在一个按钮下,比如-

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '*Grabbing our proxy text from our online server (this way we can keep updating our proxies).
    Dim Str As System.IO.Stream
    Dim srRead As System.IO.StreamReader
    Try
        ' make a Web request
        Dim req As System.Net.WebRequest = System.Net.WebRequest.Create("https://dl.dropbox.com/somethingOrOther")
        Dim resp As System.Net.WebResponse = req.GetResponse
        Str = resp.GetResponseStream
        srRead = New System.IO.StreamReader(Str)
        ' read all the text 
        TextBox2.Text = srRead.ReadToEnd
    Catch ex As Exception
        TextBox2.Text = "Unable to download content"
    Finally
        '  Close Stream and StreamReader when done
        srRead.Close()
        Str.Close()
    End Try

    ' Assign string to reference.
    Dim value1 As String = TextBox2.Text


    ' Replace word with another word.
    Dim value2 As String = value1.Replace("<br>", vbNewLine)
    TextBox2.Text = value2
    ListBox1.Items.Add(TextBox2.Text)

    '*Now , we're taking our fresh proxies in the textbox and moving them into our listbox.

    ListBox1.Items.AddRange(TextBox2.Text.Split(vbNewLine))
End Sub

谁能帮帮我?

【问题讨论】:

  • 听起来你需要一个事件处理程序来改变
  • 你能发布一些你的代码吗?
  • 他的意思是你能把它贴在你的问题中。
  • 他的意思是“你能否以可编辑的文本格式发布它,将任何敏感信息更改为通用信息(例如将实际的 Dropbox url 更改为更温和的内容,如“dl.dropbox.xom/somedropboxurl"”)而不是screengrab,这样你就可以向我们展示你的整个按钮处理程序而不是它的一部分?”但我想我会尽我所能。:)
  • 啊,我只是没有一段直接相关的代码。

标签: .net vb.net


【解决方案1】:

试试ListBox.SelectedIndex 属性。

如果您需要检查特定索引处的值是什么,可以使用ListBox.Items 属性。

如果您需要遍历列表框(例如,查找特定值),则可以使用ListBox.Items.Count 属性:

For l_currentIndex As Integer = 0 to MyListBox.Items.Count - 1
    If CStr(MyListBox.Items(l_currentIndex)) = "MyExpectedValue" Then
        MyListBox.SelectedInex = l_currentIndex
        Exit ' Exit For Loop
    End If
Next

【讨论】:

    【解决方案2】:

    这段代码:

    'Assign string to reference.
    Dim value1 As String = TextBox2.Text
    
    
    'Replace word with another word.
    Dim value2 As String = value1.Replace("<br>", vbNewLine)
    TextBox2.Text = value2
    ListBox1.Items.Add(TextBox2.Text)
    
    '*Now , we're taking out fresh proxies in the textbox and moving them into our listbox
    
    ListBox1.Items.AddRange(TextBox2.Text.Split(vbNewLine))
    

    在功能上等同于这段代码:

    TextBox2.Text = TextBox2.Text.Replace("<br>", vbNewLine)
    ListBox1.Items.Add(TextBox2.Text)
    ListBox1.Items.AddRange(TextBox2.Text.Split(vbNewLine))
    

    ListBox1.Items.AddRange(TextBox2.Text.Split(vbNewLine)) 行不会做你想做的事,因为你已经用&lt;br&gt; 元素替换了所有vbNewLines(顺便说一句,这些元素对于XHTML 格式不正确......他们应该改为&lt;br /&gt;...)。

    如果您可以发布整个 button.click 处理程序,用通用信息代替敏感内容(例如您的 Dropbox 网址),我们或许可以为您提供更好的帮助。

    更新:

    假设您的表单标记如下所示:

    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Single" AutoPostBack="true"></asp:ListBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Get Proxies" />
    

    为您的处理程序尝试这些:

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim req As System.Net.WebRequest = Nothing
        Dim resp As System.Net.WebResponse = Nothing
        Dim Str As System.IO.Stream = Nothing
        Dim srRead As System.IO.StreamReader = Nothing
        Dim responseText As String = String.Empty
        Dim proxies() As String = Nothing
        Dim list As ArrayList = Nothing
        Dim ipAddress As String = String.Empty
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '*Grabbing our proxy text from our online server (this way we can keep updating our proxies).
        Try
            ' make a Web request
            req = System.Net.WebRequest.Create("https://dl.dropbox.com/somethingOrOther")
            resp = req.GetResponse
            Str = resp.GetResponseStream
            srRead = New System.IO.StreamReader(Str)
            ' read all the text 
            responseText = srRead.ReadToEnd
        Catch ex As Exception
            responseText = "Unable to download content"
        Finally
            ' Close Stream and StreamReader when done
            srRead.Close()
            Str.Close()
        End Try
    
        '*Now , we're taking our fresh proxies in the textbox and moving them into our listbox.
        proxies = responseText.Split(vbNewLine)
        For Each ipAddress In proxies
            list.Add(New ListItem(ipAddress))
        Next
    
        ListBox1.Items.AddRange(list.ToArray(GetType(ListItem)))
    End Sub
    
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex > -1 Then
            TextBox2.Text = ListBox1.SelectedValue
        End If
    End Sub
    

    【讨论】:

    • 但我需要它来滚动列表框项目。
    • 请澄清您的意思。 ListBox 应该已经是可滚动的(取决于一次显示的项目数)。
    猜你喜欢
    • 1970-01-01
    • 2013-10-23
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多