|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I'm having trouble allowing the checkbox to be unchecked. I have several checkboxs to allow the user to select items, that appear in a textbox. If the change their minds and want to uncheck the box, I need to remove that item in the textbox. I found a work around but its not elegant. I added an additional checkbox to clear all choices and resetting all checkboxes to unchecked state. I disable the checkbox to prevent unchecking.
I think the users would prefer to check or uncheck the individual boxes. Any assistance on this would be greatly appreciated. Current code for 1 of the checkboxs: Private Sub cbGrilledPeppers_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbGrilledPeppers.CheckedChanged tbCondiments.Text = tbCondiments.Text & "> " & "Grilled Peppers" If cbGrilledPeppers.CheckState = CheckState.Checked Then ' If checked, do not allow items to be dragged onto the form. Me.cbGrilledPeppers.Enabled = False End If End Sub Thank you |
|
#2
|
||||
|
||||
|
Hi Whiteelm,
Sorry if this is a bit cumbersome, but I just threw this together to demonstrate one way to achieve this using an array of items and simply deleting/adding the items from/to the array. You can then display the contents of the array in the textbox. To a Form (Form1) add three checkboxes: chkPickles, chkCheese & chkCucumber and one textbox: Text1. Now add the following code: Code:
Option Explicit
'Define an array of n-1 elements, where n is the number of checkboxes
Dim strItems(2) As String
Dim a As Integer
Dim intpos As Integer
Dim intBlank As Integer
Dim found As Boolean
Private Sub chkCheese_Click()
If chkCheese.Value = 1 Then
For a = 0 To UBound(strItems)
If strItems(a) = "Cheese" Then
found = True
intpos = a
Else
If strItems(a) = "X" Then
intBlank = a
End If
End If
Next
If found <> True Then
strItems(intBlank) = "Cheese"
End If
Else
For a = 0 To UBound(strItems)
If strItems(a) = "Cheese" Then strItems(a) = "X"
Next
End If
Text1.Text = ""
For a = 0 To UBound(strItems)
If strItems(a) <> "X" Then Text1.Text = Text1.Text & strItems(a) & " "
Next
found = False
End Sub
Private Sub chkCucumber_Click()
If chkCucumber.Value = 1 Then
For a = 0 To UBound(strItems)
If strItems(a) = "Cucumber" Then
found = True
intpos = a
Else
If strItems(a) = "X" Then
intBlank = a
End If
End If
Next
If found <> True Then
strItems(intBlank) = "Cucumber"
End If
Else
For a = 0 To UBound(strItems)
If strItems(a) = "Cucumber" Then strItems(a) = "X"
Next
End If
Text1.Text = ""
For a = 0 To UBound(strItems)
If strItems(a) <> "X" Then Text1.Text = Text1.Text & strItems(a) & " "
Next
found = False
End Sub
Private Sub chkPickles_Click()
If chkPickles.Value = 1 Then
For a = 0 To UBound(strItems)
If strItems(a) = "Pickles" Then
found = True
intpos = a
Else
If strItems(a) = "X" Then
intBlank = a
End If
End If
Next
If found <> True Then
strItems(intBlank) = "Pickles"
End If
Else
For a = 0 To UBound(strItems)
If strItems(a) = "Pickles" Then strItems(a) = "X"
Next
End If
Text1.Text = ""
For a = 0 To UBound(strItems)
If strItems(a) <> "X" Then Text1.Text = Text1.Text & strItems(a) & " "
Next
found = False
End Sub
Private Sub Form_Load()
found = False
For a = 0 To UBound(strItems)
strItems(a) = "X"
Next
End Sub
Hopefully this will give you some ideas, Rob |
|
#3
|
||||
|
||||
|
To make it a little bit more readable you can make the items appear on different lines of the textbox by setting it's multiline property to True, you can now change the line of code at the end of each checkboxes Click event to add a Carriage Return Line Feed character to the line:
Code:
Private Sub chkCheese_Click()
If chkCheese.Value = 1 Then
For a = 0 To UBound(strItems)
If strItems(a) = "Cheese" Then
found = True
intpos = a
Else
If strItems(a) = "X" Then
intBlank = a
End If
End If
Next
If found <> True Then
strItems(intBlank) = "Cheese"
End If
Else
For a = 0 To UBound(strItems)
If strItems(a) = "Cheese" Then strItems(a) = "X"
Next
End If
Text1.Text = ""
For a = 0 To UBound(strItems)
If strItems(a) <> "X" Then Text1.Text = Text1.Text & strItems(a) & vbCrLf
Next
found = False
End Sub
etc..
|
|
#4
|
|||
|
|||
|
Thank you for the quick reply. I will try your code today. I will post the final code after testing. Appreciate the assistance.
Larry Quote:
|
|
#5
|
|||
|
|||
|
Thank you for your assistance. It got me thinking. I re-wrote my code as follows to provide me the required text as each checkbox is checked. Each checkbox has its own textbox, then I combine the results of all textbox entries for a single text string for the database field:
Private Sub cbGrilledOnions_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbGrilledOnions.CheckedChanged tbGrilledOnions.Text = "Grilled Onions" If cbGrilledOnions.CheckState = CheckState.Checked Then ' If checked, do not allow items to be dragged onto the form. 'Me.cbGrilledOnions.Enabled = False ElseIf cbGrilledOnions.CheckState = CheckState.Unchecked Then tbGrilledOnions.Text = " " End If End Sub Quote:
|
|
#6
|
||||
|
||||
|
Larry,
I'm glad you've figured it out, it makes a lot of sense doing it that way, just concatenating the contents of all the textboxes!! Sorry if mine was a bit of a long winded way of going about it, at least it gave you some inspiration though!! Regards, Rob |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Checkbox |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|