VS 2010 Changing Checkbox Values-VBForums
Results 1 to 6 of 6

Thread: Changing Checkbox Values

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Changing Checkbox Values

    Hi,

    I have an array of structs and each struct contains values for the checkboxes. I am trying to display the checkbox values contained within a particular struct.

    MyStruct is an array of structs which has a values for the checkboxes

    ChkBoxCol is the collection of check boxes for the form.

    I am iterating through the collection as follows:

    Code:
    For Each ctrl In ChkBoxCol
                If MyStruct(index).ChkBoxArray(pos) = True Then
                    CType(ctrl, CheckBox).Checked = True
                    pos = pos + 1
                End If
            Next
    What I am noticing is that if the check boxes are checked in order (checkbox 1, 2, 3, 4...etc) the code works fine and the values are displayed correctly in the form. However, if the check boxes are not checked in order (4, 5, 7...etc) the code does not write the values of the check boxes and instead keeps them blank.

    I just need to display the values for the check boxes for a specific struct.

  2. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,768

    Re: Changing Checkbox Values

    There are two likely possibilities here. The first question I would ask is: Are you handling the CheckChanged event for these Checkboxes, and if so, how?

    The second question is: How are you storing the information into the checkboxes? Since you are using a structure in a collection, that could be the source of the problem.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Changing Checkbox Values

    The checkbox objects are stored in the collection. The values of the check boxes are stored in a boolean array within the struct. I have verified that the struct values are correct. The problem is only introduced when I try to write back the values from the struct into the form using the above code.

  4. #4
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,768

    Re: Changing Checkbox Values

    Ok, so are you doing anything in CheckChanged?

    The next thing to do is to put a breakpoint on the For Each line. Once you get there, step through one iteration of the loop while watching what values are coming from the structure. One of two things will happen:

    1) If you are stepping with F11, you will find out that setting the checkbox is triggering some event that you weren't expecting that is subsequently screwing up the data.

    2) You will find that the structure doesn't quite hold what you expected at the time of the call.

    The next step is dependent on which of those are the case.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Changing Checkbox Values

    I figured out the problem. The pos = pos + 1 statement needs to be outside the if block to ensure that the variable is always incremented.

    Thanks.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    89,101

    Re: Changing Checkbox Values

    Why use a For Each loop and then increment a variable every iteration? Why not just use a For loop in the first place?

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.