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. Execute string command?

Execute string command?

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
8 Posts 4 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.
  • S Offline
    S Offline
    sundayschool
    wrote on last edited by
    #1

    I have 39 buttons on a form (numbered 1-39) and I want to randomly assign text values to those buttons when it loads. So I have a sortedlist object with the values I want to assign. However, I want to use a single event (like the form load event) to assign the text string to the buttons. In this load event, I want to use a looping structure like "while" to assign the text from the sortedlist to each button. The only way I could find to do this was to generate the button identifier as a string. For example: string = "button"&counter&".text=x.getbyindex(counter)" x is my sortedlist instance. Counter in this case is the counter on my loop. So as it loops the 1st time, it sets button1.text = the first instance in the sortedlist. The second loop sets button2 and so on. If there is a better way to do this, I am all ears. However, even if there is a better way, I would still like to know if VB has an execute string command. Any help is much appreciated.:) scout12

    P 1 Reply Last reply
    0
    • S sundayschool

      I have 39 buttons on a form (numbered 1-39) and I want to randomly assign text values to those buttons when it loads. So I have a sortedlist object with the values I want to assign. However, I want to use a single event (like the form load event) to assign the text string to the buttons. In this load event, I want to use a looping structure like "while" to assign the text from the sortedlist to each button. The only way I could find to do this was to generate the button identifier as a string. For example: string = "button"&counter&".text=x.getbyindex(counter)" x is my sortedlist instance. Counter in this case is the counter on my loop. So as it loops the 1st time, it sets button1.text = the first instance in the sortedlist. The second loop sets button2 and so on. If there is a better way to do this, I am all ears. However, even if there is a better way, I would still like to know if VB has an execute string command. Any help is much appreciated.:) scout12

      P Offline
      P Offline
      Pablo ar
      wrote on last edited by
      #2

      Scout12: 1st off, there is NO WAY to execute a command like the one you are trying... Ok, now to the solution of your problem: What you really have is an array of controls... ever heard of that? You have a full set of controls, that are all of the same class, and mostly, do the same thing. So, the best way to go is, "Name them all the same, and distinguish them by an identifying number within the 'name' definition". If you know nothing about working with arrays, you better start studying about it, because it is an actual codeing standard procedure, valuable for any programming language. Now, concretely, you must select all the command buttons and:

      • At the name property, put "cmdMassReCaption"
      • At the index property, put numbers 0...39
        and on the form_load, use a For...Next block structure, to peruse through all the controls that are part of the array: For i = 0 To 39    cmdMassReCaption(i).Caption = lstTheNamingList.List(i) Next i So, you'll access every single one of the command buttons by using the same name for each of them, but you will also use a unique (i) indexing number. Well, hope this is an immediate solution to your problem with buttons, but I profusely suggest you start studying about arrays, it's something you just cant't miss. Kindly, Pablo. ----------------------------------------------------------------------------- We live to code, this is our religion.
      S D 2 Replies Last reply
      0
      • P Pablo ar

        Scout12: 1st off, there is NO WAY to execute a command like the one you are trying... Ok, now to the solution of your problem: What you really have is an array of controls... ever heard of that? You have a full set of controls, that are all of the same class, and mostly, do the same thing. So, the best way to go is, "Name them all the same, and distinguish them by an identifying number within the 'name' definition". If you know nothing about working with arrays, you better start studying about it, because it is an actual codeing standard procedure, valuable for any programming language. Now, concretely, you must select all the command buttons and:

        • At the name property, put "cmdMassReCaption"
        • At the index property, put numbers 0...39
          and on the form_load, use a For...Next block structure, to peruse through all the controls that are part of the array: For i = 0 To 39    cmdMassReCaption(i).Caption = lstTheNamingList.List(i) Next i So, you'll access every single one of the command buttons by using the same name for each of them, but you will also use a unique (i) indexing number. Well, hope this is an immediate solution to your problem with buttons, but I profusely suggest you start studying about arrays, it's something you just cant't miss. Kindly, Pablo. ----------------------------------------------------------------------------- We live to code, this is our religion.
        S Offline
        S Offline
        sundayschool
        wrote on last edited by
        #3

        I still have some questions on this one if you don't mind. As my example, using the visual designer, I created 2 buttons. Q1) I changed the name property on 2 button controls to cmdMassRecaption. However, there is no index property in the property grid. There is an image index and a tab index but no index. Can you clarify this? Q2) By placing cmdMassRecaption in the name field for both buttons, the code generator made the following declarations Private cmdMassReCaption As System.Windows.Forms.Button Private cmdMassReCaption As System.Windows.Forms.Button This would not compile because the second statement produces and error stating that it is already declared. Do I have the naming syntax correct? Q3) I searched my .net Framework documentation and can't find a reference to cmdMassRecaption anywhere. How can I find out more about this button and coding standards in the VB language? I appreciate your help. scout12

        D 1 Reply Last reply
        0
        • S sundayschool

          I still have some questions on this one if you don't mind. As my example, using the visual designer, I created 2 buttons. Q1) I changed the name property on 2 button controls to cmdMassRecaption. However, there is no index property in the property grid. There is an image index and a tab index but no index. Can you clarify this? Q2) By placing cmdMassRecaption in the name field for both buttons, the code generator made the following declarations Private cmdMassReCaption As System.Windows.Forms.Button Private cmdMassReCaption As System.Windows.Forms.Button This would not compile because the second statement produces and error stating that it is already declared. Do I have the naming syntax correct? Q3) I searched my .net Framework documentation and can't find a reference to cmdMassRecaption anywhere. How can I find out more about this button and coding standards in the VB language? I appreciate your help. scout12

          D Offline
          D Offline
          Dennis C Dietrich
          wrote on last edited by
          #4

          sundayschool wrote: However, there is no index property in the property grid. I guess Pablo assumed that you use VBA/VB 6. The suggested solution however will now work with VB .NET (for more details see Control Array Changes in Visual Basic .NET[^]). Best regards Dennis

          1 Reply Last reply
          0
          • P Pablo ar

            Scout12: 1st off, there is NO WAY to execute a command like the one you are trying... Ok, now to the solution of your problem: What you really have is an array of controls... ever heard of that? You have a full set of controls, that are all of the same class, and mostly, do the same thing. So, the best way to go is, "Name them all the same, and distinguish them by an identifying number within the 'name' definition". If you know nothing about working with arrays, you better start studying about it, because it is an actual codeing standard procedure, valuable for any programming language. Now, concretely, you must select all the command buttons and:

            • At the name property, put "cmdMassReCaption"
            • At the index property, put numbers 0...39
              and on the form_load, use a For...Next block structure, to peruse through all the controls that are part of the array: For i = 0 To 39    cmdMassReCaption(i).Caption = lstTheNamingList.List(i) Next i So, you'll access every single one of the command buttons by using the same name for each of them, but you will also use a unique (i) indexing number. Well, hope this is an immediate solution to your problem with buttons, but I profusely suggest you start studying about arrays, it's something you just cant't miss. Kindly, Pablo. ----------------------------------------------------------------------------- We live to code, this is our religion.
            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Control arrays only exist in VB6 and below, not in VB.NET... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            P 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Control arrays only exist in VB6 and below, not in VB.NET... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              P Offline
              P Offline
              Pablo ar
              wrote on last edited by
              #6

              hey Dennis, Rage... you got it right guys, I was talking VB6 here, but I didn't know there is no control array on .Net, why is that? and as to you sundayschool... guess you ain't going to school on sundays anyway! hehe, joking... cmdMassReCaption is a way to name a button. I mean, the control IS A BUTTON but we give it a name we can identify it for what it does, or related to what we want to do with it. That way of naming controls and components, with three lowercase letters for a shorts of the control type, and then the name, is the GNU standard. try to keep it that way, cause everybody codes that way and when you share code, is easier for everybody else to read it and make changes to it. So: cmdMassReCaption ^ ^--------------- The name: Mass (Re-) Caption. |---- The type: cOmMANd. Ok, now get VB6 and get on with studying control arrays, cause you still need it, don't think 'you don't need to know that, cause it's not used anymore'!!! Kindly, Pablo.

              D D 2 Replies Last reply
              0
              • P Pablo ar

                hey Dennis, Rage... you got it right guys, I was talking VB6 here, but I didn't know there is no control array on .Net, why is that? and as to you sundayschool... guess you ain't going to school on sundays anyway! hehe, joking... cmdMassReCaption is a way to name a button. I mean, the control IS A BUTTON but we give it a name we can identify it for what it does, or related to what we want to do with it. That way of naming controls and components, with three lowercase letters for a shorts of the control type, and then the name, is the GNU standard. try to keep it that way, cause everybody codes that way and when you share code, is easier for everybody else to read it and make changes to it. So: cmdMassReCaption ^ ^--------------- The name: Mass (Re-) Caption. |---- The type: cOmMANd. Ok, now get VB6 and get on with studying control arrays, cause you still need it, don't think 'you don't need to know that, cause it's not used anymore'!!! Kindly, Pablo.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Pablo.ar wrote: but I didn't know there is no control array on .Net, why is that? Because it's not proper OOP... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                1 Reply Last reply
                0
                • P Pablo ar

                  hey Dennis, Rage... you got it right guys, I was talking VB6 here, but I didn't know there is no control array on .Net, why is that? and as to you sundayschool... guess you ain't going to school on sundays anyway! hehe, joking... cmdMassReCaption is a way to name a button. I mean, the control IS A BUTTON but we give it a name we can identify it for what it does, or related to what we want to do with it. That way of naming controls and components, with three lowercase letters for a shorts of the control type, and then the name, is the GNU standard. try to keep it that way, cause everybody codes that way and when you share code, is easier for everybody else to read it and make changes to it. So: cmdMassReCaption ^ ^--------------- The name: Mass (Re-) Caption. |---- The type: cOmMANd. Ok, now get VB6 and get on with studying control arrays, cause you still need it, don't think 'you don't need to know that, cause it's not used anymore'!!! Kindly, Pablo.

                  D Offline
                  D Offline
                  Dennis C Dietrich
                  wrote on last edited by
                  #8

                  Pablo.ar wrote: That way of naming controls and components, with three lowercase letters for a shorts of the control type, and then the name, is the GNU standard. I've never heard it called GNU standard but of course it is a common notation (see INFO: Object Hungarian Notation Naming Conventions for VB[^]). However again Hungarian notation should not be used when writing .NET based software (see Design Guidelines for Class Library Developers[^], especially the Field Usage Guidelines[^]). Best regards Dennis

                  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