1 Attachment(s)
[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.
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?
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:
Public Class GamePickControl
Private TeamNameA As String
Public Property TopTeam() As String
Get
Return TeamNameA
End Get
Set(ByVal value As String)
TeamNameA = value
End Set
End Property
Private TeamNameB As String
Public Property BottomTeam() As String
Get
Return TeamNameB
End Get
Set(ByVal value As String)
TeamNameB = value
End Set
End Property
Private ChoosenWinner As String
ReadOnly Property Winner() As String
Get
Return ChoosenWinner
End Get
End Property
'This is where I would want to allow the choice of either
'TopTeam or BottomTeam on another instance of a GamePickControl UserControl
Public Enum Rank
TopTeam
BottomTeam
End Enum
Private TeamSlot As Rank
Public Property PlayingSlot() As Rank
Get
Return TeamSlot
End Get
Set(ByVal value As Rank)
TeamSlot = value
End Set
End Property
Private NextGame As System.Windows.Forms.GamePickeControl
Public Property MovesOnTo() As System.Windows.Forms.GamePickControl
Get
Return NextGame
End Get
Set(ByVal value As System.Windows.Forms.GamePickControl)
NextGame = value
End Set
End Property
'Now I would insert code in the radiobuttons to send the radiobutton.text from the
'checked radiobutton in this GamePickControl to nextgame.radiobutton1.text or nextgame.radiobutton2.text based on
'the selected setting for PlayingSlot. I imagine (incorrectely) that the attribute I would be setting would look like
'system.windows.forms.nextgame.radiobutton1.text = ChoosenWinner
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.
1 Attachment(s)
Re: Access user control attributes from a user control
Check this out as a starting point.
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.
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:
Public Class myCustomUserControl
Private anyOtherInstanceOfThisUserControl As myCustomUserControl
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:
anyOtherInstanceOfThisUserControl.aVarableWithinTheUserContol
correct?
Thanks again, if this seems right I think I'll call this thread resolved.
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.