Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Control array problems! help plz

Control array problems! help plz

Scheduled Pinned Locked Moved Visual Basic
helpdata-structuresdebuggingquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kevindotnet
    wrote on last edited by
    #1

    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

    M S 2 Replies Last reply
    0
    • K kevindotnet

      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

      M Offline
      M Offline
      mr_lasseter
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K kevindotnet

        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

        S Offline
        S Offline
        Stephen McGuire
        wrote on last edited by
        #3

        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

        K 2 Replies Last reply
        0
        • S Stephen McGuire

          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

          K Offline
          K Offline
          kevindotnet
          wrote on last edited by
          #4

          Thanks Mike and Steve,i got the point, that is very helpful.

          1 Reply Last reply
          0
          • S Stephen McGuire

            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

            K Offline
            K Offline
            kevindotnet
            wrote on last edited by
            #5

            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

            S 1 Reply Last reply
            0
            • K kevindotnet

              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

              S Offline
              S Offline
              Stephen McGuire
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups