Advertisement

06.09.2008 at 03:47AM PDT, ID: 23468562
[x]
Attachment Details

Array of labels....

Asked by imarshad in Microsoft Visual Basic.Net, Programming Languages, Visual Basic Programming

Tags: Array of controls, array of labels, runtime addition of labels, `

Hi all,
        In VB6 we could load labels at runtime and display them by using Control arrays... something like
       
        Load Label1(Label1.Count)
        Label1(Label1.UBound).Top = Y
        Label1(Label1.UBound).Left = X
        Label1(Label1.UBound).Caption = "XYZ"
        Label1(Label1.UBound).Visible = True
         
I know that Arrays of control is not supported anymore in VB.NET. So I searched for some workaround and I found the following method.
But I am unable to run this code. I always get a "Object reference not set to an instance of an object" on the line Labels(0) = New Label
Now my questions are

1) How to get this working??

2) Also what is the .NET way of loading labels at run time? Also I want to have a single Eventhandler for all the labels? Maybe using collections is the way but a sample code will be highly helpful.....
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
Dim Labels() As Label
  
  Sub AddMoreLabels(ByVal NewX As Integer, ByVal NewY As Integer)
        Try
            If Labels Is Nothing Then
                Labels(0) = New Label
                Me.Controls.Add(Labels(UBound(Labels) - 1))
                AddHandler Labels(UBound(Labels) - 1).Click, AddressOf Labels_Click
                Labels(UBound(Labels) - 1).Name = "Lbl(" & UBound(Labels) - 1 & ")"
                Labels(UBound(Labels) - 1).Location = New System.Drawing.Point(NewX, NewY)
            Else
                ReDim Preserve Labels(UBound(Labels))
                Labels(UBound(Labels) - 1) = New Label
                Me.Controls.Add(Labels(UBound(Labels) - 1))
                AddHandler Labels(UBound(Labels) - 1).Click, AddressOf Labels_Click
                Labels(UBound(Labels) - 1).Name = "Lbl(" & UBound(Labels) - 1 & ")"
                Labels(UBound(Labels) - 1).Location = New System.Drawing.Point(NewX, NewY)
                Labels(UBound(Labels) - 1).Visible = True
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Labels_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'See below for GetIndex code.
        MsgBox("Hello there from: " & GetIndex(Sender))
    End Sub
    'This function returns the "Index" of an array. Ie. If Sender is Lbl(5) it
    'will return 5 (See Labels_Click for Example)
    Function GetIndex(ByVal Sender As System.Object) As Integer
        'I used "Pos" and "Name just to enhance readability
        Dim Name As String = DirectCast(Sender, Control).Name
        Dim Pos As Integer = InStr(1, Name, "(") + 1
        Return CInt(Mid(Name, Pos, Len(Name) - Pos))
    End Function
[+][-]06.09.2008 at 04:18AM PDT, ID: 21742266

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.09.2008 at 04:19AM PDT, ID: 21742273

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.09.2008 at 04:29AM PDT, ID: 21742327

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, Programming Languages, Visual Basic Programming
Tags: Array of controls, array of labels, runtime addition of labels, `
Sign Up Now!
Solution Provided By: rachitkohli
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.09.2008 at 10:08PM PDT, ID: 21748615

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.10.2008 at 12:55AM PDT, ID: 21749209

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906