[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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!

0
jxharding
Asked:
jxharding
  • 3
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
jxhardingAuthor Commented:
this will be a much used reference in my opinion!
thanks a million idle_mind
0
 
jxhardingAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sure use this instead:

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

~IM
0
 
jxhardingAuthor Commented:
excellent, thanks again!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now