Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

pls help create array of linklabels to be added to form, and when click on linklabel, it opens the filename of the linklabel

Posted on 2004-10-05
5
Medium Priority
?
275 Views
Last Modified: 2010-04-23
hi, i have not before dynamically created arrays of controls.
could someone please help me with this, i have included all the code i have done so far.
the idea is that a dataset contains 10 entries.
for each entry , i want to dynamically add a linklabel to the form, with the text of the filename.
and when the user clicks on the filename, it must open.
thats it.
this will be a learning curve for me.
here is my code so far.
please just create a strongly typed dataset called dataset1
and add a element "FilesCreated"
with a row "FileName"
'----------------------------------------------------------------

Public Class Form1
    Inherits System.Windows.Forms.Form


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents HeadingLabel As System.Windows.Forms.Label
    Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel
    Friend WithEvents Dataset11 As DisplayFileNamesLink.Dataset1
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.HeadingLabel = New System.Windows.Forms.Label
        Me.LinkLabel1 = New System.Windows.Forms.LinkLabel
        Me.Dataset11 = New DisplayFileNamesLink.Dataset1
        CType(Me.Dataset11, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'HeadingLabel
        '
        Me.HeadingLabel.Location = New System.Drawing.Point(0, 0)
        Me.HeadingLabel.Name = "HeadingLabel"
        Me.HeadingLabel.Size = New System.Drawing.Size(168, 23)
        Me.HeadingLabel.TabIndex = 0
        Me.HeadingLabel.Text = "Label1"
        '
        'LinkLabel1
        '
        Me.LinkLabel1.Location = New System.Drawing.Point(0, 32)
        Me.LinkLabel1.Name = "LinkLabel1"
        Me.LinkLabel1.TabIndex = 1
        Me.LinkLabel1.TabStop = True
        Me.LinkLabel1.Text = "LinkLabel1"
        '
        'Dataset11
        '
        Me.Dataset11.DataSetName = "Dataset1"
        Me.Dataset11.Locale = New System.Globalization.CultureInfo("en-US")
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.LinkLabel1)
        Me.Controls.Add(Me.HeadingLabel)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.Dataset11, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub AddFileNameToList(ByVal FullPath As String)
        Dim NewFileCreated As Dataset1.FilesCreatedRow = Dataset11.FilesCreated.NewFilesCreatedRow
        NewFileCreated.FileName = FullPath
        Dataset11.FilesCreated.AddFilesCreatedRow(NewFileCreated)
    End Sub
    Private Sub DisplayAllFilesCreated()
        'Dim HeadingLabel As Label
        HeadingLabel.Text = "The Following Files Have Been Created : "
        Dim myLabel() As LinkLabel
        Dim i As Integer
        For i = 0 To Dataset11.FilesCreated.Count
            'create a new linklabel
            'set linklabel's height to be = linklabel(i -1).top +  linklabel(i -1).height
            myLabel(i).Text = Dataset11.FilesCreated(i)(0).ToString
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        For i = 0 To 9
            AddFileNameToList("C:\F" & i.ToString & ".txt")
        Next
        DisplayAllFilesCreated()
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked

    End Sub
End Class


'-----------------thanks!

0
Comment
Question by:jxharding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 12227090
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents HeadingLabel As System.Windows.Forms.Label
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.HeadingLabel = New System.Windows.Forms.Label
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.SuspendLayout()
        '
        'HeadingLabel
        '
        Me.HeadingLabel.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.HeadingLabel.Location = New System.Drawing.Point(8, 8)
        Me.HeadingLabel.Name = "HeadingLabel"
        Me.HeadingLabel.Size = New System.Drawing.Size(212, 16)
        Me.HeadingLabel.TabIndex = 0
        Me.HeadingLabel.Text = "Label1"
        '
        'Panel1
        '
        Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Panel1.AutoScroll = True
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel1.Location = New System.Drawing.Point(8, 32)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(212, 269)
        Me.Panel1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(232, 310)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.HeadingLabel)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private ds1 As New DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myTable As New DataTable("Files")
        myTable.Columns.Add("FileName")
        ds1.Tables.Add(myTable)

        Dim i As Integer
        For i = 0 To 9
            AddFileNameToList("C:\F" & i.ToString & ".txt")
        Next
        DisplayAllFilesCreated()
    End Sub

    Private Sub AddFileNameToList(ByVal FullPath As String)
        Dim newFile As DataRow = ds1.Tables("Files").NewRow
        newFile.Item("FileName") = FullPath
        ds1.Tables("Files").Rows.Add(newFile)
    End Sub

    Private Sub DisplayAllFilesCreated()
        'Dim HeadingLabel As Label
        HeadingLabel.Text = "The Following Files Have Been Created : "

        Dim link As LinkLabel
        Dim dr As DataRow
        Dim dt As DataTable = ds1.Tables("Files")
        Dim i As Integer

        Panel1.Controls.Clear()
        For i = 0 To dt.Rows.Count - 1
            dr = dt.Rows(i)
            link = New LinkLabel
            link.Text = dr.Item("FileName")
            link.Links(0).LinkData = link.Text
            link.Top = 15 + i * link.Height
            link.Left = 15
            AddHandler link.LinkClicked, AddressOf Me.LinkLabel_LinkClicked
            Panel1.Controls.Add(link)
        Next
    End Sub

    Private Sub LinkLabel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        Try
            System.Diagnostics.Process.Start(e.Link.LinkData)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

End Class
0
 

Author Comment

by:jxharding
ID: 12234737
this will be a much used reference in my opinion!
thanks a million idle_mind
0
 

Author Comment

by:jxharding
ID: 12287475
hi idle_mind
do you perhaps know of a way that i can modify the linklabel_linkclicked event so i can always open the file with wordpad?

   Try
            System.Diagnostics.Process.Start(e.Link.LinkData)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

thanks!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12291182
Sure use this instead:

    Process.Start("WordPad.exe", e.Link.LinkData)

~IM
0
 

Author Comment

by:jxharding
ID: 12297430
excellent, thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question