Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB 2008, Text Box in array txtName(0,0), txtName(0,1), etc.

Posted on 2010-11-26
10
Medium Priority
?
780 Views
Last Modified: 2012-05-10
In VB 6, one could have a one name for a number of text box controls but diffrentiated via index numbers like:

txtAcct(0)
txtAcct(1)
etc.

Is this possible in vb 2008? I need to create txtName(5,5)  <-- 36 controls with the same name.

Please add some comments beside a yes/no answer.

Thank you.
0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34220749
You can do the same in .NET:

    Dim txtName() As TextBox

--OR--

    Dim txtName(5, 5) As TextBox

for example.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34220871
Obviously you can create an array of controls, but "control arrays" don't exist natively in VB2008 like they did in VB6.

You can however make a sub react to multiple controls by either listing more than one control after the "Handles" keyword or by "wiring" them up at run-time using the AddHandler() method.  Instead of an "index" as in VB6, you instead use the "sender" parameter to determine which control was the source of the event (just cast it to the appropriate control type).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34220906
Hmmm...  Seems I forgot about "control arrays" in VB6--I never used them that much. Good call Idle_Mind  :)
0
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 34221734
Hi Idle_Mind,

The metod you described is wonderful but I could use some code to get started. Let us say, we need to create an array of 6x 6 text boxes (to be created at run-time.

1- do I need them to be created, named, sized, etc, at run-time? If so, how is it done? The need to sit on top of a fram.
2- How the code for "AddHandler() method" is done?


Thank you,

Mike
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34222281
Is this WinForms or WebForms?  (you mentioned a "frame")

Will it ONLY be a grid of 6x6 boxes or will the number be defined at run-time?

The best method really depends on the nature of the "grid'...
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 34222473
It is WinForms. I ment a group option frame. The text boxes when created will be on top of the border of the option group box (I call it frame here).

Six rows, on each row, 1st column a txtName, followed by txtNo1, txtNo2, ... txtNo5

I could use just text boxes but I though it maybe learn how to do this via code. A good to skill to have.

Mike
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 34223125
Try this out...

To a blank form, add a Button, Label and a GroupBox:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GroupBox1.Controls.Clear()

        Dim tlp As New TableLayoutPanel
        tlp.ColumnCount = 6
        For c As Integer = 1 To tlp.ColumnCount
            tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100))
        Next
        tlp.RowCount = 6
        For r As Integer = 1 To tlp.RowCount
            tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 100))
        Next
        tlp.Dock = DockStyle.Fill

        For r As Integer = 1 To tlp.RowCount
            For c As Integer = 1 To tlp.ColumnCount
                Dim tb As New TextBox
                tb.Name = "TextBox" & c.ToString("00") & r.ToString("00")
                tb.Dock = DockStyle.Fill
                tb.Multiline = True
                AddHandler tb.TextChanged, AddressOf TB_TextChanged
                tlp.Controls.Add(tb, c - 1, r - 1)
            Next
        Next

        GroupBox1.Controls.Add(tlp)
    End Sub

    Private Sub TB_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tb As TextBox = CType(sender, TextBox)
        Label1.Text = tb.Name & ": " & tb.Text
    End Sub

End Class

Open in new window

Idle-Mind-374115.flv
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 34223896
Idle_Mind,

Your code is wonderful.

How can I:

- set the first column text boxes to 130 width , 20 height.
- the rest to 29 width , 20 height.
- do I have any control on the gap between the text boxes? If so, how?

Alos, after a user enters the data in it, I want to store its data into a array variable

Public MyData(5,5) as String

If the project doen't make sense is because I am just testing some ideas and there is no actual project involved.

FYI, in my next question I will ask about:

How to read the data in these boxes (the text boxes use no events). I will use a save button to read and validate the data in 6x6 array of the text boxes and in an event there was an error, I will shade it yellow with some data describing the error in their tootip control (property). I will post this after receiving some possible response to my earlier question on this post.

Thank you,

Mike
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 34223911
The video is great. I am holding back on expressing my gratitude on how much I appreciate the quality and completeness of your responses, otherwise I will be writing endless about it.

Thanks.
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 34224510
Excellent.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question