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
268 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
  • 3
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 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 85

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now