Initially Empty Labels

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cindy2
    New Member
    • Sep 2007
    • 35

    Initially Empty Labels

    Hi,

    In my vb2005-project I have a x by x array as type double. To visualise what's in it, I connected each index to a label. When I run the programm, the initial array is empty, so each label contains a zero.

    The problem I have is, I don't wanna see all the zero's initially (on runtime). I only want to see the labels if I put a zero (or another number) in the array. Is there an easy way to do this?

    Cindy
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    (Caveat: I'm a VB6 developer so may be talking garbage...)

    Can you explain what you mean by "connecting each index to a label"? Or is this too obvious in VB2005 to explain?

    Comment

    • cindy2
      New Member
      • Sep 2007
      • 35

      #3
      Originally posted by Killer42
      (Caveat: I'm a VB6 developer so may be talking garbage...)

      Can you explain what you mean by "connecting each index to a label"? Or is this too obvious in VB2005 to explain?
      Hi,

      Yes, it's very obvious in vb2005 :). Let's say I have an array with 4 by 4 elements, which gives a total of 16 elements. Now, on my design-form I put 16 labels. Each of these labels is connected to an index of the array:

      Code:
      label1.text = Arr(0, 0)    'first colomn
      label2.text = Arr(1, 0)
      label3.text = Arr(2, 0)
      label4.text = Arr(3, 0)
      label5.text = Arr(1, 1)   'second column
      label6.text = Arr(2, 1)
      ....etc.
      Label16.text = Arr(3, 3)
      I hope this explains what you mean, because on the other hand, I don't know VB6.

      Cindy

      Comment

      • cugone
        New Member
        • Sep 2007
        • 20

        #4
        Depending on the legal range of numbers you want, just put a number outside said range and check for not that number. EX:

        Your legal range is zero or above:

        Code:
        'This can also be accomplished by setting each label in the Propeties tab on the
        'Designer page (select them all, set the Text Prop to -1.0).
        
        label1.text = "-1.0"
        label2.text = "-1.0"
        ...
        label16.text = "-1.0"
        
        .....
        dim i as double = 0.0
        dim j as double = 0.0
        
        for each allControls in me.Controls
             if (typeof allControls is label) then
                  if Convert.toDouble(allControls.text) >= 0.0 then
                       allControls.text = array(i,j).tostring
                  end if
             end if
             i += 1
             j += 1
        next

        Comment

        • cindy2
          New Member
          • Sep 2007
          • 35

          #5
          Originally posted by cugone
          Depending on the legal range of numbers you want, just put a number outside said range and check for not that number. EX:

          Your legal range is zero or above:

          Code:
          'This can also be accomplished by setting each label in the Propeties tab on the
          'Designer page (select them all, set the Text Prop to -1.0).
          
          label1.text = "-1.0"
          label2.text = "-1.0"
          ...
          label16.text = "-1.0"
          
          .....
          dim i as double = 0.0
          dim j as double = 0.0
          
          for each allControls in me.Controls
               if (typeof allControls is label) then
                    if Convert.toDouble(allControls.text) >= 0.0 then
                         allControls.text = array(i,j).tostring
                    end if
               end if
               i += 1
               j += 1
          next
          Thank you for your reply. I'm sorry, but I don't understand. I know the code you have written is specific, but I'm afraid it's not specific enough for me. I tried some things with your code, but it does not give the results I want.

          For example: Is the array of type data or string?

          Cindy

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by cindy2
            Yes, it's very obvious in vb2005 :). Let's say I have an array with 4 by 4 elements, which gives a total of 16 elements. Now, on my design-form I put 16 labels. Each of these labels is connected to an index of the array:
            Code:
            label1.text = Arr(0, 0)    'first colomn
            label2.text = Arr(1, 0)
            label3.text = Arr(2, 0)
            label4.text = Arr(3, 0)
            label5.text = Arr(1, 1)   'second column
            label6.text = Arr(2, 1)
            ....etc.
            Label16.text = Arr(3, 3)
            So, in other words, there isn't any sort of connection between them. You just copied the values from the array into the labels.

            Which means the reason there are zeroes there is that you put them there. Which in turn means that the solution is obvious - don't! :)

            For example...
            [CODE=vbnet]If Arr(1, 0) Then
            label2.text = Arr(1, 0)
            End If[/CODE](This is taking advantage of the fact that any non-zero value will evaluate to True in an IF. If you want, you could also say If Arr(1, 0) <> 0 Then.)

            Of course, I wouldn't code this IF test 16 times. It'd make for nicer code to do something like creating a function that returns a string containing the number only if it's non-zero. For the sake of argument, let's say you called the function BlankIfZero. Then you could say...
            [CODE=vbnet]label1.text = BlankIfZero(Arr (0, 0)) 'first colomn
            label2.text = BlankIfZero(Arr (1, 0))
            label3.text = BlankIfZero(Arr (2, 0))
            label4.text = BlankIfZero(Arr (3, 0))
            label5.text = BlankIfZero(Arr (1, 1)) 'second column
            label6.text = BlankIfZero(Arr (2, 1))[/CODE]

            Comment

            • cindy2
              New Member
              • Sep 2007
              • 35

              #7
              Originally posted by Killer42
              For example...
              [CODE=vbnet]If Arr(1, 0) Then
              label2.text = Arr(1, 0)
              End If[/CODE](This is taking advantage of the fact that any non-zero value will evaluate to True in an IF. If you want, you could also say If Arr(1, 0) <> 0 Then.)

              Of course, I wouldn't code this IF test 16 times. It'd make for nicer code to do something like creating a function that returns a string containing the number only if it's non-zero. For the sake of argument, let's say you called the function BlankIfZero. Then you could say...
              [CODE=vbnet]label1.text = BlankIfZero(Arr (0, 0)) 'first colomn
              label2.text = BlankIfZero(Arr (1, 0))
              label3.text = BlankIfZero(Arr (2, 0))
              label4.text = BlankIfZero(Arr (3, 0))
              label5.text = BlankIfZero(Arr (1, 1)) 'second column
              label6.text = BlankIfZero(Arr (2, 1))[/CODE]
              Thanks for your reply,

              I only want to "hide' the zero's initially at runtime. So it must still be possible to put a zero in the array and then copy it to the corresponding label. Is that also possible with your approach?

              Cindy

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by cindy2
                I only want to "hide' the zero's initially at runtime. So it must still be possible to put a zero in the array and then copy it to the corresponding label. Is that also possible with your approach?
                Sure. Since your code is putting the zeroes in there, you can do it or skip it any time you like.

                One simple technique would be to create a global Boolean variable. Let's call it ThisIsNotTheFir stTime, just for fun. At the end of the segment of code which sets the labels, you set it to True.

                Then, in the BlankIfZero function, you'd do something like...

                [CODE=vb]Public Function BlankIfZero(ByV al TheNumber As Long) As String
                If (TheNumber <> 0) Or ThisIsNotTheFir stTime Then
                BlankIfZero = TheNumber
                End If
                End Function[/CODE]

                You just have to use a bit of imagination. :)

                Comment

                • cindy2
                  New Member
                  • Sep 2007
                  • 35

                  #9
                  Originally posted by Killer42
                  Sure. Since your code is putting the zeroes in there, you can do it or skip it any time you like.

                  You just have to use a bit of imagination. :)

                  Thank you, it does work fine, but I still have a problem. The labels are initially empty, but when I want to put a number in the array, that then is copied to a label, I have to go through all the 'code' that copies the array-indexes to the labels. This is because in the first instance I don't know which index must be copied to the corresponding label. So all the zero's will be visible (again). My code looks something like this:
                  Code:
                  If Arr1(j) = Arr2(i) Then                           
                            Array(i, column) = Arr3(j)      ' "Array" is the array we are talking about
                  End If
                  And as mentioned, next I have to copy the indexnumbers to the labels:
                  [CODE=vbnet]
                  label1.text = BlankIfZero(Arr ay(0, 0)) 'first colomn
                  label2.text = BlankIfZero(Arr ay(1, 0))
                  label3.text = BlankIfZero(Arr ay(2, 0))
                  label4.text = BlankIfZero(Arr ay(3, 0))
                  label5.text = BlankIfZero(Arr ay(1, 1)) 'second column
                  label6.text = BlankIfZero(Arr ay(2, 1))
                  ........etc.
                  label16.text = BlankIfZero(Arr ay(3, 3))
                  [/CODE]

                  I really tried to use my imagination, but I can't figure it out how to 'hide' the initial zero's when I change the array. Do you have some advice for me?

                  Cindy

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Um... I think someone else posted a solution earlier in this thread which might help. That is, pick some value that won't show up in the real data, and initialise all your array entries with that value. For example, set them all to -999.

                    Then, every time you copy them to the labels, you show whatever value is there, unless it's -999. That way, if a zero does turn up, it will be displayed.

                    What do you think? I can't say for sure, as I don't know your data.

                    Comment

                    • QVeen72
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1445

                      #11
                      Hi,

                      Declare the Array as a Variant.. say :
                      Dim MyArr(4,4)

                      Do not write as Double.. (or whatever)
                      This is Because, when you declare an Array as some type, All the Array items will be Initialized to default Value (0 for numeric, Blank String for Strings, False For Boolean). With Variant, All of the Array Items wud be Empty.. And Non-Initialized items will show as blank in the Label..
                      And will work for your problem..
                      Write one Common Procedure To Populate the Labels with array Values.
                      And whenevr, Array items are Manipulated, call that Procedure.

                      Regards
                      Veena

                      Comment

                      • cindy2
                        New Member
                        • Sep 2007
                        • 35

                        #12
                        Originally posted by QVeen72
                        Hi,

                        Declare the Array as a Variant.. say :
                        Dim MyArr(4,4)

                        Do not write as Double.. (or whatever)
                        This is Because, when you declare an Array as some type, All the Array items will be Initialized to default Value (0 for numeric, Blank String for Strings, False For Boolean). With Variant, All of the Array Items wud be Empty.. And Non-Initialized items will show as blank in the Label..
                        And will work for your problem..
                        Write one Common Procedure To Populate the Labels with array Values.
                        And whenevr, Array items are Manipulated, call that Procedure.

                        Regards
                        Veena
                        Thank you for your reply,
                        It does work, all the labels are blank initially. And when I put a number in the array, the corresponding label will visualize it.

                        Another part of the programm adds all the row ellements of the array. Because of the 'variant' type, the programm tries to add string-elements (the elements of the array that haven't been populated yet), so an error occured. I tried to convert the type of the array to single for that part of programm, but that didn't work. Do you have a suggestion?

                        Cindy

                        Comment

                        • QVeen72
                          Recognized Expert Top Contributor
                          • Oct 2006
                          • 1445

                          #13
                          Hi,

                          use Val Function
                          Some thing like this :

                          TArr(1,2) = Val(TArr(2,3)) + Val(TArr(2,4))

                          Regards
                          Veena

                          Comment

                          • cindy2
                            New Member
                            • Sep 2007
                            • 35

                            #14
                            Thank you all!!!
                            It works great! :)

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #15
                              Originally posted by cindy2
                              Thank you all!!!
                              It works great! :)
                              Excellent!

                              Good work, team. :)

                              Comment

                              Working...