Results 1 to 3 of 3

Thread: [02/03] Array Problem

  1. #1

    Thread Starter
    Member ukjock's Avatar
    Join Date
    Feb 2006
    Location
    Scotland
    Posts
    59

    [02/03] Array Problem

    A BIG thank you to Isorfir for his help in the following thread:

    http://www.vbforums.com/showthread.php?t=398486

    I am hoping that some one can help with this one.

    I am trying to achieve that when a checkbox is checked the value is put in to the array and displayed, then if 2 checkboxes are checked then it rotates through the 2 of the textlines in the array. But no matter what I try and achieve i can never get the 2 to rotate nor get textbox2 to be displayed if it is the only thing selected. (More checkboxs will be added later)

    I have attached the sample project and an image of what happens, the following should happen in the picture.

    1. nothing selected, the label shows Nothing Selected (YES)
    2. Checkbox1 checked then it shows the text from textbox1 in the Label (YES)
    3. Checkbox1+2 are checked the rotates the text textbox1+2 in to the label (NO)
    4. Checkbox1 is un-checked so show the text from textbox2 as checkbox to is still checked (NO)
    5. Nothing is checked, then change the label to Nothinng is Selected (YES)


    All the coding in for this in the above posting, but I have reposted it below here, I have looked through it and tried to alter some lines but nothing works, infact i make it worse.

    VB Code:
    1. Dim ErrorArray(7) As String 'Remember to start at 0, this array is 8 long
    2.  
    3.    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    4.         If CheckBox1.Checked = True Then
    5.             TextBoxArray(0) = TextBox1.Text
    6.         End If
    7.  
    8.         If CheckBox1.Checked = False Then
    9.             TextBoxArray(0) = ""
    10.         End If
    11.  
    12.     End Sub
    13.  
    14.     Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    15.         If CheckBox2.Checked = True Then
    16.             TextBoxArray(1) = TextBox1.Text
    17.         End If
    18.  
    19.         If CheckBox2.Checked = False Then
    20.             TextBoxArray(1) = ""
    21.         End If
    22.  
    23.     End Sub

    Then in the timer there is

    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         Dim hasValue As Integer
    3.         Dim I As Integer
    4.         Dim J As Integer
    5.  
    6.         'Check to see if the array holds any values
    7.         Do While I < TextBoxArray.Length
    8.             hasValue += Len(TextBoxArray(I))
    9.             I += 1
    10.         Loop
    11.         I = 0
    12.  
    13.         'If if doesn't, this text displays
    14.         If hasValue = 0 Then
    15.             LBLOutput.Text = "Nothing is Selected"
    16.  
    17.         Else
    18.  
    19.             'If it does, it will display each checked box once per tick
    20.             If J < TextBoxArray.Length Then
    21.                 If TextBoxArray(J) <> "" Then
    22.                     LblOutput.Text = TextBoxArray(J)
    23.                 End If
    24.                 J = +1
    25.  
    26.             Else
    27.                 J = 0
    28.  
    29.             End If
    30.         End If
    31.     End Sub

    Thanks for looking.

    Kind Regards

    Chris

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [02/03] Array Problem

    Don't forget, each time the timer ticks, all your variables get redeclared. You can change this by assigning one of your count variables as Static, which will mean that only one instance of it exists throughout the program, no matter how many times it is called. Also, you should be using a boolean instead of an integer for hasValue, but this code will fix your rotating problem:

    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         Dim hasValue As Boolean
    3.         Dim I As Integer = 0
    4.         Static CurrentBox As Integer
    5.  
    6.         'Check to see if the array holds any values
    7.         Do While I < TextBoxArray.Length
    8.             If TextBoxArray(I) <> "" Then hasValue = True
    9.             I += 1
    10.         Loop
    11.         I = 0
    12.  
    13.         'If if doesn't, this text displays
    14.         If Not hasValue Then
    15.             LblOutput.Text = "Nothing is Selected"
    16.         Else
    17.             If TextBoxArray(CurrentBox) <> "" Then
    18.                 LblOutput.Text = TextBoxArray(CurrentBox)
    19.                 CurrentBox += 1
    20.             Else
    21.                 CurrentBox = 0
    22.             End If
    23.  
    24.         End If
    25.     End Sub

    Next problem, you've got a copy and paste error in the CheckChanged handler for CheckBox2. It's setting the text to textbox 1, so even if it does work, you'll need to change it to checkbox2 to get it to function like you want.

    VB Code:
    1. Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    2.         If CheckBox2.Checked = True Then
    3.             TextBoxArray(1) = [color=red]TextBox1.Text[/color] 'Should Be TextBox2.Text
    4.         End If
    5.  
    6.         If CheckBox2.Checked = False Then
    7.             TextBoxArray(1) = ""
    8.         End If
    9.  
    10.     End Sub

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3

    Thread Starter
    Member ukjock's Avatar
    Join Date
    Feb 2006
    Location
    Scotland
    Posts
    59

    Re: [02/03] Array Problem

    Thank you for that, it is very much appreciated.

    I spotted that small copy and paste issue and corrected it, right after posting this.

    I now have the problem that if i checkbox2 it will not display the textbox2 content. it will sit and say Nothing is selected.

    it will rotate through textbox1 and textbox2 if they are both selected, which is really nice.

    I thought I would add another checkbox and textbox and leave 2 and 3 running, but it would not scroll between the 2 unless 1 was checked, and if you unchecked 1 once under way it would get to the end of the array and not jump back to the which ever array content is filled.

    I am off to try and find some material on how to deal with arrays.

    Kind Regards

    Chris

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