Results 1 to 7 of 7

Thread: [RESOLVED] Access user control attributes from a user control

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    4

    Resolved [RESOLVED] Access user control attributes from a user control

    I am trying to build a tournament bracket application. In order to build the application efficiently I would like to make a user control "GameControl" that represents a game and give it attributes that represent the names of the competitors "TeamNameA" and "TeamNameB" ,an attribute "Winner" to represent which team should move on, and an attribute "NextRoundGame" that assigns another instance of the user control so that it can assign the "winner" to one of the "TeamName" attributes of the next round in the competition. If the controls were native to VB I feel like I could do it easily (I have only spent about 30 hours self teaching/tutorial following) but I can't seem to find out how to pass values to custom attributes of custom user controls through code.

    A visual reference of my project is attached. The idea is that no matter how large the tournament is I could add all the "GameControl" boxes and assign them to each other as necessary at design time and then as the tournament progressed clicking on the radio button of the winner would pass that teams name down to the next level.

    Thanks in advance for any help. I think this is the easiest way to make the program flexible provided it would work, but I fear from my searches that it's not a supported technique.
    Attached Images Attached Images  

  2. #2

    Re: Access user control attributes from a user control

    What do you mean by these controls aren't native to VB? I fail to see what is the custom control, since you are writing a control. Easy way is to make a program limit. A tournament can only be so big after all. I'd just manually program out the GUI, and use checks to see how many 'teams' you have.

    Eg: The main box asks: How many teams do you have? The built in max is like...8 teams (based on your visual representation). So, the program would draw the control box at the specified resolution to show that there is a maximum of 8 teams like you have.

    So, hide everything else for now, except the first 4 GameControls.

    When TeamA is clicked in, for example, GC1(GameControl1), YOu would show GameControl5, and disable TeamB in GC1. TeamA in GC5 would become what TeamA was in GC1. The winner of GC2 would take up TeamB's slot.

    See how this would work?

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    4

    Re: Access user control attributes from a user control

    Thanks for the reply, That would be the simplest solution if it were only eight or so teams. The specific idea for this program came from creating a March Madness Bracket system so it's really 64 teams, that's a lot of repetitive code to enter. I also wasn't expecting the software to allow the user to choose how many teams, it was more about having the game boxes available to use when creating the program.

    I did start the program (a couple of times ago) with code for each selection box, but it was going to get out of hand quick, especially when using similar boxes to allow people to create brackets filled with their expected outcomes and save them all.

    I thought it might be useful for me to share an example of what I thought the bulk of the code would look like, some of this is wrong, I've just entered it the way I expected it to go, because I haven't yet found out how to do it right.

    VB Code:
    1. Public Class GamePickControl
    2.  
    3.     Private TeamNameA As String
    4.     Public Property TopTeam() As String
    5.         Get
    6.             Return TeamNameA
    7.         End Get
    8.         Set(ByVal value As String)
    9.             TeamNameA = value
    10.         End Set
    11.     End Property
    12.  
    13.     Private TeamNameB As String
    14.     Public Property BottomTeam() As String
    15.         Get
    16.             Return TeamNameB
    17.         End Get
    18.         Set(ByVal value As String)
    19.             TeamNameB = value
    20.         End Set
    21.     End Property
    22.  
    23.     Private ChoosenWinner As String
    24.  
    25.     ReadOnly Property Winner() As String
    26.         Get
    27.             Return ChoosenWinner
    28.         End Get
    29.     End Property
    30.     'This is where I would want to allow the choice of either
    31.     'TopTeam or BottomTeam on another instance of a GamePickControl UserControl
    32.  
    33.     Public Enum Rank
    34.         TopTeam
    35.         BottomTeam
    36.     End Enum
    37.  
    38.     Private TeamSlot As Rank
    39.     Public Property PlayingSlot() As Rank
    40.         Get
    41.             Return TeamSlot
    42.         End Get
    43.         Set(ByVal value As Rank)
    44.             TeamSlot = value
    45.         End Set
    46.     End Property
    47.  
    48.     Private NextGame As System.Windows.Forms.GamePickeControl
    49.     Public Property MovesOnTo() As System.Windows.Forms.GamePickControl
    50.         Get
    51.             Return NextGame
    52.         End Get
    53.         Set(ByVal value As System.Windows.Forms.GamePickControl)
    54.             NextGame = value
    55.         End Set
    56.     End Property
    57.     'Now I would insert code in the radiobuttons to send the radiobutton.text from the
    58.     'checked radiobutton in this GamePickControl to nextgame.radiobutton1.text or nextgame.radiobutton2.text based on
    59.     'the selected setting for PlayingSlot. I imagine (incorrectely) that the attribute I would be setting would look like
    60.     'system.windows.forms.nextgame.radiobutton1.text = ChoosenWinner
    61.  
    62. End Class

    Again, thanks for any help you can offer, I may just end up creating it all on the form directly without the usercontrols, but it seems like I could save so much time if I can just drop them into the designer once I get one written.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Access user control attributes from a user control

    Check this out as a starting point.
    Attached Files Attached Files
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    4

    Re: Access user control attributes from a user control

    Wow, thanks. To me it looks like you did some huge amount of work just to help me out. I suppose that by the time you've made 40-something-thousand posts you can answer a questing pretty quickly and whip up some code without breaking a sweat, but thank you very much no matter how long it took.

    I definetely have several starting points now. There is a bunch of new information there for me to digest. Clearly the getting started tutorial in the included help file leaves some stuff out.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    4

    Re: Access user control attributes from a user control

    I've looked over what you sent me and it is so different from what I had done it is taking some time to digest it all. I've very new to VB and a rusty-amateur at best at any other programming, and the sample you sent me is a great way to see some new stuff and clean ways of doing things.

    Just to clarify was the major thing I was missing that I hadn't created a variable with the type being my custom user control?

    VB Code:
    1. Public Class myCustomUserControl
    2.     Private anyOtherInstanceOfThisUserControl As myCustomUserControl
    3.     Private aVariableWithinTheUserControl As String 'or Double, Integer, etc...
    With that code I create a way to access other instances of this user control from within this control, right? Obviously I need to set anyOtherInstanceOfThisUserControl to a value using a property, but the code above is what allows me to later use

    VB Code:
    1. anyOtherInstanceOfThisUserControl.aVarableWithinTheUserContol

    correct?

    Thanks again, if this seems right I think I'll call this thread resolved.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Access user control attributes from a user control

    That's basically correct. The way I set up that example, each control can have its team names set explicitly, which would be the case for each of the first round game, or by two other instances of the same control type, which would occur when a winner was selected in those two parent controls. That example could certainly be cleaned up but it was mainly to give you an idea of what was possible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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