Array of Labels

Joined
Jun 3, 2017
Messages
4
Reaction score
0
Hi:

I have a panel with 500 labels named: L0, L1, L2....L500

I need to acces them later by index, so i tried the following:

Dim LQ() As Label = {L0, L1, L2, L3, L4, L5, L6, L7, L8, L9, ...... L499}, it works fine.

But I don't want to type one by one L0, L1..... L499. Then I tried this:

Dim auxStr As String = "{"

For f = 0 To 499
auxStr = auxStr & "L" & f & ", "
Next f

auxStr = auxStr.Substring(0, auxStr.Length - 1) & "}"

Dim LQ As Label = auxStr


Error: > Value of type 'String' cannot be converted to 'Label'

Any help will be appreciated. Thanks in advance and sorry for my English.

Dalton Serkez
 
Joined
Jun 15, 2017
Messages
14
Reaction score
6
Seems that you're using VisualBasic.Net.
If that's true, you don't have to traverse labels one by one.
But you could use the For Each...Next statement.
If you got a panel named panel1. And you have 500 labels on panel1, please use the code below:
Code:
Dim ctl As Control
    For Each ctl In Me.Panel1.Controls() ' Travers label's in panel1
        If "Label" = ctl.Name.ToString().Substring(0, 5) Then ' Make a judgement that current control is a label
            ctl.Text = "hello" ' Alter label's content
        End If
    Next
So after you run that all 500 labels ' text will become "hello".
The second method for 500 label is to define a Label Array which containing 500 elements.
And you can use For Each either regular For loop to traverse them.
So you can dynamically define labels by using "Panel1.Controls.Add" repeatedly.
Hope these info can help you.
 
Last edited:
Joined
Jun 3, 2017
Messages
4
Reaction score
0
Hi CryZ. Thanks for helping me out.
Let me give you a better scenario:

I have a datagridview populated from a database, and a panel with existing labels, named L0,L1,L2,L3......L1000

The datagridview has now 500 rows and 4 columns. I need to transfer the data from DGV to every single label. This is what I am doing:

