hazejean Posted July 19, 2003 Posted July 19, 2003 can someone help? I need to check to see of a checkbox is checked. If it is checked I perform one function, if not check I perform another. I have tried checkstate, checked etc I must be missiing something. In 6.0 it was a very simple command. If check1.Value = 1 Then text(1) = verb1 Else If check1.Value = 0 Then text(1) = "" Quote
Leaders dynamic_sysop Posted July 19, 2003 Leaders Posted July 19, 2003 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If CheckBox1.CheckState = CheckState.Checked Then '/// do stuff Else '/// do other stuff cuz it aint checked. End If End Sub like that you mean ? Quote
hazejean Posted July 19, 2003 Author Posted July 19, 2003 the code I tried was as follows: ' test if check box checked...if true move verb to text For k = 1 To noverbs If Mycheck(k).CheckState = CheckState.Checked Then text(k) = verb(k) Else text(k) = "" End If Next k of course it did not work. Mycheck is an array of checkboxes. noverbs is number of checkboxes on a form I don't comprehend the need for button...etc? Can you explain? Thanks Quote
*Experts* mutant Posted July 19, 2003 *Experts* Posted July 19, 2003 That should work, but you can also try this: If MyCheck(k).Checked Then 'it is checked Else 'it is not checked End If Quote
hazejean Posted July 19, 2003 Author Posted July 19, 2003 also tried that code. Is it possible there are bugs in my VB version? Thanks Quote
Leaders dynamic_sysop Posted July 19, 2003 Leaders Posted July 19, 2003 are you creating these checkboxes at design time or adding them as new checkboxes at runtime? Quote
Leaders dynamic_sysop Posted July 19, 2003 Leaders Posted July 19, 2003 just incase you are creating them at runtime here's an example of how to deal with the checked states : Private WithEvents CB As CheckBox Private chk(3) As CheckBox ' /// below your "windows generated code area"^^^ Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click chk(1) = New CheckBox() chk(1).Location = New System.Drawing.Point(304, 30) chk(1).Visible = True MyBase.Controls.Add(chk(1)) AddHandler chk(1).Click, AddressOf CB_Click End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If Not chk(1) Is Nothing Then If chk(1).Checked = True Then MessageBox.Show("it's checked!") Else MessageBox.Show("it's NOT checked!") End If End If End Sub Private Sub CB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CB.Click '/// handle all checkboxes created at runtime in 1 sub. End Sub Quote
hazejean Posted July 19, 2003 Author Posted July 19, 2003 trying to create at run time. The creation part works. Everything working except the logic to detect if they are checked. Quote
hazejean Posted July 19, 2003 Author Posted July 19, 2003 (edited) code to genertae check boxes This is my code to generate a variable number of text boxes. It seem sto work fine? For i = 1 To noverbs Mycheck(i) = New CheckBox Mycheck(i).Location = New Point(1, 15 * i) Controls.Add(Mycheck(i)) Mycheck(i).Text = verb(i) Next i Edited February 19, 2007 by PlausiblyDamp Quote
dpifer Posted August 3, 2003 Posted August 3, 2003 (edited) Here is another example on how to handle the click event of the checkbox. Private Sub addCheckBox(ByVal iCheck As Integer, ByVal iTB As Integer, ByVal sDesc As String, ByVal bRow As Boolean, ByVal sSysType As String) Dim chkBox As New CheckBox() Dim X As Integer Dim temp As Integer Dim idx As Integer Dim obj As Object Select Case iCheck Case 0 X = 16 Case 1 X = 128 Case 2 X = 240 Case 3 X = 352 Case 4 X = 464 End Select If bRow Then temp += 32 Y = Y + temp Else temp = Y End If chkBox.Name = "CheckBox_" & sDesc & "_" & CStr(iCheck) chkBox.Location = New System.Drawing.Point(X, Y) chkBox.Text = sDesc chkBox.Tag = iTB Select Case sSysType Case "ROS" Me.pnlUsrPref_ROS.Controls.AddRange(New System.Windows.Forms.Control() {chkBox}) Case "Exam" Me.pnlUsrPref_Exam.Controls.AddRange(New System.Windows.Forms.Control() {chkBox}) End Select 'Handles the click event for the Ros and Exam checkboxes AddHandler chkBox.Click, AddressOf CheckBox_UsrPrefs End Sub Private Sub CheckBox_UsrPrefs(ByVal sender As System.Object, ByVal e As EventArgs) If CType(sender, CheckBox).Checked Then aryUsrPrefs.Add(CInt(CType(sender, CheckBox).Tag)) 'Add the tag to the array End If If CType(sender, CheckBox).Checked = False Then Dim idx As Integer Dim iTag As Integer If aryUsrPrefs.Count > 0 Then iTag = CInt(CType(sender, CheckBox).Tag) 'Get the tag of the system idx = aryUsrPrefs.IndexOf(iTag) 'Get the location of the tag in the array aryUsrPrefs.RemoveAt(idx) 'Remove it from the array End If End If End Sub Edited February 19, 2007 by PlausiblyDamp Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.