VB.Net

If you touch your software enough does it become hardware?

Moderator: Forum Moderators

Wiggy
5pork
5pork
Posts: 925
Joined: June 12th, 2005, 17:00
Location: Chesterfield, UK

VB.Net

Post by Wiggy »

Anyone know anything to do with VB.Net? I have a mate who's looking for some help with a program he's trying to make.
buzzmong
Weighted Storage Cube
Weighted Storage Cube
Posts: 7167
Joined: February 26th, 2007, 17:26
Location: Middle England, nearish Cov

Post by buzzmong »

Break his fingers until he promises never to use VB again?
Wiggy
5pork
5pork
Posts: 925
Joined: June 12th, 2005, 17:00
Location: Chesterfield, UK

Post by Wiggy »

buzzmong wrote:Break his fingers until he promises never to use VB again?
Might work, but then he won't be able to learn another one will he?

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. :?
buzzmong
Weighted Storage Cube
Weighted Storage Cube
Posts: 7167
Joined: February 26th, 2007, 17:26
Location: Middle England, nearish Cov

Post by buzzmong »

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)

Code: Select all

sub
new button (attb1, attb2, ..., attbn)
end sub
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

Code: Select all

arrayname [index of object in the array].
Try prodding Fear or Amblin, I think they deal with VB occasionally, whereas I'm fresh from Java and now working on C#.
sedontane
Mouse
Mouse
Posts: 10
Joined: March 2nd, 2008, 14:24
Contact:

Post by sedontane »

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

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

Code: Select all

lblName()   'label containing Team Name
lblScore()   'label containing Team Score
Both dynamically created at runtime with this code

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)})
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

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
it gives me this error
An unhandled exception of type 'System.NullReferenceException' occurred in Teams.exe
and this occurs in this line

Code: Select all

Scr2.lblName(i).Text = txtTeamName(i).Text
Oh and I am learning C#.Net but its bloody hard in comparison to VB
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

I'd consider myself knowing something, it's the programming language I've used for about 4 years solid now. :lol:

Firstly, :finger: , 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
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.
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

Oh you cupcake you posted code while I was replying.

*reads*
buzzmong
Weighted Storage Cube
Weighted Storage Cube
Posts: 7167
Joined: February 26th, 2007, 17:26
Location: Middle England, nearish Cov

Post by buzzmong »

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.
HereComesPete
Throbbing Cupcake
Throbbing Cupcake
Posts: 10249
Joined: February 17th, 2007, 23:05
Location: The maleboge

Post by HereComesPete »

Don't listen to the 'mong, he will break your codez!*











*I'm aware they are broken already, but they will become moar broken by his fumblings. (twss)
buzzmong
Weighted Storage Cube
Weighted Storage Cube
Posts: 7167
Joined: February 26th, 2007, 17:26
Location: Middle England, nearish Cov

Post by buzzmong »

Cockle off Pete.

Anyone with any sense knows not to listen to me already, don't scare the new people :P
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

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:

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

Code: Select all

Public Sub Main
  MyPrivateSub()
  CType(Me.Controls(1),Label).Text = "123"
End Sub
or

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.
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

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

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.
HereComesPete
Throbbing Cupcake
Throbbing Cupcake
Posts: 10249
Joined: February 17th, 2007, 23:05
Location: The maleboge

Post by HereComesPete »

See Buzz! I said your codez wus wrong!


/brings nothing to the coding table (except tasty food)
buzzmong
Weighted Storage Cube
Weighted Storage Cube
Posts: 7167
Joined: February 26th, 2007, 17:26
Location: Middle England, nearish Cov

Post by buzzmong »

Fear wrote:
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.
Wrong.

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
Did I mention I hadn't done VB in a while?
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 :P )
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

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:

Code: Select all

Dim myControl As New Label
myControl.ID="lblName2"
Me.Controls.Add(MyControl)
Then refer to them like this:

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

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
or even more effeicent

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
The properties method is not recommended when you do not want to limit the number of labels.
sedontane
Mouse
Mouse
Posts: 10
Joined: March 2nd, 2008, 14:24
Contact:

Thanks

Post by sedontane »

Thanks guys I will get on it - Ill be back to tell you the result/let you see the finished program
Dr. kitteny berk
Morbo
Morbo
Posts: 19676
Joined: December 10th, 2004, 21:53
Contact:

Post by Dr. kitteny berk »

I bring nothing to this thread other than

:wave:

pull up a badger or something.
HereComesPete
Throbbing Cupcake
Throbbing Cupcake
Posts: 10249
Joined: February 17th, 2007, 23:05
Location: The maleboge

Post by HereComesPete »

Hey! I brought nothing here first dammit!

Might as well say Loes! :wave: too and hope you'll say something on occasion and maybe become a 5punker.
Dr. kitteny berk
Morbo
Morbo
Posts: 19676
Joined: December 10th, 2004, 21:53
Contact:

Post by Dr. kitteny berk »

HereComesPete wrote:Hey! I brought nothing here first dammit!
Yeah, but I brought nothing more awesomely.
Hehulk
KHAAAN!
KHAAAN!
Posts: 4746
Joined: April 18th, 2005, 15:36
Location: Bummingham, England
Contact:

Post by Hehulk »

* Hehulk makes a stylishly late entrance, just to one up Berk

Loes :wave:
Post Reply