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. for next loop [modified]

for next loop [modified]

Scheduled Pinned Locked Moved Visual Basic
visual-studiohelptutorialquestion
7 Posts 5 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.
  • R Offline
    R Offline
    RoedRunner
    wrote on last edited by
    #1

    While I understand how to use a for next loop, I cannot work the integer (eg. 1 to 10) into the name of an object. For instance, I would like to create a for next loop for the following code ckfld1.Checked = False ckfld2.Checked = False ckfld3.Checked = False ckfld4.Checked = False ckfld5.Checked = False ckfld6.Checked = False ckfld7.Checked = False ckfld8.Checked = False ckfld9.Checked = False ckfld10.Checked = False Could someone show me how to do this? I tried the following but it does not work. For x As Integer = 1 To 10 dim c as new checkbox c.name = "ckfld" & x c.checked = false Next Any help is appreciated. I am using vs 2005. Thanks Marc -- modified at 23:05 Monday 11th June, 2007

    M T T 3 Replies Last reply
    0
    • R RoedRunner

      While I understand how to use a for next loop, I cannot work the integer (eg. 1 to 10) into the name of an object. For instance, I would like to create a for next loop for the following code ckfld1.Checked = False ckfld2.Checked = False ckfld3.Checked = False ckfld4.Checked = False ckfld5.Checked = False ckfld6.Checked = False ckfld7.Checked = False ckfld8.Checked = False ckfld9.Checked = False ckfld10.Checked = False Could someone show me how to do this? I tried the following but it does not work. For x As Integer = 1 To 10 dim c as new checkbox c.name = "ckfld" & x c.checked = false Next Any help is appreciated. I am using vs 2005. Thanks Marc -- modified at 23:05 Monday 11th June, 2007

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

      Try this instead:

      For Each Box As CheckBox In Me.Controls
      Box.Checked = True
      Next

      This finds all CheckBoxes in your form and checks them. You can also do the same using a Panel or GroupBox.


      Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

      R 1 Reply Last reply
      0
      • M MatrixCoder

        Try this instead:

        For Each Box As CheckBox In Me.Controls
        Box.Checked = True
        Next

        This finds all CheckBoxes in your form and checks them. You can also do the same using a Panel or GroupBox.


        Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

        R Offline
        R Offline
        RoedRunner
        wrote on last edited by
        #3

        The problem is that there will be checkboxes that should not be affected by the for next loop, which is why I was trying to accomplish this the other way. Is it possible to do it the other way?

        M 1 Reply Last reply
        0
        • R RoedRunner

          The problem is that there will be checkboxes that should not be affected by the for next loop, which is why I was trying to accomplish this the other way. Is it possible to do it the other way?

          M Offline
          M Offline
          MatrixCoder
          wrote on last edited by
          #4

          In that case you would put the CheckBoxes you do want to be checked in a Panel.


          Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

          1 Reply Last reply
          0
          • R RoedRunner

            While I understand how to use a for next loop, I cannot work the integer (eg. 1 to 10) into the name of an object. For instance, I would like to create a for next loop for the following code ckfld1.Checked = False ckfld2.Checked = False ckfld3.Checked = False ckfld4.Checked = False ckfld5.Checked = False ckfld6.Checked = False ckfld7.Checked = False ckfld8.Checked = False ckfld9.Checked = False ckfld10.Checked = False Could someone show me how to do this? I tried the following but it does not work. For x As Integer = 1 To 10 dim c as new checkbox c.name = "ckfld" & x c.checked = false Next Any help is appreciated. I am using vs 2005. Thanks Marc -- modified at 23:05 Monday 11th June, 2007

            T Offline
            T Offline
            Tirthadip
            wrote on last edited by
            #5

            Firstly you have many checkboxes....I want to know whether you have maintained the same convention while naming your checkbox control....i.e ckfld1,ckfld2,ckfld3,ckfld4...???? If a specific set of checkboxes are to be unchecked then matrixcoder has rightly said to put your checkboxes in a Panel.Then you can write For Each ctrl As Control In Panel1.Controls CType(ctrl,CheckBox).Checked=False Next Other wise you have to Split the ID of your checkboxes to have the number....you can use Substring method of string Class to do that. After that you can use that number in a For-Each loop to uncheck your CheckBoxes.But it should be a second choice I think.

            Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist

            D 1 Reply Last reply
            0
            • R RoedRunner

              While I understand how to use a for next loop, I cannot work the integer (eg. 1 to 10) into the name of an object. For instance, I would like to create a for next loop for the following code ckfld1.Checked = False ckfld2.Checked = False ckfld3.Checked = False ckfld4.Checked = False ckfld5.Checked = False ckfld6.Checked = False ckfld7.Checked = False ckfld8.Checked = False ckfld9.Checked = False ckfld10.Checked = False Could someone show me how to do this? I tried the following but it does not work. For x As Integer = 1 To 10 dim c as new checkbox c.name = "ckfld" & x c.checked = false Next Any help is appreciated. I am using vs 2005. Thanks Marc -- modified at 23:05 Monday 11th June, 2007

              T Offline
              T Offline
              Thomas Stockwell
              wrote on last edited by
              #6

              What you are doing is creating another object that does not have the same name, checked value, any of the information that the checkbox already on the form has. What you should do is loop through all the controls of the form and then check the name to the naming convention you use. Check to see if the last number is the correct number, and if so you should set the appropriate check value for that object.

              Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

              1 Reply Last reply
              0
              • T Tirthadip

                Firstly you have many checkboxes....I want to know whether you have maintained the same convention while naming your checkbox control....i.e ckfld1,ckfld2,ckfld3,ckfld4...???? If a specific set of checkboxes are to be unchecked then matrixcoder has rightly said to put your checkboxes in a Panel.Then you can write For Each ctrl As Control In Panel1.Controls CType(ctrl,CheckBox).Checked=False Next Other wise you have to Split the ID of your checkboxes to have the number....you can use Substring method of string Class to do that. After that you can use that number in a For-Each loop to uncheck your CheckBoxes.But it should be a second choice I think.

                Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist

                D Offline
                D Offline
                Duane in Japan
                wrote on last edited by
                #7

                Do you actually need to loop through the same set of chkboxes, the exact same set each time or is it a random selection? Private Sub TextBox29_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox29.Validating Dim Box26 As Double = 0 Dim Box29 As Double = 0 TextBox29.SelectAll() Dim response As MsgBoxResult If TextBox29.Text.Length = 0 Then MsgBox(("A 'B5' value is required." & vbCrLf & _ "Would you like to add more input"), MsgBoxStyle.YesNo) If response = MsgBoxResult.Yes Then e.Cancel = True Else e.Cancel = False TextBox28.Text = 0 TextBox31.Text = 0 TextBox32.Focus() End If ElseIf Not IsNumeric(TextBox29.Text) Then MsgBox("This 'B5' value is not numeric.", MsgBoxStyle.Exclamation) e.Cancel = True ElseIf CheckBox10.Checked Then e.Cancel = False Else Box26 = CDbl(TextBox26.Text) Box29 = CDbl(TextBox29.Text) TextBox27.Text = CStr(Box29 + 1) TextBox28.Text = CStr(Box26 - Box29) End If 'If Box29 <= 0 Then ' MsgBox("Your 'B5' value must be greater than zero.", MsgBoxStyle.Exclamation) ' e.Cancel = True 'End If TextBox35.Clear() TextBox36.Clear() TextBox37.Clear() TextBox38.Clear() TextBox39.Clear() End Sub Notice above: ElseIf CheckBox10.Checked Then e.Cancel = False I have a total of 10 chkboxes and I will check them at complete random depending on the current scenario, and the scenarios are never the same. Later down in the code I just turn off all chkboxes but I could program only certain ones if needed. This is a NOOBs rendition. Private Sub TextBox39_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox39.Validating Dim Box35 As Double = (TextBox35.Text) Dim Box36 As Double = (TextBox36.Text) Dim Box37 As Double = (TextBox37.Text) Dim Box38 As Double = (TextBox38.Text) TextBox39.Text = CStr((Box35 + Box36) + (Box37 - Box38)) If CheckBox1.Checked Then TextBox4.Text = TextBox39.Text TextBox8.Focus() End If If CheckBox2.Checked Then TextBox7.Text = TextB

                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