VB.NET列表框项目切换

时间:2012-10-06 13:03:49

标签: .net vb.net

我正在开发这个项目,我在列表框中有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

有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

尝试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

答案 1 :(得分:0)

此代码:

'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))不会按照您的要求执行操作,因为您已使用vbNewLine元素替换了所有<br>(顺便提一下,这些元素是格式错误的XHTML ......它们应该是<br /> ...)。

如果您可以发布整个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