2 Attachment(s)
Re: Control Array in VS 2010
Ok, here's an example of a form and a user control similar to what you're doing. It should give you an example of how things are done in .NET.
The project file is here:
Attachment 80011
The binary is included if you don't want to re-compile it. The output looks like this:
Attachment 80010
The user control code is as follows:
Code:
Public Class Stats
Private strStatName As String
Private intValue As Integer
Private intTotalPoints As Integer
Private intPointsUsed As Integer
'A custom constructor to simplify making a new control.
Public Sub New(ByVal statName As String)
InitializeComponent()
Me.StatName = statName
Me.Value = 0
End Sub
'The property for name of our stat
Public Property StatName() As String
Get
Return strStatName
End Get
Set(ByVal value As String)
strStatName = value
lblStatName.Text = strStatName
End Set
End Property
'The property for the current value of the stat
Public Property Value() As Integer
Get
Return intValue
End Get
Set(ByVal value As Integer)
intValue = value
lblValue.Text = intValue.ToString
intPointsUsed = CalculatePointsUsed(intValue)
End Set
End Property
'A read-only property for the amount of points the current stat value uses
Public ReadOnly Property PointsUsed() As Integer
Get
Return intPointsUsed
End Get
End Property
'A write-only property to set the total points the control has to work with
Public WriteOnly Property TotalPoints() As Integer
Set(ByVal value As Integer)
intTotalPoints = value
End Set
End Property
'An event to signal when the stat value has been changed
Public Event StatChanged(ByVal sender As Object, ByVal e As EventArgs)
'What happens when I hit the + button
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If intValue + 1 > 15 Then Exit Sub 'Stat can't go above 15!
Dim intPointShift As Integer = CalculatePointsUsed(intValue + 1) - CalculatePointsUsed(intValue)
If intTotalPoints - intPointShift < 0 Then Exit Sub 'Not enough points to raise stat!
'Add 1 to the current value.
Me.Value += 1
'Raise the "StatChanged" event so the parent form knows the value changed.
RaiseEvent StatChanged(Me, New EventArgs)
End Sub
Private Sub btnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSub.Click
If intValue - 1 < 0 Then Exit Sub 'Stat can't go below 0!
'Subtract 1 from the current value.
Me.Value -= 1
'Raise the "StatChanged" event so the parent form knows the value changed.
RaiseEvent StatChanged(Me, New EventArgs)
End Sub
Private Function CalculatePointsUsed(ByVal statValue As Integer) As Integer
'This is the formula for calculating the total point-cost of the stat.
'0 = 0 points. 1 through 5 = only 1 point. 6 through 10 = 1 point per
'level, plus 1 for the 1-5 range. 11 through 15 = 2 points per level, plus
'5 points for the 6 through 10 range and 1 more for the 1 through 5 range.
'The math formulas have been simplified to the case structure below.
'If it's anything outside the 0 through 15 range, it'll return 999 which
'can be used as an error-check.
Select Case statValue
Case 0
Return 0
Case 1 To 5
Return 1
Case 6 To 10
Return statValue - 4
Case 11 To 15
Return (2 * statValue) - 14
Case Else
Return 999
End Select
End Function
End Class
Re: Control Array in VS 2010
...and the form's code:
Code:
Public Class Form1
'Set my total points
Private Const STARTING_POINTS As Integer = 50
'Set current point count
Private intTotalPoints As Integer = STARTING_POINTS
'On my form, I just have a simple Label and Panel. The panel is where I'm going to put my user controls.
'I'll populate it when the form opens.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.lblPoints.Text = String.Format("Points: {0}", intTotalPoints)
'Define my stats
Dim myStats() As String = {"Strength", "Intelligence", "Wisdom", "Dexterity", "Constitution", "Charisma"}
Dim i As Integer
'Loop through the stats, make a control for each
For Each statName In myStats
Dim c As New Stats(statName)
c.TotalPoints = STARTING_POINTS
c.Top = i * c.Height
'This line links the "StatChanged" event in the user control with the event handler below
AddHandler c.StatChanged, AddressOf StatChanged
'Add the stat control to the panel
Me.Panel1.Controls.Add(c)
i += 1
Next
End Sub
Private Sub StatChanged(ByVal sender As Object, ByVal e As EventArgs)
'If one of those stat controls changes, we need to recompute the total points remaining
Dim intCount As Integer
'Add up each stat control's point usage
For Each c As Stats In Me.Panel1.Controls.OfType(Of Stats)()
intCount += c.PointsUsed
Next
'Subtract it from the max and we got our new point total
intTotalPoints = STARTING_POINTS - intCount
'Inform all the stat controls what the new total is
For Each c As Stats In Me.Panel1.Controls.OfType(Of Stats)()
c.TotalPoints = intTotalPoints
Next
'Update the label on the form
Me.lblPoints.Text = String.Format("Points: {0}", intTotalPoints)
End Sub
Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
'The Done button will record the final values in the stat controls to a dictionary. Using a dictionary
'has a lot of benefits. If later, I want to know what the character's strength is, all I need to do is:
'myStrength = finalStats("Strength")
'Better though would be to make a custom character class.
'This routine will also build a nice block of text with the stats and their values, as shown in the messagebox.
Dim finalStats As New Dictionary(Of String, Integer)
Dim strResults As New System.Text.StringBuilder
For Each c As Stats In Me.Panel1.Controls.OfType(Of Stats)()
finalStats(c.StatName) = c.Value
strResults.AppendLine(String.Format("{0,-13} = {1,3}", c.StatName, c.Value))
Next
MessageBox.Show(strResults.ToString)
End Sub
End Class
Re: Control Array in VS 2010
how can i call the main Form (form1) from the user control whid this code
Code:
Private Sub Form1_TextChangeForm(ByVal sender As Object, ByVal e As System.EventArgs) Handles Form1.TextChangeForm
TextBox1.Text = Form1.Poäng_GetForm
End Sub
The red Form1 i get "Handles clause requires a WithEvent variable defined in the containing type or one of its base types."
If i instead write Me i get "Event 'TextChangeForm' cannot be found." but i hawe defined if in the main form like this
Code:
Public Event TextChangeForm(ByVal sender As Object, ByVal e As System.EventArgs)
Re: Control Array in VS 2010
Is your Event on the form or on the usercontrol?
UserControl events need 4 things to make things happen on the parent form.
First, they need the Event definition in the UserControl code. This is because something is happening on the UserForm and we want to alert everything else that it's occurring.
Second, we need at least one RaiseEvent call in the code of the UserForm. This is the actual point where the code triggers the Event. If you don't have this, your Event will never fire because nothing will actually trigger it.
Third, you need a function on the parent form to Handle the Event. This is called an Event Handler. They usually, but not always, have "Handles ControlName.EventName" after them. This code instructs the parent form what to do when the control triggers it's event.
Last, you need a handler wiring. Normally, when you drag a control onto a form, this is done for you automatically. In the designer code of your form it will automatically put:
Dim WithEvents ControlName As ControlType
The "WithEvents" parts means "Set up the event wiring for all events on this control". Now, if you noticed, in my example, I created all my controls dynamically. I only wanted to wire up my one event, so I did it manually. This is where the AddHandler line comes in. I'm telling it "Add a handler link between this control's event and this function on this form."
Does this answer your question?
Re: Control Array in VS 2010
Not realy i'l post my code
The code you posted was good but it had to meany thing i never used before
i did a whole new prject to se if i could make the form and usercontrol speack whid each other
Form1.vb code
Code:
Public Class Form1
Dim poängform As Integer
Public Event TextChangeForm(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Property Poäng_GetForm() As Integer
Get
Return poängform
End Get
Set(ByVal value As Integer)
poängform = value
End Set
End Property
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
poängform = TextBox1.Text
RaiseEvent TextChangeForm(Me, e)
End Sub
Private Sub Skills_TextChangeSkill(ByVal sender As Object, ByVal e As System.EventArgs) Handles Skills1.TextChangeSkill, Skills2.TextChangeSkill
TextBox1.Text = Skills1.Poäng_GetUser
End Sub
End Class
Skills.bv code
Code:
Public Class Skills
Dim PoängUser As Integer
Public Event TextChangeSkill(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub Form1_TextChangeForm(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseMove
TextBox1.Text = Form1.Poäng_GetForm
End Sub
Public Property Poäng_GetUser() As Integer
Get
Return PoängUser
End Get
Set(ByVal value As Integer)
PoängUser = value
End Set
End Property
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
PoängUser = TextBox1.Text
RaiseEvent TextChangeSkill(Me, e)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PoängUser = Form1.Poäng_GetForm + PoängUser + 1
TextBox1.Text = PoängUser
RaiseEvent TextChangeSkill(Me, e)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PoängUser = Form1.Poäng_GetForm + PoängUser - 1
TextBox1.Text = PoängUser
RaiseEvent TextChangeSkill(Me, e)
End Sub
End Class
i hawe one textbox in the form and one in the usercontrol and 2 buttons in the usercontrol.
and i'm using 2 usercontrols
What i want is if i change the one in the form to lets say 100 the text boxes in the usercontrols to 100 (only to see that its transfeard) later i'm yust going to sent it a integer and when i press button1 the textbox in the form would increase whid 1 and the one on the form decrease whid 1 and reversed when i press button 2
Code:
When i start the prog
0
/ \
/ \
0 (0) 0(0)
after i hawe manualy increased the textbox on the form
100
/ \
/ \
0 (100) 0(100)
Press the increse button on the usercontrol 1
99
/ \
/ \
1 (99) 0(99)
press the increase button on the usercontrol 2
98
/ \
/ \
1 (98) 1(98)
Re: Control Array in VS 2010
Ok, you don't need the Event or property or anything on the Form since the form is the parent of the controls. If you change the value of the TextBox, then all you want to do is update the controls. You KNOW they're called Skills1 and Skills2, so just set them.
You need an event on UserControls because you never know what they could be sitting on.
Also, you're confused in your user control. You only have one variable: PoängUser. You need at LEAST 2 by your example, you need PoängUser to keep track of how many TOTAL points are left, and you need one to track what the control is set at. Look at your own example. You have your first usercontrol set to 1 (99) and your second to 0 (99). That's two numbers you need.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Skills1.Poäng_GetUser = TextBox1.Text
Skills2.Poäng_GetUser = TextBox1.Text
End Sub
Private Sub Skills_TextChangeSkill(ByVal sender As Object, ByVal e As System.EventArgs) Handles Skills1.TextChangeSkill, Skills2.TextChangeSkill
TextBox1.Text = Skills1.Poäng_GetUser
End Sub
End Class
Re: Control Array in VS 2010
How can i make my UserControl to a array like i did the text boxes erlier like this
Code:
Dim UserControls() As TextBox
Private Sub Form1_Load
UserControl = {Skills1.TextBox2, Skills2.TextBox2}
End Sub
This is the error
"An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."
After a lite testing i narowed the error down to this
Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
i = 0
Do While i <= 1
UserForm(i).Text = TextBox1.Text
i = i + 1
Loop
End Sub
and this
Code:
Private Sub Skills_Textbox(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Skills1.TextBoxChanged, Skills2.TextBoxChanged
i = 0
Do While i <= 1
If TextBox1.Text <> UserForm(i).Text Then
TextBox1.Text = UserForm(i).Text
End If
i = i + 1
Loop
End Sub