Hello sir,
I am having 3 linklabels on the form and 1 button.
Consider these are the linklabels.
a
b
c

When "a" is clicked, button is visible on the form and when i click the button HTML output is displayed (i had already attached HTML pages to my project and also i know how to display output for HTML pages in the project) .

For a,b,c i am having different HTML pages.

What i actually need is?
I want to use only one button. But when i click a,b,c linklabels, the appropriate HTML output must be displayed.

Recommended Answers

All 16 Replies

Use the same handler for all link labels and a global variable to hold information which link label was clicked.

' Save last clicked link label
Private m_Link As String = "a"

Private Sub LinkHandler(ByVal sender As Object, ByVal e As System.EventArgs) Handles a.Click, b.Click, c.Click
  ' Trap all LinkLabel clicks
  m_Link = CType(sender, LinkLabel).Name

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  ' Show web page
  Select Case m_Link
    Case "a"
      ' Show a-link page
    Case "b"
      ' Show b-link page
    Case "c"
      ' Show c-link page
  End Select

End Sub

Use the same handler for all link labels and a global variable to hold information which link label was clicked.

' Save last clicked link label
Private m_Link As String = "a"

Private Sub LinkHandler(ByVal sender As Object, ByVal e As System.EventArgs) Handles a.Click, b.Click, c.Click
  ' Trap all LinkLabel clicks
  m_Link = CType(sender, LinkLabel).Name

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  ' Show web page
  Select Case m_Link
    Case "a"
      ' Show a-link page
    Case "b"
      ' Show b-link page
    Case "c"
      ' Show c-link page
  End Select

End Sub

Thankyou sir,
But i am having 1000 of linklabels in my project, and so its difficult to give cases for every linklabels click.
Is there any other easy way to do this?

With 1000 link labels you are creating them dynamically, right?

' Array for link label controls
Private m_LinkControls() As Control
' Index for selected link label
Private m_LinkIndex As Integer

' Call this sub at the start up to create controls
Private Sub CreateLinks()
  ' Create 1000 link labels
  Dim TempLink As LinkLabel
  Dim i As Integer

  ReDim m_LinkControls(999)
  For i = 0 To 999
    TempLink = New LinkLabel
    ' Set TempLink.Location (in the form)
    ' Set TempLink.Text (link's caption text)
    ' Now, use TempLink.Tag to hold information. In this case it holds it's index (0-999)
    TempLink.Tag = i
    ' Add handler
    AddHandler TempLink.Click, AddressOf LinkHandler
    m_LinkControls(i) = TempLink
  Next i

End Sub

Private Sub LinkHandler(ByVal sender As Object, ByVal e As System.EventArgs)
  ' Trap all LinkLabel clicks
  ' Cast Tag object back to Integer
  m_LinkIndex = CInt(CType(sender, LinkLabel).Tag)

End Sub
Since you have 1000 links you probably have web pages in an array or similar indexed collection and now get the right web page with m_LinkIndex
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  ' Show web page

  ' Use m_LinkIndex as an index to your web pages
  
End Sub

Hello sir,
With your previous coding i changed the coding like this and that works.

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Show web page
    Select Case m_Link
        Case "a"
            webbrowser1.navigate = path.combine(application.StartupPath,"\Source\c1.html"))
        Case "b"
            webbrowser1.navigate = path.combine(application.StartupPath,"\Source\c2.html"))
        Case "c"
            webbrowser1.navigate = path.combine(application.StartupPath,"\Source\c3.html"))
    End Select

But in this coding how can i do like that?

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Show web page
     webbrowser1.navigate = path.combine(application.StartupPath,"\Source\c2.html"))
        ' Use m_LinkIndex as an index to your web pages

    End Sub

If you're dealing with 1000 files, I suggest putting the file names in an array

' An array for file names
Private m_WebPages() As String

' Fill array with file names
Private Sub FillPageLinks()
  '
  Dim i As Integer

  ReDim m_WebPages(999)
  ' Now read links from a text file, database or whatever
  ' or suppose your naming is following c1.html, c2.html, ...
  For i = 0 To 999
    m_WebPages(i) = "\Source\c" & (i + 1).ToString & ".html"
  Next i

End Sub

then the m_LinkIndex is also the index to file array

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  ' Show web page

  ' Use m_LinkIndex as an index to your web pages
  webbrowser1.navigate = path.combine(application.StartupPath, m_WebPages(m_LinkIndex)))
  
End Sub

Hello Sir,
I used the coding as below, But whatever linklabel i Clicked it only shows the first link in webbrowser (ie C1)

Public Class Form1
    ' Array for link label controls
    Private m_LinkControls() As Control
    ' Index for selected link label
    Private m_LinkIndex As Integer

    ' Call this sub at the start up to create controls
    Private Sub CreateLinks()
        ' Create 1000 link labels
        Dim TempLink As LinkLabel
        Dim i As Integer

        ReDim m_LinkControls(2)
        For i = 0 To 2
            TempLink = New LinkLabel
            ' Set TempLink.Location (in the form)
            ' Set TempLink.Text (link's caption text)
            ' Now, use TempLink.Tag to hold information. In this case it holds it's index (0-999)
            TempLink.Tag = i
            ' Add handler
            AddHandler TempLink.Click, AddressOf LinkHandler
            m_LinkControls(i) = TempLink
        Next i

    End Sub

    Private Sub LinkHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Trap all LinkLabel clicks
        ' Cast Tag object back to Integer
        m_LinkIndex = CInt(CType(sender, LinkLabel).Tag)

    End Sub
    ' An array for file names
    Private m_WebPages() As String

    ' Fill array with file names
    Private Sub FillPageLinks()
        '
        Dim i As Integer

        ReDim m_WebPages(2)
        ' Now read links from a text file, database or whatever
        ' or suppose your naming is following c1.html, c2.html, ...
        For i = 0 To 2
            m_WebPages(i) = "\Check\c" & (i + 1).ToString & ".html"
        Next i

    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Show web page

        ' Use m_LinkIndex as an index to your web pages
        WebBrowser1.Navigate(Application.StartupPath & m_WebPages(m_LinkIndex))

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call CreateLinks()
        Call FillPageLinks()
    End Sub
