Results 1 to 12 of 12

Thread: My CheckBox always believes it is unchecked !

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    501

    My CheckBox always believes it is unchecked !

    I have a very stubborn check box . Let me tell you its story :
    There is Form1 with a button leading to Form2 . On Form2 there is located that stubborn check box I was telling you about . That check box has code in its CheckedChange event and accordingly to its Checked condition , it enables or disables some text boxes .
    Well , the first time Form2 is shown , that check box works fine : when I check it it enambles the text boxes and when I uncheck it , it disables those text boxes .
    Then , I press the button to return to Form1 . If needed , the code in the button is :
    Form1.Show
    Me.Close
    My adventure starts when I show again Form2 ... This time the check box is acting weirdly . No matter if I check it or I uncheck it , it always disables the text boxes , as if it is constantly unchecked ! I can see it changing its checked state whenever I click on it , but it always runs the code to disable the text boxes ...
    I even used some break points in the code and found out that indeed it is the code that disables the text boxes that is called every time , no matter if the check box is checked or unchecked .
    I don't thing this has anything to do with the fact that this event is called by all the 4 check boxes on the form . After all , it is always the 1st check box that causes the problem (I can see it when moving the cursor over the genericindex variable) .


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My CheckBox always believes it is unchecked !

    For a start, turn Option Strict On. That would show you right away that you are treating the CheckState as a Boolean, which it isn't. You are handling the CheckedChanged event, yet you are testing the CheckState property, which doesn't make sense. They are not a pair. The CheckedChanged event relates to the Checked property and the CheckStateChanged event relates to the CheckState property.

    Is the ThreeState property of your CheckBox set to true? If not then CheckState and CkeckStateChanged are of no interest to you. If your CheckBox only has two states then you only care about the Checked property. If your CheckBox has three states then you probably only care about the CheckState property, although you may care about the Checked property too.

    Fix that and see if the problem persists. If it does, create a new project and try to reproduce the problem. If you can't then most likely we couldn't either. If you can but you can't find a solution, explain to us how to reproduce it so we can test for ourselves.

    Also, it seems wrong that Form2 should be showing Form1. If you don't want the user to access Form1 while Form2 is open then you should just call ShowDialog instead of Show.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: My CheckBox always believes it is unchecked !

    I tried to re-create your issue

    In form1

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button2.Click
            Dim f As New Form2
            f.ShowDialog()
        End Sub
    Code:
    Public Class Form2
    
        Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, _
                                             ByVal e As System.EventArgs) _
                                         Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
    
            Dim cb As CheckBox = CType(sender, CheckBox)
            Select Case cb.Name
                Case "CheckBox1"
                    If CheckBox1.Checked Then TextBox1.Enabled = True Else TextBox1.Enabled = False
                Case "CheckBox2"
                    If CheckBox2.Checked Then TextBox2.Enabled = True Else TextBox2.Enabled = False
            End Select
        End Sub
    End Class
    Worked as expected. It looks like you have a dictionary maybe holding the controls???? How / when is it built?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: My CheckBox always believes it is unchecked !

    Quote Originally Posted by dbasnett View Post
    I tried to re-create your issue

    In form1

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button2.Click
            Dim f As New Form2
            f.ShowDialog()
        End Sub
    Code:
    Public Class Form2
    
        Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, _
                                             ByVal e As System.EventArgs) _
                                         Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
    
            Dim cb As CheckBox = CType(sender, CheckBox)
            Select Case cb.Name
                Case "CheckBox1"
                    If CheckBox1.Checked Then TextBox1.Enabled = True Else TextBox1.Enabled = False
                Case "CheckBox2"
                    If CheckBox2.Checked Then TextBox2.Enabled = True Else TextBox2.Enabled = False
            End Select
        End Sub
    End Class
    Worked as expected. It looks like you have a dictionary maybe holding the controls???? How / when is it built?
    By rewriting this you've already corrected his errors. As jmcilhinney has already made clear, he's treating the "CheckState" property as a boolean, which it isn't. Instead he should be using the "checked" property as you have done above, which is why yours works while his does not.

    In short, changed your if statements to check the "checked" property, not the "checkedstate".

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    501

    Re: My CheckBox always believes it is unchecked !

    They are part of an array . It is declared and defined when the program starts .
    I tried with Strict ON but I can't convert some classes , though I don't understand why the editor wants me to ... Am using some variables that have already been declared as my class yet the editor says that Strict ON does not allow conversions . But there is nothing for it to convert . Oh well ...
    Oh , I forgot above to say that I tried indeed (Separately) with both the properties (Checked and CheckSTate) but the problem remains ...

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: My CheckBox always believes it is unchecked !

    Quote Originally Posted by iliekater View Post
    They are part of an array . It is declared and defined when the program starts .
    I tried with Strict ON but I can't convert some classes , though I don't understand why the editor wants me to ... Am using some variables that have already been declared as my class yet the editor says that Strict ON does not allow conversions . But there is nothing for it to convert . Oh well ...
    Oh , I forgot above to say that I tried indeed (Separately) with both the properties (Checked and CheckSTate) but the problem remains ...
    So you are saying the controls on the form are not related to the controls in the array(based on the default instance of the form?), which sounds like the problem you described.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: My CheckBox always believes it is unchecked !

    So basically you are ignoring the advice given to you and claiming that you still have a problem? No wonder... Turn Option Strict On and deal with the conversions - you cannot implicitly convert from String to Int for example, which means that there IS something to convert. Use CInt and the like, and have a look at the Convert class. Also, read the MSDN documentation for conversion operators.

    Also, could you post the amended code, where you use Checked and it still doesn't work?

    One more thing. I noticed that you used Microsoft.VisualBasic.Right over there. That's a leftover from VB6, along with Len, InStr... You should use the String.Substring method instead.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My CheckBox always believes it is unchecked !

    Quote Originally Posted by iliekater View Post
    I tried with Strict ON but I can't convert some classes , though I don't understand why the editor wants me to ... Am using some variables that have already been declared as my class yet the editor says that Strict ON does not allow conversions
    Option Strict On certainly DOES allow conversions. What it doesn't allow is IMPLICIT conversions, which is exactly what the error message would have said. It requires all conversions to be EXPLICIT. For instance, this is an implicit conversion from Integer to String:
    vb.net Code:
    1. Dim number As Integer = 100
    2.  
    3. Me.Label1.Text = number
    The Text property is type String so can only have a String assigned to it. That means 'number' MUST be converted, which happens implicitly at run time. An explicit conversion looks like this:
    vb.net Code:
    1. Dim number As Integer = 100
    2.  
    3. Me.Label1.Text = number.ToString()
    Simple, but you're actually thinking about what types you have an what types you need, which will hopefully help you identify where the two aren't compatible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    501

    Re: My CheckBox always believes it is unchecked !

    There might be something when closing Form2 . You see if I change the line
    Code:
    Me.Close
    to
    Me.Hide
    then the next time Form2 is shown it keeps working fine . I looked in both forms but I couldn't find anything related to the closing of Form2 .

    However , apart from that , I noticed one more strange thing : if Form2 is closed with the check box being checked , then when Form2 is shown again , the Changed event is triggered ! That's strange , of course , because when Form2 is re-shown , it has the check box unchecked (because Form2 was Closed when returning back to Form1) so there is no reason for the event to be triggered (that is , not even during the Initialization method when re-creating the form) ! Moreover , when this event is triggered , it is always in the else part of the code , meaning that it was called with the check box turning unchecked ! But I can't understand how come that ... It's like a ghost checking or uncheking that control ...

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My CheckBox always believes it is unchecked !

    This sounds like attack of the default instance again. Default instances cause as many issues as they solve because people don't know that they're using them and how they differ from an instance you create yourself. First, follow the Blog link in my signature and check out my post on default instances. Once you've done that and you know what they are, go through your code and remove all references to default instances. If you need an instance, always create it yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    501

    Re: My CheckBox always believes it is unchecked !

    I read this topic (which was linked in your signature) :
    http://devcity.net/Articles/94/1/multipleforms.aspx
    and this one :
    http://jmcilhinney.blogspot.com/2009...instances.html
    which was provided by dbasnett .
    To be honest , I still fail to understand why it is useful to use new instances instead of the default instance of a form . But that's not the point .
    The point is I went on and indeed tried using a new instance of Form2 . I was full of hopes that by doing so I will always have a fresh instance of Form2 and therefore I wouldn't face that problem (remember that the problem I described never appears the first time the form is shown , it appears only in the following times) .
    However , to my great surprise , now the problem appears even from the first time Form2 is shown ! As I said , now it is not the default instance of Form2 but a new instance of it , but still the problem persists ... I have other similar forms but none behaves like that . Is there any other way we could find out what's going on ? Maybe a way to find out , at least , who is checking / unchecking the check box (other than the user , that is) ? Because , as I said , if the first time Form2 was closed with the check box being activated , then when Form2 is shown for the second time , the Checked event is triggered like someone activated the check box ! which is not because when a form is created from zero the check box cannot be checked (which is also the way the check box is designed in design time) . It's like someone trying to check the check box before Form2 is (re)created ...

  12. #12
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: My CheckBox always believes it is unchecked !

    In my experience, every time a form is instantiated and shown, the CheckedChanged events fire since the checkboxes are being instantiated and their Checked properties are set to False. As far as I know, this is normal behaviour.

    What you can do is check whether the form is opening when the event fires or the user has actually checked the checkbox. You can do this in various ways, but probably the simplest and the safest way is to set up a Boolean flag which will show whether the form has finished opening or not.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

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