0

I have a LinkLabel which is set up to receive some URLs that result from making a selection in a ComboBox. What I'm trying to accomplish is for the user to select a state from my combo, and then be able to click the individual links that appear in link label.

Having my links in array, what I'm getting is the array displays the links as "one whole" string, and I want them to separate links. Here's what I have:

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

I'll have about 3 other arrStateName type arrays, so my SelectedIndex will span from [0] to [3], and each array will contain 3 URL links.

So where am I going wrong here? If anyone can give me a boost in the right direction I would appreciate it. Some suggested using Dictionary data type, but I'm new to and when I tried test it out, I got frustrated because it doesn't seem to produce the results I want. Using the TKey and TValue throws me off, and I can never get all of my links to display in the box. I used Integer for my keys, and String for my values (links), but couldn't make it work. Some much needed guidance would be appreciated. Is what I'm trying to do possible, or should I be using some other control types?

DesignerMind
  • 410
  • 4
  • 8
  • 29

2 Answers2

1

Make a class object:

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

Load some statesLinks into a 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

Get the links from the selection:

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
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
0

Add a RichTextBox and set Detect Urls = true, BorderStyle = None, Backcolor = color of form, if it is on form. The size should be large enough to hold the url's. Then

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

In

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

valter