Solved

How would I compile a string in VB.NET and then run it as code?

Posted on 2006-11-23
9
269 Views
Last Modified: 2010-04-23
Hi,

I have the following code which is in an event handler for a button:

            With DirectCast(sender, Button)
                Dim index As Integer
                index = CInt(Microsoft.VisualBasic.Right(.Name, 1))
            End With

and then I would like to run something like this:

            tbRFID8.Text = frmAssignTag.assignBox()

where the "8" in the "tbRFID8" is the index variable. So something like

            tbRFID(index).Text = frmAssignTag.assignBox()

although I know that will not work. From my excel work I think i need the function which compiles the express something like

            UnknownFunction("tbRFID" & index & ".Text") = frmAssignTag.assignBox()

Any help would be greatly appreciated. Thanks.
0
Comment
Question by:eadam-uk
[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
  • 2
  • 2
9 Comments
 

Author Comment

by:eadam-uk
ID: 18004834
Like the excel INDIRECT() function
0
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 18004930
can you explain a bit more about what you are trying to do? there is probably a better way than to create a string containing code, then parse that string to execute the code, which is what you are wanting if i am reading the question right?
0
 

Author Comment

by:eadam-uk
ID: 18004969
Sure thing,

I have eight text boxes names tbRFID1 ... to tbRFID8 and eight button's names btRFID1... to tbRFID8. I have an event handler on the buttons which is below:

    Private Sub btnAssn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssn1.Click, btnAssn2.Click, _
    btnAssn3.Click, btnAssn4.Click, btnAssn5.Click, btnAssn6.Click, btnAssn7.Click, btnAssn8.Click
             With DirectCast(sender, Button)
                Dim index As Integer
                index = CInt(Microsoft.VisualBasic.Right(.Name, 1))
                MessageBox.Show(CStr(index))
            End With
     End Sub

the index value represents the last number in the button, which is 1 to 8. I then want to assign the results of

   frmAssignTag.assignBox()

to the corresponding text box 1 to 8, instead of displaying it in a message box as it does now. I could do an event handler for every text box, but I would like to create one that handles all buttons and boxes if possible.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:eadam-uk
ID: 18004974
Sorry, that should be btRFD1... to btRFID8 on the third line down, not btRFID1... to tbRFID8
0
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 500 total points
ID: 18005174
ok so instead of MessageBox.Show(CStr(index)) you want to set the text of the textbox whose name is tbRFID[index]

you can search for a control by its name, so you would use

Dim tb as textbox = CType(Me.FindControl("tbRFID" & index), Textbox)
tb.text = frmAssignTag.assignBox()
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 18005196
i think you are overcomplicating. you are already handling the button click event, and in there you will find that the parameter "sender" has in store already the button that was pressed. so if you modify your code to look like this may be sufficient:

Private Sub btnAssn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssn1.Click, btnAssn2.Click, _
    btnAssn3.Click, btnAssn4.Click, btnAssn5.Click, btnAssn6.Click, btnAssn7.Click, btnAssn8.Click
             With DirectCast(sender, Button)
                .Text = frmAssignTag.assignBox()
            End With
End Sub
0
 

Author Comment

by:eadam-uk
ID: 18005225
Genius, thanks!

Me.FindControl didn't work, but  your advice led me to Me.Controls.Find which produces an array of all the items it finds with that name. As I know I only have one item under that name, i just took the first index in the array which seems to work a treat.

                Dim find As Array = Me.Controls.Find("tbRFID" & index, True)
                Dim tb As TextBox = CType(find(0), TextBox)
                tb.Text = frmAssignTag.assignBox()

Thanks again!
0
 

Author Comment

by:eadam-uk
ID: 18005230
Hi newyuppie,

Wouldn't that assign my value to the button's text then and not the text box?

Thanks.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 18005233
<<Wouldn't that assign my value to the button's text then and not the text box?>>
exactly, but i misunderstood your question!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

628 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