Results 1 to 9 of 9

Thread: label array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    78

    label array

    Hi

    I have about 20 labels displaying "C".
    When I press a button I want all of them to display "F".
    When I press another button I want all of them to go back to "C".

    ("C" & "F" = Celsius & Fahrenheit.)

    How do I make an array of them?

    It's for a PocketPC application.

    Thanks.

  2. #2
    Member
    Join Date
    May 2010
    Posts
    56

    Re: label array

    hmm try this:


    vb Code:
    1. For Each label As Label In Form1
    2.             If label.Text = "C" Then
    3.                 label.Text = "F"
    4.                 'I have to do this check otherwise I change all labels in my form
    5.             ElseIf label.Text = "F" Then
    6.                 label.Text = "C"
    7.             End If
    8.         Next

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    78

    Re: label array

    Thanks for the answer, unfortunately it comes with 2 errors:

    1.".......Form1" cannot refer to itself through its default instance; use "Me" instead.
    2. Expression is of type ".......Form1", which is not a collection type.

    If I follow its advice and use "Me" instead of "Form1", then line 1 of error goes away, but I still get the line 2 error.

    Thanks.

  4. #4
    Member
    Join Date
    May 2010
    Posts
    56

    Re: label array

    I am sorry, I did it very quickly.


    use:


    vb Code:
    1. For Each label As Label In Me.Controls
    2.             If label.Text = "C" Then
    3.                 label.Text = "F"
    4.                 'I have to do this check otherwise I change all labels in my form
    5.             ElseIf label.Text = "F" Then
    6.                 label.Text = "C"
    7.             End If
    8. Next

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: label array

    As stated in your original post, I'm assuming that your Label caption is already set to "F" or "C" and you want to change it.
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim labels = From lbl As Label In Me.Controls.OfType(Of Label)() _
    3.                  Where lbl.Text = "C"
    4.     For Each lbl In labels
    5.         lbl.Text = "F"
    6.     Next
    7. End Sub
    For the other button, replace "F" with "C" and "C" with "F"
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    78

    Re: label array

    Thanks kiwi1342

    Now I get another crash:
    InvalidCastException
    "When casting from a number, the value must be a number less than infinity.
    Make sure the source type is convertible to the destination type."

    But if I replace "Me.Controls" with "tab2.Controls" it works perfect, only on that tab. (or tab3)
    My application has 5 tabs.
    These are such labels ("C" & "F") on 2 tabs.
    tab2 & tab3
    the other tabs have labels too, but none "C" or "F".


    I'm sorry Pradeep1210, but your code doesn’t do anything, unless I change

    Dim labels = From lbl As Label In Me.Controls.OfType(Of Label)() Where lbl.Text = "C"

    to

    Dim labels = From lbl As Label In Me.Controls Where lbl.Text = "C"

    in which case it crashes in the same manner I stated above.
    But if I replace "Me.Controls" with "tab3.Controls" it works perfect, only on that tab. (or tab2)


    Thanks for support.

  7. #7
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: label array

    You can still put them in an array if this is more clear. Since I am not at a computer with VS you may need to play a little with the code to get it to work

    Code:
    Dim lblArray() as Label = {label1, label2, label3, label4}
    where label1, label2 etc. are the names of your current labels

    then you can loop throught them

    Code:
    For i = 0 to 3
         lblArray(i).Text = "C"
    Next i
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  8. #8
    Member
    Join Date
    May 2010
    Posts
    56

    Re: label array

    hmmm try this.

    I am sorry but I can't try it right now :/


    vb Code:
    1. For Each objcontrol As Control In Me.Controls
    2.             If objcontrol.GetType.ToString = "System.Windows.Forms.Label" And objcontrol.Text = "C" Then
    3.                 objcontrol.Text = "F"
    4.             ElseIf objcontrol.GetType.ToString = "System.Windows.Forms.Label" And objcontrol.Text = "F" Then
    5.                 objcontrol.Text = "C"
    6.             End If
    7.         Next

  9. #9
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: label array

    A small fix in my original post. Use the NEW keyword

    Quote Originally Posted by kaliman79912 View Post
    You can still put them in an array if this is more clear. Since I am not at a computer with VS you may need to play a little with the code to get it to work

    Code:
    Dim lblArray as Label() = NEW Label() {label1, label2, label3, label4}
    where label1, label2 etc. are the names of your current labels

    then you can loop throught them

    Code:
    For i = 0 to 3
         lblArray(i).Text = "C"
    Next i
    Dim lblArray as Label() = NEW Label() {label1, label2, label3, label4}
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width