Solved

Looping through text boxes

Posted on 2008-10-16
4
393 Views
Last Modified: 2008-10-23
I have a form with 26 text boxes.  I'm loading default values into the text boxes from a sheet. Instead of doing something like this:

frmMyForm.tbParam1 = Cells(1,1).Value
frmMyForm.tbParam2 = Cells(1,2).Value
....... up to 26

I'd like to setup a for loop to do the work
For x = 0 to 26
 frmMyForm.tbParamx = Cells(1,x).Value
Next x

The above code of course doesn't work but that's the idea.

Is there a way to cycle through the text boxes on a form instead of calling out each independently?

Thanks!
0
Comment
Question by:JohnSantaFe
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 22736768
if your tbParam name is consistent that you could do this

Cheers

Dave
For Each ccont In frmMyForm.Controls
        If TypeName(ccont) = "TextBox" Then ccont.Value = ActiveSheet.Cells(1, CInt(Replace(ccont.Name, "tbParam", vbNullString)))
    Next ccont

Open in new window

0
 
LVL 2

Accepted Solution

by:
MasterPage earned 500 total points
ID: 22737615
I would declare an array of TextBoxes that is populated in the form constructor.  Then you can loop though the array to do your business.

Note the TextBox array index is zero-based, hence the x-1.  I kept your Cells column reference one-based per your example.
Public Class frmMyForm
 
    Protected tbArray() As TextBox
 
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        tbArray = New TextBox() {tbParam1, tbParam2, ...,  tbParam26 }
    End Sub
 
    Protected Sub PopulateTextBoxes()
        For x As Integer = 1 To 26
            tbArray(x - 1).Text = Cells(1, x).Value
        Next x
    End Sub
 
 
End Class

Open in new window

0
 

Author Comment

by:JohnSantaFe
ID: 22741891
Hi Dave,

What type is ccont in your example?

When I try it I get a run time type mismatch no matter if I declare ccont as an Object, Varient, or Text Box.

Thanks.
0
 

Author Comment

by:JohnSantaFe
ID: 22788743
I ended up using Master Page's idea of an array of text boxes although in a simpler way that didn't involve creating a class
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

791 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