-
Mar 31st, 2021, 02:34 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Giving values to a series of user control internal controls
Consider a collection of "user control"s with 3 text boxes which you want to give specific values from a table (i.e. DataGridView cells). How to use a loop to do such operation? "For"? or "For each"?
-
Mar 31st, 2021, 02:53 PM
#2
Re: Giving values to a series of user control internal controls
it will depends. if you want to link the i/j of the for loop to the row/column of the datagridview, the for loop may be better
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Mar 31st, 2021, 03:26 PM
#3
Re: Giving values to a series of user control internal controls
If it really was just three, I'd be inclined not to use a loop at all, but to write it out. If it was more than three, I'd probably have them in some kind of collection, which would either be a Dictionary or a List. In that case I would prefer a For Each if it made sense to do so, but it often won't, as Delaney noted, in which case there is nothing wrong with the For Next, in terms of performance.
My usual boring signature: Nothing
-
Mar 31st, 2021, 05:20 PM
#4
Thread Starter
Hyperactive Member
Re: Giving values to a series of user control internal controls
Originally Posted by Delaney
it will depends. if you want to link the i/j of the for loop to the row/column of the datagridview, the for loop may be better
Can I have both ways codes? I will do try-and-fail to achieve desirable response. Thanks for your time.
-
Mar 31st, 2021, 05:22 PM
#5
Thread Starter
Hyperactive Member
Re: Giving values to a series of user control internal controls
Originally Posted by Shaggy Hiker
If it really was just three, I'd be inclined not to use a loop at all, but to write it out. If it was more than three, I'd probably have them in some kind of collection, which would either be a Dictionary or a List. In that case I would prefer a For Each if it made sense to do so, but it often won't, as Delaney noted, in which case there is nothing wrong with the For Next, in terms of performance.
Unfortunately controls are more than even 10. And yes. For each and a "List" kind of code. Can you help me? Thanks for your time
-
Mar 31st, 2021, 06:50 PM
#6
Re: Giving values to a series of user control internal controls
This is not a question you really need to ask us. Each of the four types of loops is better in certain circumstances so you only have to consider the circumstances to know which type of loop to use. If you have a list of items and you want to perform an action for each one then obviously you should use a For Each loop. Do you want to perform an action for each row in your grid? If so then the solution is obvious. If not, keep looking.
Do you want to perform an action for a series of sequential numbers in a range? That series might be the indexes of items in a pair of lists that you want to transfer data between. In that case, a For Each loop is not appropriate because it can only reference one list, while a For loop counter can be used to index both lists. If that's still not appropriate then keep looking.
A Do loop has four similar but different forms:
1. Keep looping while a condition is true and perform an action.
2. Keep looping until a condition is true and perform an action.
3. Perform an action and keep looping while a condition is true.
4. Perform an action and keep looping until a condition is true.
A While loop is equivalent to the first type of Do loop. You should make a decision to either always use a While loop in that scenario or to never use a While loop. Mixing and matching means inconsistency and is therefore bad.
The biggest problem that beginners have is that they try to write code without actually knowing what it has to do and that you don't know what loop you need to use is evidence of that problem. If you think about the actual functionality that you're trying to implement then which loop to use isn't really a question. If you have a 1:1 correspondence between rows in a grid and user controls that you can put in an array (it's not clear that that is the case because your description is vague) then a For loop is the obvious choice.
-
Apr 1st, 2021, 04:41 AM
#7
Thread Starter
Hyperactive Member
Re: Giving values to a series of user control internal controls
Originally Posted by jmcilhinney
This is not a question you really need to ask us. Each of the four types of loops is better in certain circumstances so you only have to consider the circumstances to know which type of loop to use. If you have a list of items and you want to perform an action for each one then obviously you should use a For Each loop. Do you want to perform an action for each row in your grid? If so then the solution is obvious. If not, keep looking.
Do you want to perform an action for a series of sequential numbers in a range? That series might be the indexes of items in a pair of lists that you want to transfer data between. In that case, a For Each loop is not appropriate because it can only reference one list, while a For loop counter can be used to index both lists. If that's still not appropriate then keep looking.
A Do loop has four similar but different forms:
1. Keep looping while a condition is true and perform an action.
2. Keep looping until a condition is true and perform an action.
3. Perform an action and keep looping while a condition is true.
4. Perform an action and keep looping until a condition is true.
A While loop is equivalent to the first type of Do loop. You should make a decision to either always use a While loop in that scenario or to never use a While loop. Mixing and matching means inconsistency and is therefore bad.
The biggest problem that beginners have is that they try to write code without actually knowing what it has to do and that you don't know what loop you need to use is evidence of that problem. If you think about the actual functionality that you're trying to implement then which loop to use isn't really a question. If you have a 1:1 correspondence between rows in a grid and user controls that you can put in an array (it's not clear that that is the case because your description is vague) then a For loop is the obvious choice.
Hi! I meant the latter. I'm new to newer VS and was away for a quit long time. Mainly I do microcontroller programming which is C based and a bit different to VS(VB). For some reasons I cannot use C# btw.
The problem is unavailability of index in parenthesis which were used in Visual Basic 6.0 I remember.
What I need is correct version of the code below:
Code:
For i As Integer = 1 To 10
UserControl(i).Textbox1.Text = DataGridView1.Item(0, i)
UserControl(i).Textbox2.Text = DataGridView1.Item(1, i)
UserControl(i).Label1.Text = DataGridView1.Item(2, i)
Next
Another example is to show second tab(s) of a TabControl of all UserControls by pressing a button. I know there should be a loop to do "UserControl(i).TabControl1.SelectedIndex = 1" but don't know how.
Last edited by pourkascheff; Apr 1st, 2021 at 04:44 AM.
-
Apr 1st, 2021, 04:51 AM
#8
Re: Giving values to a series of user control internal controls
What you were using in VB 6 was Control Arrays, but they don't exist in the same way in VB.Net (there are several variations that give you more options).
For this kind of thing you can simply create an array containing the controls, and then you can access the items from it as in your example, eg:
Code:
Dim UserControl as MyUserControl() = {UserControl1, UserControl2, ... UserControl10}
-
Apr 1st, 2021, 07:45 AM
#9
Thread Starter
Hyperactive Member
Re: Giving values to a series of user control internal controls
Thanks mate! It worked. The code has following image:
Code:
Dim UserControl As UserControl() = {UserControl1, UserControl2, UserControl3, UserControl4, UserControl5, UserControl6, UserControl7, UserControl8, UserControl9, UserControl10, UserControl11, UserControl12, UserControl13, UserControl14, UserControl15, UserControl16, UserControl17, UserControl18, UserControl19, UserControl20, UserControl21, UserControl22, UserControl23, UserControl24}
For i As Integer = 1 To 24
UserControl(i - 1).TabControl1.SelectedTab.Text = DataGridView1.Item(0, i).Value.ToString
UserControl(i - 1).TankHeight.Text = DataGridView1.Item(1, i).Value.ToString & " m)"
Next
I even remember declaring them (from 1 to 24) is even possible via another loop. Guess you have 500 controls =))))) Are you going to sit and type all of them in parenthesis? Hell no...
-
Apr 1st, 2021, 08:05 AM
#10
Re: [RESOLVED] Giving values to a series of user control internal controls
I can't guarantee they'll be in the correct order without sorting them...
Code:
Dim UserControl As UserControl() = Me.Controls.OfType(Of UserControl).ToArray
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|