Dim LQ() As Label = {L0, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16, L17, L18, L19, L20, L21, L22, L23, L24, L25, L26, L27, L28, L29, L30, L31,
L32, L33, L34, L35, L36, L37, L38, L39, L40, L41, L42, L43, L44, L45, L46, L47, L48, L49, L50, L51, L52, L53, L54, L55, L56, L57, L58, L59, L60, L61, L62, L63,
L64, L65, L66, L67, L68, L69, L70, L71, L72, L73, L74, L75, L76, L77, L78, L79, L80, L81, L82, L83, L84, L85, L86, L87, L88, L89, L90, L91, L92, L93, L94, L95,
L96, L97, L98, L99, L100, L101, L102, L103, L104, L105, L106, L107, L108, L109, L110, L111, L112, L113, L114, L115, L116, L117, L118, L119, L120, L121, L122, L123, L124, L125, L126, L127,
L128, L129, L130, L131, L132, L133, L134, L135, L136, L137, L138, L139, L140, L141, L142, L143, L144, L145, L146, L147, L148, L149, L150, L151, L152, L153, L154, L155, L156, L157, L158, L159,
L160, L161, L162, L163, L164, L165, L166, L167, L168, L169, L170, L171, L172, L173, L174, L175, L176, L177, L178, L179, L180, L181, L182, L183, L184, L185, L186, L187, L188, L189, L190, L191,
L192, L193, L194, L195, L196, L197, L198, L199, L200, L201, L202, L203, L204, L205, L206, L207, L208, L209, L210, L211, L212, L213, L214, L215, L216, L217, L218, L219, L220, L221, L222, L223,
L224, L225, L226, L227, L228, L229, L230, L231, L232, L233, L234, L235, L236, L237, L238, L239, L240, L241, L242, L243, L244, L245, L246, L247, L248, L249, L250, L251, L252, L253, L254, L255,
L256, L257, L258, L259, L260, L261, L262, L263, L264, L265, L266, L267, L268, L269, L270, L271, L272, L273, L274, L275, L276, L277, L278, L279, L280, L281, L282, L283, L284, L285, L286, L287,
L288, L289, L290, L291, L292, L293, L294, L295, L296, L297, L298, L299, L300, L301, L302, L303, L304, L305, L306, L307, L308, L309, L310, L311, L312, L313, L314, L315, L316, L317, L318, L319,
L320, L321, L322, L323, L324, L325, L326, L327, L328, L329, L330, L331, L332, L333, L334, L335, L336, L337, L338, L339, L340, L341, L342, L343, L344, L345, L346, L347, L348, L349, L350, L351,
L352, L353, L354, L355, L356, L357, L358, L359, L360, L361, L362, L363, L364, L365, L366, L367, L368, L369, L370, L371, L372, L373, L374, L375, L376, L377, L378, L379, L380, L381, L382, L383,
L384, L385, L386, L387, L388, L389, L390, L391, L392, L393, L394, L395, L396, L397, L398, L399, L400, L401, L402, L403, L404, L405, L406, L407, L408, L409, L410, L411, L412, L413, L414, L415,
L416, L417, L418, L419, L420, L421, L422, L423, L424, L425, L426, L427, L428, L429, L430, L431, L432, L433, L434, L435, L436, L437, L438, L439, L440, L441, L442, L443, L444, L445, L446, L447,
L448, L449, L450, L451, L452, L453, L454, L455, L456, L457, L458, L459, L460, L461, L462, L463, L464, L465, L466, L467, L468, L469, L470, L471, L472, L473, L474, L475, L476, L477, L478, L479,
L480, L481, L482, L483, L484, L485, L486, L487, L488, L489, L490, L491, L492, L493, L494, L495, L496, L497, L498, L499}

Dim LABEL_NBR As Integer = 0
For DL = 0 To DGV.Rowcount-1
LQ(LABEL_NBR).Text = DGV_SEARCH.Rows(DL).Cells(0).Value
LQ(LABEL_NBR + 1).Text = DGV_SEARCH.Rows(DL).Cells(1).Value
LQ(LABEL_NBR + 2).Text = DGV_SEARCH.Rows(DL).Cells(2).Value
LQ(LABEL_NBR + 3).Text = DGV_SEARCH.Rows(DL).Cells(3).Value
LABEL_NBR += 4
Next

This method works, but every time datagridview is updated, I have to manually add more labels to LQ(): ----> L500,L501,L502,L503

What I am trying to do is to loop through datagridview and dimension LQ according to DGV number of rows.

Dim auxStr As String = "{"
For f = 0 DGV_SEARCH RowCount-1
auxStr = auxStr & "L" & f & ", "
Next f
auxStr = auxStr.Substring(0, auxStr.Length - 1) & "}"
Dim LQ As Label = auxStr

But this caused the error mentioned above.

Thanks again
 
