VB.Net
Moderator: Forum Moderators
Might work, but then he won't be able to learn another one will he?buzzmong wrote:Break his fingers until he promises never to use VB again?
The problem he's having is, and I quote:
"No wiggy the problem I am having is that I cant access the attributes of dynamically created controls after creation and therefore my dynamically created controls become useless"
Whatever that means.
-
- Weighted Storage Cube
- Posts: 7167
- Joined: February 26th, 2007, 17:26
- Location: Middle England, nearish Cov
How's he creating his controls dynamically?
If he's just making the code go (this is in pesudo code, haven't done vb in a while)
He won't be giving the button any sort of reference name, and I don't know if VB will assign a default one to it.
I'd have added the objects to be part of an array (or a similar structure, I'd probably use ArrayList in Java for objects), so you can access them via
Try prodding Fear or Amblin, I think they deal with VB occasionally, whereas I'm fresh from Java and now working on C#.
If he's just making the code go (this is in pesudo code, haven't done vb in a while)
Code: Select all
sub
new button (attb1, attb2, ..., attbn)
end sub
I'd have added the objects to be part of an array (or a similar structure, I'd probably use ArrayList in Java for objects), so you can access them via
Code: Select all
arrayname [index of object in the array].
Hi I am attempting to create a scoring system program in VB.Net
I have 2 forms Manager & scr2
I will just mention that all dynamic controls are created fine with no errors
Manager contains a label and 2 buttons at Design time and at runtime a variable amount of labels buttons and text boxes created dynamically.
Important controls for manager are
the top three are dynamically created at runtime
scr2 is completely blank until runtime when a frameset is drawn dependent on number of teams requiring scores and 2 labels are added per team
Important controls are
Both dynamically created at runtime with this code
And this is expanded for a 4th iteration as well
I am first (before reaching the eventual goal of using the btnSubmit controls) trying to make scr2 collect values from
Manager by using this code
it gives me this error
Oh and I am learning C#.Net but its bloody hard in comparison to VB
I have 2 forms Manager & scr2
I will just mention that all dynamic controls are created fine with no errors
Manager contains a label and 2 buttons at Design time and at runtime a variable amount of labels buttons and text boxes created dynamically.
Important controls for manager are
Code: Select all
txtTeamName() 'textbox containing team name
txtTeamScore() 'textbox containing team score
btnSubmit() 'button to apply changes]
btnScr2Load 'button to start second screen. Static control
scr2 is completely blank until runtime when a frameset is drawn dependent on number of teams requiring scores and 2 labels are added per team
Important controls are
Code: Select all
lblName() 'label containing Team Name
lblScore() 'label containing Team Score
Code: Select all
Dim sp1, sp2 As Integer
If teamNum < 3 Then
sp1 = Math.Round(Me.Width / 2)
Else
sp1 = Math.Round(Me.Width / 4)
End If
sp2 = Math.Round(Me.Height / 4)
Select Case teamNum
Case Is < 3
ReDim lblName(1)
lblName(1) = New lblch()
With lblName(1)
.Name = "lblName1"
.Text = "Team 1"
.Location = New System.Drawing.Point(sp1 - Math.Round(lblName(1).Width / 2), sp2)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(1)})
ReDim lblName(2)
lblName(2) = New lblch()
With lblName(2)
.Name = "lblName2"
.Text = "Team 2"
.Location = New System.Drawing.Point(sp1 - Math.Round(lblName(2).Width / 2), sp2 * 3)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(2)})
Case Is = 3
ReDim lblName(1)
lblName(1) = New lblch()
With lblName(1)
.Name = "lblName1"
.Text = "Team 1"
.Location = New System.Drawing.Point(sp1 - Math.Round(lblName(1).Width / 2), sp2)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(1)})
ReDim lblName(2)
lblName(2) = New lblch()
With lblName(2)
.Name = "lblName2"
.Text = "Team 2"
.Location = New System.Drawing.Point((sp1 * 3) - Math.Round(lblName(2).Width / 2), sp2)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(2)})
ReDim lblName(3)
lblName(3) = New lblch()
With lblName(3)
.Name = "lblName3"
.Text = "Team 3"
.Location = New System.Drawing.Point((sp1 * 2) - Math.Round(lblName(3).Width / 2), sp2 * 3)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(3)})
I am first (before reaching the eventual goal of using the btnSubmit controls) trying to make scr2 collect values from
Manager by using this code
Code: Select all
Private Sub btn2scr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2scr.Click
If Scr2.Visible = False Then
Scr2.Show()
For i = 1 To teamNum
Scr2.lblName(i).Text = txtTeamName(i).Text
Next
Else
Scr2.Hide()
End If
End Sub
and this occurs in this lineAn unhandled exception of type 'System.NullReferenceException' occurred in Teams.exe
Code: Select all
Scr2.lblName(i).Text = txtTeamName(i).Text
I'd consider myself knowing something, it's the programming language I've used for about 4 years solid now.
Firstly, , vibrospaz. VB was shit, VB.Net is <s>more powerful than you could possibly imagine</s> equally as powerful as PHP and C#. Every language has it's place, I wouldn't use vb.net to develop a microcontroller, for example.
Now, onto the problem at hand. I'm not sure if he is talking about a web application, or not. I shall assume he is.
The example above demonstrates how you MUST load dynamically created controls on every postback, otherwise the events wont fire for them.
If that isn't his problem, you'll have to be less vague and post some code.
Firstly, , vibrospaz. VB was shit, VB.Net is <s>more powerful than you could possibly imagine</s> equally as powerful as PHP and C#. Every language has it's place, I wouldn't use vb.net to develop a microcontroller, for example.
Now, onto the problem at hand. I'm not sure if he is talking about a web application, or not. I shall assume he is.
Code: Select all
Protected Sub Page_Load(blah)
Dim o as new Button
AddHandler o.OnClick, AddressOf ButtonClick
Me.Controls.Add(o)
End Sub
Protected Sub ButtonClick(sender as object, e as eventargs)
CType(sender,Button).text="You Clicked Me"
End Sub
If that isn't his problem, you'll have to be less vague and post some code.
-
- Weighted Storage Cube
- Posts: 7167
- Joined: February 26th, 2007, 17:26
- Location: Middle England, nearish Cov
NullReferenceException is saying that it can't assign a value to Scr2.lblName(i).Text because there is nothing in txtTeamName(i).Text.
Bodge job solution I'd try is set txt.TeamName(i).Text to " " (a space) earlier on in the code when it's created, alternatively you can catch the exception in a Try and Catch loop.
At least that's my take on it.
Bodge job solution I'd try is set txt.TeamName(i).Text to " " (a space) earlier on in the code when it's created, alternatively you can catch the exception in a Try and Catch loop.
At least that's my take on it.
-
- Throbbing Cupcake
- Posts: 10249
- Joined: February 17th, 2007, 23:05
- Location: The maleboge
I'm finding your code quite hard to follow, but it looks like you are referring to objects that you've declared privately in a different scope.
It's akin to doing:
The label isn't available anywhere outside the controls collection of the form, so you have to grab a reference to it from there. E.g.
or
It's akin to doing:
Code: Select all
Private Sub MyPrivateSub
Dim lblMylabel as new label
Me.Controls.Add(lblMyLabel)
End Sub
Public Sub Main
MyPrivateSub
lblMyLabel.Text = "123" <would error (wouldn't even compile)
End Sub
Code: Select all
Public Sub Main
MyPrivateSub()
CType(Me.Controls(1),Label).Text = "123"
End Sub
Code: Select all
Public Sub Main
MyPrivateSub
CType(Me.FindControl(lblMyLabel),Label).Text = "123"
End Sub
Last edited by Fear on March 2nd, 2008, 15:22, edited 1 time in total.
Wrong.buzzmong wrote:NullReferenceException is saying that it can't assign a value to Scr2.lblName(i).Text because there is nothing in txtTeamName(i).Text.
You can set a string to nothing, that is perfectly valid.
It means Scr2 is nothing, txtTeamName(i) is nothing, or Scr2.lblName(i) is nothing.
It just so happens that the VB.Net base control will convert a nothing value to an empty string anyway.
Code: Select all
Set(ByVal value As String)
If (value Is Nothing) Then
value = ""
End If
If (value <> Me.Text) Then
If Me.CacheTextInternal Then
Me.text = value
End If
Me.WindowText = value
Me.OnTextChanged(EventArgs.Empty)
If Me.IsMnemonicsListenerAxSourced Then
Dim control As Control = Me
Do While (Not control Is Nothing)
Dim impl As ActiveXImpl = DirectCast(control.Properties.GetObject(Control.PropActiveXImpl), ActiveXImpl)
If (Not impl Is Nothing) Then
impl.UpdateAccelTable
Return
End If
control = control.ParentInternal
Loop
End If
End If
End Set
Last edited by Fear on March 2nd, 2008, 15:25, edited 1 time in total.
-
- Throbbing Cupcake
- Posts: 10249
- Joined: February 17th, 2007, 23:05
- Location: The maleboge
-
- Weighted Storage Cube
- Posts: 7167
- Joined: February 26th, 2007, 17:26
- Location: Middle England, nearish Cov
Did I mention I hadn't done VB in a while?Fear wrote:Wrong.buzzmong wrote:NullReferenceException is saying that it can't assign a value to Scr2.lblName(i).Text because there is nothing in txtTeamName(i).Text.
You can set a string to nothing, that is perfectly valid.
It means Scr2 is nothing, or Scr2.lblName(i) is nothing.
It just so happens that the VB.Net base control will convert a nothing value to an empty string anyway.
Code: Select all
Set(ByVal value As String) If (value Is Nothing) Then value = "" End If If (value <> Me.Text) Then If Me.CacheTextInternal Then Me.text = value End If Me.WindowText = value Me.OnTextChanged(EventArgs.Empty) If Me.IsMnemonicsListenerAxSourced Then Dim control As Control = Me Do While (Not control Is Nothing) Dim impl As ActiveXImpl = DirectCast(control.Properties.GetObject(Control.PropActiveXImpl), ActiveXImpl) If (Not impl Is Nothing) Then impl.UpdateAccelTable Return End If control = control.ParentInternal Loop End If End If End Set
Java was my last domain, and it doesn't like strings which havn't been initated with a Null.
I'll bow out now and let Fear have full reign over proceededings (that and I'm going to go play Vampire: The Masquerade )
Meh, another post time.
I've had another look through, and I think there is an easier way to dynamically add controls which will simplify things for you.
When you add a control, do something like this:
Then refer to them like this:
And so on.
Do away with those nasty arrays and I think it will become easier.
If you have a small and finite amount of labels, you could consider using properties to make them available.
or even more effeicent
The properties method is not recommended when you do not want to limit the number of labels.
I've had another look through, and I think there is an easier way to dynamically add controls which will simplify things for you.
When you add a control, do something like this:
Code: Select all
Dim myControl As New Label
myControl.ID="lblName2"
Me.Controls.Add(MyControl)
Code: Select all
Ctype(Me.FindControl("lblName2"),Label).Text="Whatever Text"
Ctype(Me.FindControl("lblName2"),Label).Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
Do away with those nasty arrays and I think it will become easier.
If you have a small and finite amount of labels, you could consider using properties to make them available.
Code: Select all
Public ReadOnly Property lblName2 as Label
Get
Dim o as control = Me.FindControl("lblName2")
If o Is Nothing then Return Nothing
Return Ctype(o,Label)
End Get
End Property
Code: Select all
Private _lblName2 as Label = Nothing
Public ReadOnly Property lblName2 as Label
Get
If _lblName2 Is Nothing Then
_lblName2 = Me.FindControl("lblName2")
End If
Return _lblName2
End Get
End Property
Thanks
Thanks guys I will get on it - Ill be back to tell you the result/let you see the finished program
-
- Morbo
- Posts: 19676
- Joined: December 10th, 2004, 21:53
- Contact:
-
- Throbbing Cupcake
- Posts: 10249
- Joined: February 17th, 2007, 23:05
- Location: The maleboge
-
- Morbo
- Posts: 19676
- Joined: December 10th, 2004, 21:53
- Contact: