Basic Checkbox question

djohnston

Active member
Joined
Jun 1, 2006
Messages
33
Programming Experience
Beginner
OK this sounds simple and all but I am a serious noob when it comes to programming. So lets say I have a checkbox, then 3 textboxes below it. When i click the checkbox i want it to set the 3 textboxes active. So when I hit the sumbit button it knows to pull data from those to be inputted into the database. But when i dont click the checkbox i dont want it to read the data. How would i go about doing this.

Sincerely
David
 
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button.Click
        If CheckBox.Checked = True Then
'code to input to database
        End If
    End Sub

    Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox.CheckedChanged

        If CheckBox.Checked = True Then
            TextBox.Enabled = True
        Else
            TextBox.Enabled = False
        End If

    End Sub
 
to modify craig's code a little you could:
VB.NET:
Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox.CheckedChanged
  Textbox1.Enabled = CheckBox.Checked
  Textbox2.Enabled = CheckBox.Checked
  Textbox3.Enabled = CheckBox.Checked
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
  If CheckBox.Checked = True Then
    'code to input to database
  Else
    'Any other database code
  End If
End Sub
 
Thanks for the help

Thanks alot Im getting that much closer to finishing this app.
I'm about 3/4 done with my first app ever, which was stopped so i could work on this other one so ya im pretty noob to this. But thank alot
David.
 
Yes Craig, as a tip. If you have a field, property, or function that is a boolean type it can be used directly as the conditional statement. Take for example a slightly more cleaner version of the previous code in the If Then Statement.

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
If [COLOR=red]CheckBox.Checked = True[/COLOR] Then
'code to input to database
Else
'Any other database code
End If
End Sub
 
 
 
'same thing but remove the = True
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
If [COLOR=blue]CheckBox.Checked[/COLOR] Then
'code to input to database
Else
'Any other database code
End If
End Sub

it's the same thing w/o the = true. So in other words you place the boolean directly in place as the conditional result.

All the program is looking for is True or False so basically it looks like this to the computer with the first routine....

If (True) = True then....

just take the condition away when using booleans.

If (True) then

Checkbox.checked returns True if checked and False if not. see what I mean? I think you get it, if not lemme know and i can show you ways to cut many logical operations in half b/c I used to code the same way you were thinking.
 
That is good advice but before every person reading this thread goes and re-writes their app because they think that adding '= True' will incurr a performance hit. You can all stop because it doesn't. It's just less wear and tear on your fingers because there is less typing.
 
EDITED DUE TO WRONG AND MISLEADING INFO: I APOLOGIZE. There is no performance hit, vis is correct. if you place = 'True' it never get's compiled. It's just like adding a remark to your code. The IL never sees it.
 
Last edited:
Thats awesome but one more question

I understand the basic IF else statement but I need like
VB.NET:
if checkbox36.checked then 
'this 
if checkbox36.checked and checkbox37.checked then 
'this 
if checkbox 36.checked and checkbox37.checked and checkbox38.checked then 
'this.
end if

I dont think i was that clear on it before just wondering how I would go about doing that.
 
Last edited by a moderator:
You will get the first result if cb36 is checked no matter the combination as long as it's checked. The second the same as long as 36 and 37 is checked no matter the combination and the 36, 37, and 38 if all three are checked.


VB.NET:
[LEFT]if checkbox36.checked and (not checkbox37.checked) and (not checkbox38.checked) then 
'this 
if checkbox36.checked and checkbox37.checked and (Not checkbox38.checked) then 
'this 
if checkbox 36.checked and checkbox37.checked and checkbox38.checked then 
'this.
end if[/LEFT]


I am on my way to work, I'm not sure if that's the result you wanted, you may have very well been looking for actual response you have listed above. Either way, if you have many checkboxes it would be much easier probably to use a loop of some sort.


If you let me know exactly what you want to accomplish I will help you get those results, unless again, you have already accomplished it and was just clarifying.
 
Trust me there is no performance hit for putting '= True' The first place i read this was in my trusty old book by Francesco Baleana. The principle is that you not are adding an extra condition but putting '= true' the condition is either true or false the compiler evaluates the condition and is ignored if it sees '= true' Do the tests yourself and check it out

You've got to remeber that 'checked' is a function that returns a boolean variable. The Function runs and and 'true' or 'false' variable is returned. I hardly think then that the compiler will go off somewhere else and check if 'true = true' No, to code this...

If CheckBox.Checked

Is exactly the same as coding this....

If CheckBox.Checked = True


Think about it. CheckBox.checked? What the hell does that mean? is what the compiler will be thinking if the good people at microsoft hadn't told it what to do. It knows that when you say checkbox.checked that you are asking for it to be true which is exactly the same as literally asking if it is true. Its just a shortcircuit probably using some form of shorthand reflection.
 
Last edited:
Yes, you are correct.... I have broken down the IL compiled code with a simple project of my own. I used only two lines of code to compare and compiled it both ways, once with = 'True' and the other without it. When compiled through the IL it is the same, therefore = 'True' is dropped and the compiler dosn't see it.

I stand corrected with the performance hit, in .NET anyways. Basically it dosn't matter if you put it there or not it never get's compiled. So

If CheckBox1.checked = True then....

and

If CheckBox1.checked then....

will both become compiled the same way.

With that said, you said "CheckBox.checked? What the hell does that mean? is what the compiler will be thinking if the good people at microsoft hadn't told it what to do."

That is not true. Booleans are compiled into numbers, basically -1 being true and 0 being false, or any non 0 number being true, 0 always being false. With this in mind any function returning a boolean is only returning -1 or 0 to the compiler.

I wrote this post over b/c i went on a tangent thinking I was right with the performance hit when I was only half right. Instead I learned a lil somthing new myself, which I am thankful for, and I hope maybe I have still brought some nice information to the table. Here are some links to understand the MS view point on booleans and another site breaking booleans down into charts. Also, check out the information for Short Circuiting to see how that works. E-mail or msg me if you would like the few simple steps to view the IL compiled code. Not too hard to follow.

MS break on booleans..
http://msdn2.microsoft.com/en-us/library/wts33hb3.aspx

Great Great few pages to learn tons about booleans and how computers use them
http://computer.howstuffworks.com/boolean.htm

Good page on with some Short Circuiting info...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconlogicaloperators.asp
 
Last edited:
Back
Top