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

x
?
Solved

vb.net how to put value to each label

Posted on 2012-09-04
19
Medium Priority
?
1,566 Views
Last Modified: 2012-09-14
hi,

I am writing VB.net 2010 with sql 2005 , I am try to reset all the label text, but I get the error like picture, some one can help me?

code:

 For i = 1 To 3
            Dim myLabel As Label = CType(Me.Controls("Label" & i + 3), Label)
            myLabel.Text = "test"
        Next
error.jpg
0
Comment
Question by:Simon Chen
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 38365516
try

For i = 1 To 3
          Me.Controls.Find("Label" & i + 3).Text = "test"
        Next 

Open in new window

0
 

Author Comment

by:Simon Chen
ID: 38365543
no, it didn't work,  argument no specified for parameter 'searchAllChildren' of 'public function find.......
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1000 total points
ID: 38365545
Dim myLabel As Label
        For i = 1 To 3
            myLabel = CType(Me.Controls.Find("Label" & i + 3, False)(0), Label)
            myLabel.Text = "test"
        Next

Open in new window

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 35

Expert Comment

by:YZlat
ID: 38365552
Question, are your Labels namet Label4, label5, and Label6,
or Label13, Label23, and Label33?
0
 

Author Comment

by:Simon Chen
ID: 38365557
label4,label5,label6 ......until label21
0
 
LVL 2

Expert Comment

by:kristof1104
ID: 38365562
Hy,

The error you are getting means the lable you are trying to set is "Nothing".

It does not exist!
can I ask why you are using "Label" & i + 3?

I assume your labels are called:
Label1
Label2
Label3?

The code you use suggest that the labels are called:
Label4
Label5
Label6

I see a Label23 in your picture Are you using the following label names?
Label13
Label23
Label33

the you should use
Dim myLabel As Label = CType(Me.Controls("Label" & i + "3"), Label)

Open in new window



Regards Kristof
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38365576
did you try my second suggestion?

Dim myLabel As Label
        For i = 1 To 3
            myLabel = CType(Me.Controls.Find("Label" & i + 3, False)(0), Label)
            myLabel.Text = "test"
        Next 

Open in new window

0
 

Author Comment

by:Simon Chen
ID: 38365579
I try this script ,but still get error
error2.jpg
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 38365587
You want to pass TRUE to Find() so it does a recursive search.  This will find the Label no matter what its container is, like if it is in a Panel for instance and not directly contained by the form.

So change YZlat's code to:
        Dim myLabel As Label
        For i = 4 To 21
            myLabel = CType(Me.Controls.Find("Label" & i, True)(0), Label)
            myLabel.Text = "test"
        Next

Open in new window

0
 

Author Comment

by:Simon Chen
ID: 38365588
the other label is fixed I didn't need to change any thing, only from label 4 to label 21link to  the database file name
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38365592
Dim myLabel As Label
        For i = 1 To 3
            	myLabel = CType(Me.Controls.Find("Label" & i + 3, False)(0), Label)
		If Not myLabel Is Nothing Then             
 			myLabel.Text = "test"       
		End If 
            	myLabel.Text = "test"
        Next  

Open in new window

0
 
LVL 2

Expert Comment

by:kristof1104
ID: 38365594
Are you sure there are labels named Label4,Label5,Label6?
debug to see at which iteration the error comes up!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38365607
If you continue to have issues, try out this more verbose form to help diagnose:
        Dim myLabel As Label
        Dim ctlName As String
        Dim matches() As Control
        For i As Integer = 4 To 21
            ctlName = "Label" & i
            matches = Me.Controls.Find(ctlName, True)
            If matches.Length > 0 Then
                If TypeOf matches(0) Is Label Then
                    myLabel = CType(matches(0), Label)
                    myLabel.Text = "test"
                Else
                    MessageBox.Show("Match was not a Label", "Incorrect Type of Control found!")
                End If
            Else
                MessageBox.Show(ctlName, "Control Not Found!")
            End If
        Next

Open in new window

0
 

Author Comment

by:Simon Chen
ID: 38365619
YZlat,
thanks a lot, this good for me ,it pass on my debug.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38367585
if so, then why didn't you accept my answer?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38367862
I posted a MINOR correction to YZlat's post here:
http:#a38365545

My post:
http:#a38365587

If my correction fixed the issue then I'd suggest a split between the two.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 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