Results 1 to 10 of 10

Thread: [RESOLVED] Giving values to a series of user control internal controls

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Resolved [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"?

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    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)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  4. #4

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: Giving values to a series of user control internal controls

    Quote Originally Posted by Delaney View Post
    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.

  5. #5

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: Giving values to a series of user control internal controls

    Quote Originally Posted by Shaggy Hiker View Post
    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  7. #7

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: Giving values to a series of user control internal controls

    Quote Originally Posted by jmcilhinney View Post
    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.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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}

  9. #9

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    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...

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

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
  •  



Click Here to Expand Forum to Full Width