Execute string command?
-
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
-
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
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.
-
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.
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
-
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
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
-
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.
Control arrays only exist in VB6 and below, not in VB.NET... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Control arrays only exist in VB6 and below, not in VB.NET... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
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.
-
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.
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
-
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.
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