Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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) = ""

  • Leaders
Posted

   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 ?

Posted

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

  • Leaders
Posted

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

Posted (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 by PlausiblyDamp
  • 2 weeks later...
Posted (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 by PlausiblyDamp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...