Link to home
Start Free TrialLog in
Avatar of jxharding
jxharding

asked on

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

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!

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jxharding
jxharding

ASKER

this will be a much used reference in my opinion!
thanks a million idle_mind
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!
Sure use this instead:

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

~IM
excellent, thanks again!