Joined
Jun 15, 2017
Messages
14
Reaction score
6
Code A allocate a quantity of labels dynamically.
You could use Code A instead of define Dim LQ() As Label = {L0, L1, L2, L3, blah, blah
Because that's too boring to write such a number of Lx.
Assume you got a panel named panel1, put Code A into any procedure you wanna allocate 500 labels.
Notice: I created just 20 labels in Code1, and you shall change the number of labels.
Code:
' Code A
' These above code has been tested.
Const LBLWIDTH As Integer = 48  ' Constant of each label's width.
Const LBLHEIGHT As Integer = 16 ' Constant of each label's height.
' j represents column, l represents line number. Both of them are start form 0.
Dim i As Integer = 0, j As Integer = 0, l As Integer = 0
For i = 0 To 19 ' Assume that we are about to create 20 Labels.
    ' This With statment is a new feature of VisualBasic.Net
    ' Notice that different VB.Net may have different syntax specification.
    ' I'm using a VisualBasic.Net 2010. So I can use WITH. Test if your VB.NET support that.
    Dim LabelN As New Label With
    {
        .Name = "Label" & i.ToString,
        .Parent = Panel1,
        .Text = "Lbl " & i.ToString,
        .Size = New System.Drawing.Size(LBLWIDTH, LBLHEIGHT),
        .Location = New System.Drawing.Point(j * LBLWIDTH, l * LBLHEIGHT)
    }
    ' In the above brackets,
    ' At line 1, we set the current Label's name into LabelN. (N denote i, which represents each label's index.)
    ' At line 2, we set LabelN's parent control as panel1. So that LabelN can be shown onto panel1.
    ' At Line 3, we set Label's content, shows "Lbl N".
    ' At line 4, we define the size of current label. So the label size is (48*16).
    ' At line 5, we set LabelN's location. That is Which position does every label appear in panel1.
    Panel1.Controls.Add(LabelN) ' Though this sentence, we add our new Label onto panel1.
    LabelN.Show() ' Make LableN visible.
    j += 1 ' Increase Column
    If j Mod 4 = 0 Then ' If j mod 4 = 0, we got a label array which contains 4 labels in each line.
        l += 1
        j = 0
    End If
Next

Then you could use Code B to fill each label.
Assume that you've got a DataGridView named DataGridView1.
Code:
' Code B
' These above code has NOT been tested. I wrote them in theory.
' Because I can't connect my DataGridView1 on a database.
Dim ctl As Control
Dim i As Integer, j As Integer, k As Integer
Dim Row As Integer = DataGridView1.RowCount    ' Get DGV's row number
Dim Col As Integer = DataGridView1.ColumnCount ' Get DGV's column number
Dim Data() As String ' A dynamic string array.
ReDim Data(Row * Col) ' Allocated the string array dynamically.
k = Row * Col ' Solve out how many cell are there stored in the DGV.
' Traverse each cell. And put their value into Data array.
For i = 1 To Row - 1
    For j = 1 To Col - 1
        Data(k) = DataGridView1.Rows(i).Cells(j).Value
    Next
Next
' Traverse each label in panel1.
For Each ctl In Me.Panel1.Controls()
    If "Label" = ctl.Name.ToString().Substring(0, 5) Then
        ' Notice that we can get LabelN'N (that is a label's index) through sentence "ctl.Name.ToString().Substring(6)".
        ' Then we convert them into an integer.
        ' We treat that integer value as the index of data.
        ' So finally we put the data into the correct label.
        ' These code are kind of efficiency, you can write your own code to improve program performance.
        ctl.Text = Data(Integer.Parse(ctl.Name.ToString().Substring(6)))
    End If
Next

Well you just mentioned about every time datagridview is updated, you have to manually add more labels to LQ()
Don't need to do that. Just execute Code A once, and you could set an Event for DataGridView:
Code:
Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    ' Put Code B here. Refreshing will be automatically.
    End Sub
In the above code sub DataGridView1_CellValueChanged will be invoked for every time DataGridView1 changed it's content. That's the convenience of VisualBasic's Event-Driving Model.

At last, by the way, may I ask you a question?
Why you have to use 500 labels?
DataGridView itself can just display data, alter data, set style...
If you need a readonly DataGridView, just set it's ReadOnly property into value True.
 
Joined
Jun 3, 2017
Messages
4
Reaction score
0
I guess this what I was looking for. I will try it, but I am sure it will work. Let you know if something goes wrong.
By the way, datagridview and panel are part of a Stock Trading System.
Labels show ticker code, name, last price and % variation.
Datagrid displays the same as labels plus Open, Close, Max, Min, Med, Volume, etc...

Thank you very much.(obrigado!!!!!), and have a nice weekend
Regards
Dalton Serkez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top