Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

loop thru labels and set value

Posted on 2006-04-04
9
Medium Priority
?
276 Views
Last Modified: 2010-04-24
I have a series of 12 labels on my form and I want to use a for loop to go thru the list and set a value from a db call.
this is my function:
Private Sub GetQuestions(ByVal SectionID As Integer)
        Dim myDR As SqlDataReader
        Dim myQuestion As New Utilities
        Dim i As Integer
        myDR = myQuestion.GetQuestions(SectionID)
        While myDR.Read
            For i = 1 To 12
                If Not IsDBNull(myDR("QuestionDesc")) Then

                    CType(lblQuestion & i, Label).Text = myDR("QuestionDesc")
                End If
            Next
        End While
    End Sub

I do not know how to concatenate the i to the end of the name of the label


thanks

Jcook32
0
Comment
Question by:jcook32
  • 4
  • 3
  • 2
9 Comments
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16375456
For each ctl as Control in Me.Controls
  If typeof ctl is Label then
    if ctl.Name.StartsWith ("lblQuestion") then
       ctl.Text =Convert.ToString(  myDr!QuestionDesc)
  end if
end if

Next

Something like this?

You have to change your myDR.Read (it will progress each time you call the read, now it will read, update 12 labels, read again, update 12 labels...etc )
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16376826
Cool way:

Imports System.Reflection

Public Class ApplicationControls

  Public Shared Function FindControl(ByVal owner As Form, ByVal name As String) As Control

    Dim propInfo As PropertyInfo = _
     owner.GetType().GetProperty(name, BindingFlags.IgnoreCase Or _
     BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public)

    If Not propInfo Is Nothing Then
      Dim value As Object = propInfo.GetValue(owner, Nothing)
      If TypeOf value Is Control Then
        Return value
      End If
    Else
      Return Nothing
    End If

  End Function  'FindControl

End Class

Sample usage:
  CType(ApplicationControls.FindControl(Me, "lblQuestion" & i).Text = myDR("QuestionDesc").ToString()

Bob
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16378470
The setdown on your solution is bob, that controlsnames don't need to be unique (if you create them by code that is). So you will not neccessarily find your control unless you return array instead of single item.
But reflection is cool, and costly, but still cool :D
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16380574
@PockyMaster:
>>unless you return array instead of single item
Huh?  You can't see the confusion on my face, can you?

Bob
0
 

Author Comment

by:jcook32
ID: 16380586
TheLearnedOne,
     The type Form is invalid in the function call. i tried. is there another type?

thanks
i like the reflection
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16380607
System.Windows.Forms.Form

Add this line to the top:

    Imports System.Windows.Forms


Bob
0
 

Author Comment

by:jcook32
ID: 16380630
this is a asp.net/vb.net app. not a windows form app.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 16380660
Geez, why didn't I ask or why didn't you say so ;)

Throw out all that code, and use this (FindControl is already defined for web pages):

  CType(FindControl("lblQuestion" & i), Label).Text = myDR("QuestionDesc").ToString()

Bob
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16382283
TheLearnedOne:

Try to do this:

Dim lbl as label

for i as integer = 0 to 10
lbl = new Label
lbl.Name = "whatever"
myform.Controls.Add(lbl)
next


Now you have 11 labels with name "whatever" :D
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

564 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