Control array problems! help plz
-
Hi guys: I create a form with a bunch of combo boxes, and i named them with: cbo0,cbo1,cbo2..... I would like to create a function to test if any of them is empty. So i did it this way: Dim combos(6) As Object Dim cnt As Integer Form_Load() Handles MyBase.Load For cnt = 0 To 6 combos(cnt) = "strLand" & cnt.ToString Next End Sub Private Function checkCbos() As Boolean Dim cbos As ComboBox For cnt = 0 To 6 cbos = CType(combos(cnt), ComboBox) If cbos.Text = "" Then errorPro.SetError(cbos, "Do not leave the factor box empty!") End If Next End Function and after all this, i countered a casting error, could anyone helps me to debug this, or have a better idea to validate those combo boxes?thanks very much. Cheers Kevin
-
Hi guys: I create a form with a bunch of combo boxes, and i named them with: cbo0,cbo1,cbo2..... I would like to create a function to test if any of them is empty. So i did it this way: Dim combos(6) As Object Dim cnt As Integer Form_Load() Handles MyBase.Load For cnt = 0 To 6 combos(cnt) = "strLand" & cnt.ToString Next End Sub Private Function checkCbos() As Boolean Dim cbos As ComboBox For cnt = 0 To 6 cbos = CType(combos(cnt), ComboBox) If cbos.Text = "" Then errorPro.SetError(cbos, "Do not leave the factor box empty!") End If Next End Function and after all this, i countered a casting error, could anyone helps me to debug this, or have a better idea to validate those combo boxes?thanks very much. Cheers Kevin
Try something like this. You might have to modifiy the code a little I don't have VS open so I don't think it will compile, but it will give you the right way to go about doing this.
for each item as control in form.controls if item.GetType is GetType(ComboBox) then if item.Text = "" then errorPro.SetError(cbos, "Do not leave the factor box empty!") end if end if next
Mike Lasseter
-
Hi guys: I create a form with a bunch of combo boxes, and i named them with: cbo0,cbo1,cbo2..... I would like to create a function to test if any of them is empty. So i did it this way: Dim combos(6) As Object Dim cnt As Integer Form_Load() Handles MyBase.Load For cnt = 0 To 6 combos(cnt) = "strLand" & cnt.ToString Next End Sub Private Function checkCbos() As Boolean Dim cbos As ComboBox For cnt = 0 To 6 cbos = CType(combos(cnt), ComboBox) If cbos.Text = "" Then errorPro.SetError(cbos, "Do not leave the factor box empty!") End If Next End Function and after all this, i countered a casting error, could anyone helps me to debug this, or have a better idea to validate those combo boxes?thanks very much. Cheers Kevin
Something like this:
Private Function checkCbos() As Boolean Dim myControl As Control For Each myControl In Me.Controls If TypeOf myControl Is ComboBox Then If myControl.Text = "" Then 'Set error provider errorPro.SetError(myControl, "Do not leave the factor box empty!") 'Return False and exit to calling procedure Return False Else 'Clear the error provider errorPro.SetError(myControl, "") End If End If Next myControl 'If code gets to here, no combo's text is "" Return True End Function
Steve -
Something like this:
Private Function checkCbos() As Boolean Dim myControl As Control For Each myControl In Me.Controls If TypeOf myControl Is ComboBox Then If myControl.Text = "" Then 'Set error provider errorPro.SetError(myControl, "Do not leave the factor box empty!") 'Return False and exit to calling procedure Return False Else 'Clear the error provider errorPro.SetError(myControl, "") End If End If Next myControl 'If code gets to here, no combo's text is "" Return True End Function
SteveThanks Mike and Steve,i got the point, that is very helpful.
-
Something like this:
Private Function checkCbos() As Boolean Dim myControl As Control For Each myControl In Me.Controls If TypeOf myControl Is ComboBox Then If myControl.Text = "" Then 'Set error provider errorPro.SetError(myControl, "Do not leave the factor box empty!") 'Return False and exit to calling procedure Return False Else 'Clear the error provider errorPro.SetError(myControl, "") End If End If Next myControl 'If code gets to here, no combo's text is "" Return True End Function
SteveHi Steve, ur code only checks a single combox and returns false if the control text is empty. I add a integer to count, and make sure all the boxes are checked. Add 1 if it is not empty. Private Function checkCbos() As Boolean Dim myControl As Control Dim count As Integer = 0 For Each myControl In Me.Controls If TypeOf myControl Is ComboBox Then If myControl.Text = "" Then 'Set error provider errorPro.SetError(myControl, "Do not leave box empty!") 'Return False and exit to calling procedure Else 'Clear the error provider errorPro.SetError(myControl, "") count += 1 End If End If Next myControl 'If code gets to here, no combo's text is "" If count = 6 Then Return True Else Return False End If End Function
-
Hi Steve, ur code only checks a single combox and returns false if the control text is empty. I add a integer to count, and make sure all the boxes are checked. Add 1 if it is not empty. Private Function checkCbos() As Boolean Dim myControl As Control Dim count As Integer = 0 For Each myControl In Me.Controls If TypeOf myControl Is ComboBox Then If myControl.Text = "" Then 'Set error provider errorPro.SetError(myControl, "Do not leave box empty!") 'Return False and exit to calling procedure Else 'Clear the error provider errorPro.SetError(myControl, "") count += 1 End If End If Next myControl 'If code gets to here, no combo's text is "" If count = 6 Then Return True Else Return False End If End Function
Well, it will check all combo boxes but only return an error for one at a time. Presumably you call checkCbos() in your Save button 'click' procedure? The function will set an error provider on the first combo box that contains "" and then exit the function. If you select an item and then click your Save button again, you will be notified of any further combos containing "". This way, if you added another combo box at any time, the code will still work. The way you have done it is fine but would fail if you added another combo at any time. If it works for you though, that's great! Steve