Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Interface and Graphics > Problem with Textbox control Array


Reply
 
Thread Tools Display Modes
  #1  
Old 07-09-2012, 07:02 AM
ferion ferion is offline
Newcomer
 
Join Date: Jun 2012
Posts: 14
Default Problem with Textbox control Array


Heyy.

What I'm trying to do is create a textbox control array at runtime, but I don't get it to work.

Here is the code:

Code:
Dim lyricsbox() As TextBox

Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim width As Integer = Me.Width
        Dim numberoftxtboxes As Integer = width / 600
        Dim i As Integer = 0
        Do While i < numberoftxtboxes
            ReDim lyricsbox(i)
            lyricsbox(i) = New TextBox
            lyricsbox(i).Size = New Size(600, Me.Height - 135)
            lyricsbox(i).Visible = True
            lyricsbox(i).Parent = Me
            lyricsbox(i).Multiline = True
            lyricsbox(i).BringToFront()
            lyricsbox(i).Text = ""
            If i = numberoftxtboxes - 1 Then
                lyricsbox(i).ScrollBars = ScrollBars.Vertical
            End If

            If i = 0 Then
                lyricsbox(i).Location = New Point((Me.Width - numberoftxtboxes * 600 - (numberoftxtboxes - 1) * 10) / 2, 60)
            Else
                lyricsbox(i).Location = New Point((Me.Width - numberoftxtboxes * 600 - (numberoftxtboxes - 1) * 10) / 2 + i * 600 + i * 10, 60)
            End If
            i += 1
        Loop
End Sub
This code results in that all the controls are "Nothing" but the last one. At runtime I can check the members of the control array:

When 2 controls should be created:
(0) Nothing
(1) {Text = ""}

When 3 controls should be created:
(0) Nothing
(1) Nothing
(2) {Text = ""}

What have I done wrong?
Thank you in advance
Reply With Quote
  #2  
Old 07-09-2012, 07:48 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,273
Default

You don't really need to bother with an array to store the controls in. The form itself will kinda act like the array that you'll add controls in.

Although you shouldn't really go messing about with it, this is one of the times when it's worth looking at the Form's Designer.vb file to see what code VB generates to add controls to a form. You can then copy that code yourself for adding your own controls.

So, for example, start a new windows application project and add a textbox to the form. Over on the Solution Explorer, look for the button at the top that's Show All Files and click it. Your form should get an expandable plus sign next to it. Expand it and you should see the Form1.Designer.vb file. Open that file and you'll see the code that VB has automatically created for adding the textbox you added to the form. It should look similar to...
Code:
<System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(13, 13)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 262)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
The important part from that code that you're missing in your code is the Me.Controls.Add(Me.TextBox1) that actually adds the control to the form.

So, scrapping the array that's not needed, the code that should do what you're trying to do would be...
Code:
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim width As Integer = Me.Width
        Dim numberoftxtboxes As Integer = width / 600
        Dim i As Integer = 0

        Dim txt As TextBox ' You only need one object that will keep getting recreated for new textboxes.

        Do While i < numberoftxtboxes

            txt = New TextBox

            ' You should probably give the control a name
            txt.Name = "Textbox" & i.ToString

            txt.Size = New Size(600, Me.Height - 135)
            txt.Visible = True
            txt.Parent = Me
            txt.Multiline = True
            txt.BringToFront()
            txt.Text = ""
            If i = numberoftxtboxes - 1 Then
                txt.ScrollBars = ScrollBars.Vertical
            End If

            If i = 0 Then
                txt.Location = New Point((Me.Width - numberoftxtboxes * 600 - (numberoftxtboxes - 1) * 10) / 2, 60)
            Else
                txt.Location = New Point((Me.Width - numberoftxtboxes * 600 - (numberoftxtboxes - 1) * 10) / 2 + i * 600 + i * 10, 60)
            End If

            me.controls.add(txt)

            i += 1
        Loop
End Sub
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 07-09-2012, 04:44 PM
ferion ferion is offline
Newcomer
 
Join Date: Jun 2012
Posts: 14
Default

But how do I in another sub change the "txt1.text" when it hasn't really been created?
Reply With Quote
  #4  
Old 07-10-2012, 02:22 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,273
Default

Through the Form's Control's property. One reason for giving the control a name.

Code:
Me.Controls("TextBox1").Text = "Hello"
__________________
There are no computers in heaven!
Reply With Quote
  #5  
Old 07-10-2012, 03:21 AM
ferion ferion is offline
Newcomer
 
Join Date: Jun 2012
Posts: 14
Default

Thank you very much!
Reply With Quote
Reply

Tags
array, control, problem, textbox


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->