StrataFrame Forum

Macro Substitution or ReFlection

http://forum.strataframe.net/Topic25450.aspx

By Terry Bottorff - 12/23/2009

I have a FrmGetPayOff with TabControl1 on it. On page1 of tabcontrol1 I have several labels with names lbl1go, lbl2go, lbl3go, ...

I would like to fill them with some numeric data found in an array ngopercents().

I have tried to follow some samples found in the forum but if I use the Ctype below it tells me I am missing an object and if I use the last several examples using an object the object in the line  below ends up to be nothing and of course produces and error. What am I doing wrong in this code or how do I get the correct object?

loObj = Me.TabControl1.TabPages(1).Controls("lbl" & i.ToString + "go")

Dim i As Integer = 0

Dim j As Integer = 0

Dim loObj As Object

For j = 0 To (nnumofplaces - 1)

i = j + 1

' CType(Me.Controls("lbl" & i.ToString + "go"), Label).Text = ngopercents(j).ToString + "%"

' CType(Me.Controls("txt" & i.ToString + "go"), Label).Text = ngoresults(j).ToString("C")

' loObj = Me.TabControl1.TabPages.Controls("lbl" & i.ToString + "go")

' loObj = Windows.Forms.TabControl.TabPageCollection.Controls("lbl" & i.ToString + "go")

' loObj = frmCalcPayOffs.TabControl1.TabPages(1).Controls("lbl" & i.ToString + "go")

loObj = Me.TabControl1.TabPages(1).Controls("lbl" & i.ToString + "go")

loObj.GetType().GetProperty("Text").SetValue(loObj, ngopercents(j), Nothing)

Next

TIA.

By Terry Bottorff - 12/24/2009

I created the silly little form that is attached and the following code works fine. Why does it not work when I try it in my previous post??? TIA

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

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

Dim i As Integer = 1

For i = 1 To 3

CType(Me.Controls("lbl" & i.ToString), Label).Text = i.ToString

CType(Me.Controls("lbl" & i.ToString + "go"), Label).Text = (i * 100).ToString

Next

For i = 10 To 11

CType(Me.TabPage1.Controls("lbl" & i.ToString), Label).Text = i.ToString

CType(Me.TabPage1.Controls("lbl" & i.ToString + "go"), Label).Text = i.ToString

Next

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim i As Integer = 1

For i = 10 To 11

CType(Me.TabPage1.Controls("lbl" & i.ToString), Label).Text = i.ToString

CType(Me.TabPage1.Controls("lbl" & i.ToString + "go"), Label).Text = i.ToString

Next

End Sub

End Class

By Les Pinter - 12/24/2009

Hi Terry,

   I've got an article on how to use reflection to read values of screen objects, and how to set their Text (or other) property, at http://lespinter.com/FoxToNet/ShowArticle.aspx?ArtNum=405. See if the technique described there will do what you need.

Les 

By Terry Bottorff - 12/24/2009

Oh thank you so much I will look into it very closely. I did find my mistake because I had a groupbox that I was not including in the statements. Here is finally what I used but I will read your article to get a grip on this. Thank you again and have a great Holiday season.

CType(Me.GroupBox2.Controls("lbl" & (j + 1).ToString + "go"), Label).Text = ngoper(j).ToString + "%"

CType(Me.GroupBox2.Controls("txt" & (j + 1).ToString + "go"), TextBox).Text = nresult(j).ToString("C")