-3

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

有人可以帮我吗?

4

2 回答 2

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
于 2012-10-06T13:25:08.743 回答
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))不会执行您想要的操作,因为您已经将所有vbNewLines替换为<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
于 2012-10-06T13:35:02.977 回答