0

我有一个LinkLabel设置为接收一些 URL,这些 URL 是在ComboBox. 我想要完成的是让用户从我的组合中选择一个状态,然后能够单击出现在链接标签中的各个链接。

将我的链接放在数组中,我得到的是数组将链接显示为“一个完整的”字符串,我希望它们分开链接。这是我所拥有的:

Public arrAlabama(2) As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Create array for Alabama and add items.

    arrAlabama(0) = "http://www.rolltide.com/"
    arrAlabama(1) = "http://www.crimsontidehoops.com/"
    arrAlabama(2) = "http://centralalabamapride.org/"

End Sub

Private Sub cboSelectState_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSelectState.SelectedIndexChanged
    ' Populate the link label.
    If cboSelectState.SelectedIndex = 0 Then
        lnklblLinkbox.Text = arrAlabama(0) _
                            & vbNewLine & arrAlabama(1) _
                            & vbNewLine & arrAlabama(2)
    End If

End Sub

我将有大约 3 个其他arrStateName类型的数组,所以我SelectedIndex将跨越[0]to [3],每个数组将包含 3 个 URL 链接。

那么我在哪里错了?如果有人可以在正确的方向上给我一个推动,我将不胜感激。有些人建议使用Dictionary数据类型,但我是新手,当我尝试测试它时,我很沮丧,因为它似乎没有产生我想要的结果。使用TKeyandTValue让我失望,我永远无法让我的所有链接都显示在框中。我用于Integer我的键和String我的值(链接),但无法使其工作。一些急需的指导将不胜感激。我正在尝试做的事情是否可行,或者我应该使用其他一些控制类型?

4

2 回答 2

1

创建一个类对象:

Public Class StateLinks
 Public Property State As String
 Public Property Links As New List(Of String)
 Public Overrides Function ToString() As String
   'tells the combobox what to display
   Return State 
 Public Sub New(state As String)
   Me.State = state
 End Sub
End Class

将一些 statesLinks 加载到List(OF T)

Private stateLinksList As New List(Of StateLinks)
Private Sub LoadMe() Handles Me.Load
  Dim coState As New StateLinks("Colorado")
  coState.Links.Add("some link")
  stateLinksList.Add(coState)
  ' continue adding then bind them
  cboSelectState.DataSource = stateLinksList
End Sub

从选择中获取链接:

Private cb_selectionChanged() Handles cboSelectState.SelectedIndexChanged
  Dim state = TryCast(cb.SelectedItem, StateLinks)
  If Not state Is Nothing
    For Each link As String In state.Links
      'each link now available
    Next
  End If
于 2014-09-19T18:21:26.737 回答
0

添加一个RichTextBox并设置Detect Urls = trueBorderStyle = NoneBackcolor = 表单颜色,如果它在 form 上。大小应该足够大以容纳网址。然后

Private Sub cboSelectState_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSelectState.SelectedIndexChanged
    'Populate RichTextBox1.
    If cboSelectState.SelectedIndex = 0 Then
        RichTextBox1.Text = arrAlabama(0) _
                        & vbNewLine & arrAlabama(1) _
                        & vbNewLine & arrAlabama(2)
    End If

End Sub

Private Sub RichTextBox1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkClickedEventArgs) Handles RichTextBox1.LinkClicked
    Dim txt As String = e.LinkText 'txt is the link you clicked
End Sub

瓦尔特

于 2014-09-19T21:12:37.720 回答