如何将数组项作为链接标签中的单独链接

时间:2014-09-19 17:57:23

标签: arrays vb.net dictionary combobox linklabel

我有LinkLabel设置为接收通过在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]延伸到[3],每个数组将包含3个URL链接。

那我在哪里错了?如果有人能给我一个正确的方向,我会很感激。有人建议使用Dictionary数据类型,但我是新手,当我尝试测试时,我感到沮丧,因为它似乎没有产生我想要的结果。使用TKeyTValue会让我失望,我永远无法将所有链接显示在框中。我将Integer用于我的密钥,String用于我的值(链接),但无法使其正常工作。一些非常需要的指导将不胜感激。我正在尝试做什么,或者我应该使用其他一些控件类型?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

添加 RichTextBox 并设置检测Urls = true BorderStyle =无 Backcolor =表单颜色,如果是在表格上。尺寸应足够大以容纳网址。然后

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

瓦尔特