Solved

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

Posted on 2006-11-23
9
217 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
  • 5
  • 2
  • 2
9 Comments
 

Author Comment

by:eadam-uk
Comment Utility
Like the excel INDIRECT() function
0
 
LVL 15

Expert Comment

by:Solar_Flare
Comment Utility
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
Comment Utility
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
 

Author Comment

by:eadam-uk
Comment Utility
Sorry, that should be btRFD1... to btRFID8 on the third line down, not btRFID1... to tbRFID8
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Accepted Solution

by:
Solar_Flare earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
<<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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now