End Class

Did you get *any* dynamically created link label to be visible in your form? I bet not...

If you create controls dynamically, you have to set all the needed properties. Besides, controls have to be added to Form, not to array.

Here's a (hopefully) better example of creating controls dynamically:

Private Sub CreateLinks()
  ' Create 1000 link labels
  Dim TempLink As LinkLabel
  Dim i As Integer

  ReDim m_LinkControls(999)
  For i = 0 To 2 ' Create only 3 links for demonstration purpose
    TempLink = New LinkLabel
    ' Set TempLink.Location (in the form)
    TempLink.Location = New Point(i * 80 + 10, 60)
    TempLink.Name = "Link" & i.ToString
    ' Set TempLink.Text (link's caption text)
    TempLink.Text = "Link" & i.ToString
    TempLink.Width = 40
    TempLink.Visible = True
    ' Now, use TempLink.Tag to hold information. In this case it hold its index (0-999)
    TempLink.Tag = i
    ' Add handler
    AddHandler TempLink.Click, AddressOf LinkHandler
    'm_LinkControls(i) = TempLink
    ' Add link labels to this form's controls!
    Me.Controls.Add(TempLink)
  Next i

End Sub

m_LinkControls() array was a solution when you had a few controls created with designer, not by code, like your original case was.

TempLink.Location = New Point(i * 80 + 10, 60)
TempLink.Text = "Link" & i.ToString

Is it possible to view the links vertically instead of horizontally?

And also i would like to give different captions for links instead of link0,link1,link2.
Is it possible?

The answer to both questions is yes.

First, I suggest reading some VB.NET tutorials, you'll find them online too.

Second, VB.NET IDE is a "smart" editor i.e. it shows procedure's "prototype" while you type. This way you know what parameters you should give to procedures and properties.

I'm here to *help*, and I'm willing to help with *real* programming problems. I'm not here to write programs for anyone.

So, start reading VB.NET tutorials and learn first basic programming skills. After that, if you encounter problems with programming, I'll be glad to help you :)

The answer to both questions is yes.

First, I suggest reading some VB.NET tutorials, you'll find them online too.

Second, VB.NET IDE is a "smart" editor i.e. it shows procedure's "prototype" while you type. This way you know what parameters you should give to procedures and properties.

I'm here to *help*, and I'm willing to help with *real* programming problems. I'm not here to write programs for anyone.

So, start reading VB.NET tutorials and learn first basic programming skills. After that, if you encounter problems with programming, I'll be glad to help you :)

Thankyou Teme, i will find them and read it.


And also i would like to give different captions for links instead of link0,link1,link2.
Is it possible?

Hello sir,
I find one way to do that.

TempLink.Text = (Me.Controls("textbox" & i + 1).Text).ToString

But here i have to add 1000 textboxes on form and name it.
Is there any other possible easy way?

TempLink.Text = (Me.Controls("textbox" & i + 1).Text).ToString

works, but do you really know what it does :S

Is there any other possible easy way?

Since the Text property is declared as a string, you can assign to it whatever you want as long as it is a string. TempLink.Text = "a" is the simplest example and that's as easy as it can ever be.

What kind of text's should those labels display? Show some concrete examples.

works, but do you really know what it does :S


Since the Text property is declared as a string, you can assign to it whatever you want as long as it is a string. TempLink.Text = "a" is the simplest example and that's as easy as it can ever be.

What kind of text's should those labels display? Show some concrete examples.

sir,
I am simply asking instead of using 1000 textboxes, is there any other way?

I am simply asking instead of using 1000 textboxes, is there any other way?

Text boxes? You mean link labels, right?

Ok, I wouldn't put 1000 controls in a form, no matter what kind of controls they are.

Can you explain what are you *exactly* trying to achieve with your proggie?

If you really try to have a prog with which a user can select from 1000 URLs to open, I have used a combination of a treeview and listview control. Treeview control had URL categories and listview control showed the URLs from the selected category. When a user selected a listview item (URL) it was opened with the browser. But this was my solution for my prog, not sure what kind of prog you're doing.

Text boxes? You mean link labels, right?

Ok, I wouldn't put 1000 controls in a form, no matter what kind of controls they are.

Can you explain what are you *exactly* trying to achieve with your proggie?

If you really try to have a prog with which a user can select from 1000 URLs to open, I have used a combination of a treeview and listview control. Treeview control had URL categories and listview control showed the URLs from the selected category. When a user selected a listview item (URL) it was opened with the browser. But this was my solution for my prog, not sure what kind of prog you're doing.

TempLink.Text = (Me.Controls("textbox" & i + 1).Text).ToString

What i want is, actually the above code works in an array.
So consider i am having 3 textboxes on the form.
Textbox1,Textbox2,Textbox3
So that code gets value from every Textboxes to templink.text, which then add captions to linklables from that corresponding text in textboxes.
Think i have to give captions for 1000 linklables, so i have to place 1000 textboxes on form.
Is there anyother way to make it easier, instead of placing 1000 textboxes.
Hope you get me

You want the user to give the names for the links?

If this is the case, use a loop. In each loop create a link with the code given before, and prompt the user for the name. Something like NewLink.Text = InputBox("Give a link name") . Not a pretty solution, but an easy one to implement.

You may also give each link some "default name", like "Link1" etc., and provide the user a way to rename links.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.