I am trying to do something and not able to get to where I need to go. I have 12 checkboxes on a form. 4 for "And", 4 for "Or" and 4 for "Exclude".
I am trying to check to see if all checkboxes that contain the word "Exclude" in the control name are checked. If they are ALL checked, to show a messagebox. I am getting there but when I try to loop through the controls to check the name, I am getting a blue line under the code that says:
Class 'System.Windows.Form.Control' cannot be indexed because it has no default property.
Private excludeCheckBoxes() As Elegant.Ui.CheckBox = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.excludeCheckBoxes = New Elegant.Ui.CheckBox() {chkResolutionTypeExclude, _
chkDateToFromExclude, _
chkClientNameExclude, _
chkClientPhoneNumberExclude}
End Sub
Private Function AreAllExcludeCheckBoxesChecked() As Boolean
Dim result As Boolean = True
For Each chkBox As Elegant.Ui.CheckBox In Me.excludeCheckBoxes
If chkBox.Checked = False Then
result = False
Exit For
End If
Next
Return result
End Function
<CLSCompliant(True)> Private Sub DisallowAllExclude()
If Me.AreAllExcludeCheckBoxesChecked() = True Then
MessageBox.Show("Hey, you can't exclude every options like that.")
End If
End Sub
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -
Private excludeCheckBoxes() As Elegant.Ui.CheckBox = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.excludeCheckBoxes = New Elegant.Ui.CheckBox() {chkResolutionTypeExclude, _
chkDateToFromExclude, _
chkClientNameExclude, _
chkClientPhoneNumberExclude}
End Sub
Private Function AreAllExcludeCheckBoxesChecked() As Boolean
Dim result As Boolean = True
For Each chkBox As Elegant.Ui.CheckBox In Me.excludeCheckBoxes
If chkBox.Checked = False Then
result = False
Exit For
End If
Next
Return result
End Function
<CLSCompliant(True)> Private Sub DisallowAllExclude()
If Me.AreAllExcludeCheckBoxesChecked() = True Then
MessageBox.Show("Hey, you can't exclude every options like that.")
End If
End Sub
Invalid Operation Exception. Frustrating. I can get it to work without an issue when using an If block but I am sure there is an easier way of doing this.
The last error you got is a null reference exception. Do you know which line in your code that it causes it?
The code I posted will not cause it provided that you have the checkboxes as named on your form. And If you don't, it won't compile and thus you can't get an exception from it.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -
The last error you got is a null reference exception. Do you know which line in your code that it causes it?
The code I posted will not cause it provided that you have the checkboxes as named on your form. And If you don't, it won't compile and thus you can't get an exception from it.
The control names are correct. I made sure of it and double checked. The error happens on the loading of the form in the button click event.
what sort of size is your project? could you upload it (remove any confidential information)?
i am about to send you a PM with the URL i uploaded the project to. i am using two custom controls on the forms so you will need to install them. ill explain more in the PM.
Here is my go at it, I have used this type of loop before and it has worked very well for me (well enough that I include a more enhanced variation of it with my base dll in all projects):
Code:
Private Function All_Exclude_checked(ByVal ctrl As Control) As Boolean
'the thought process behind this is to send your panel containing the controls
' in as the initial control and allow this to loop. If there are any sub controls
' then they are handled with the loop below. Otherwise, it will check the control
' itself. if it is the type of control you are looking for then check the text. If
' the text is NOT "Excluded" then return false. This would mean that if ANY other
' checkbox was marked then it would return false.
If CType(ctrl, Control).HasChildren = True Then
For Each child As Control In ctrl.Controls
All_Exclude_checked(child)
Next
End If
If TypeOf ctrl Is CheckBox Then
If CType(ctrl, CheckBox).Text <> "Excluded" Then
Return False
Else
Return True
End If
End If
End Function
Hope that this helps some!
D
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you.
Please remember to mark threads as closed if your issue has been resolved.
After looking back over the code, I noticed that I REALLY messed up on it. Here is a revised version that works - with checkboxes anyways!!
Code:
Private trigger As Boolean = True
Private Function All_Exclude_checked(ByVal ctrl As Control) As Boolean
If CType(ctrl, Control).HasChildren = True Then
For Each child As Control In ctrl.Controls
All_Exclude_checked(child)
Next
ElseIf TypeOf ctrl Is CheckBox Then
If CType(ctrl, CheckBox).Checked = True Then
If CType(ctrl, CheckBox).Text <> "Excluded" Then
trigger = False
End If
End If
End If
Return trigger
End Function
I am using an external variable to keep track of whether another box was checked. Be SURE to reset that variable (trigger) BEFORE you run it again because it will retain the last value it was set to!!!
Again, sorry for the sloppiness - I really need to watch when I am copying and pasting and trimming stuff down!!!
D
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you.
Please remember to mark threads as closed if your issue has been resolved.
OK, I am using FxCop from Microsoft to scan my built exe and it is giving me an error. It is saying:
'Ctrl', a parameter, is cast to type 'CheckBox' multiple times in method 'FrmRecords.AllExcludedChecked(Control)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant isint instruction."
So as per the topic suggested, I went into my code and changed it to the following and it still says the same thing when I rebuild the project and scan it with FxCop.
vb Code:
<CLSCompliant(True)> Private Function AllExcludedChecked(ByVal Ctrl As Control) As Boolean
Dim Chk As Elegant.Ui.CheckBox = DirectCast(Ctrl, Elegant.Ui.CheckBox)
If Ctrl.HasChildren = True Then
For Each Child As Control In Ctrl.Controls
AllExcludedChecked(Child)
Next Child
ElseIf TypeOf Ctrl Is Elegant.Ui.CheckBox Then
If Chk.Checked = True Then
If Chk.Text = "Exclude" Then
Trigger = True
Else
Trigger = False
End If
End If
End If
Return Trigger
End Function
Any ideas?
Last edited by BrailleSchool; Jan 30th, 2011 at 09:27 AM.
Public Class Form1
Dim ctlDict As New Dictionary(Of String, Control)
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'build a dictionary of all controls
Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
Do Until ctl Is Nothing
ctlDict.Add(ctl.Name, ctl)
ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
Loop
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'query the dictionary of controls
Dim foo As IEnumerable(Of Generic.KeyValuePair(Of String, Control))
foo = From de In ctlDict _
Where de.Key.Contains("Exclude") AndAlso _
TypeOf de.Value Is CheckBox AndAlso _
CType(de.Value, CheckBox).Checked Select de
'the count
Debug.WriteLine(foo.Count.ToString)
End Sub
End Class
Last edited by dbasnett; Jan 30th, 2011 at 10:27 AM.
Public Class Form1
Dim ctlDict As New Dictionary(Of String, Control)
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'build a dictionary of all controls
Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
Do Until ctl Is Nothing
ctlDict.Add(ctl.Name, ctl)
ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
Loop
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'query the dictionary of controls
Dim foo As IEnumerable(Of Generic.KeyValuePair(Of String, Control))
foo = From de In ctlDict _
Where de.Key.Contains("Exclude") AndAlso _
TypeOf de.Value Is CheckBox AndAlso _
CType(de.Value, CheckBox).Checked Select de
'the count
Debug.WriteLine(foo.Count.ToString)
End Sub
End Class