473,441 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Code Run Dynamically From Textbox in Vb.Net

Hello Friends

I run dynamically code from text-box in vb.net
only one text-box txtoutput can be access in dynamically run code

while get input from another text-box compiler show error
"txtbox1 is not declared .it may be inaccessible due to its protection level"


Please help me how to resolve this error

Thanks in Advance
Attached Images
File Type: jpg Run Test Ok.jpg (82.8 KB, 390 views)
File Type: jpg error textbox.jpg (90.0 KB, 279 views)
Attached Files
File Type: txt Dynamic Code.txt (2.5 KB, 291 views)
Nov 27 '19 #1
1 3923
SioSio
272 256MB
Add textBox1 and txtBox2 to the array of objects representing the arguments passed to the method (UpdateText).
And add textBox1 and txtBox2 to the argument of subroutine UpdateText.

Expand|Select|Wrap|Line Numbers
  1. Public Sub runCode()
  2.         ' Read code from file  
  3.         '  Dim input = My.Computer.FileSystem.ReadAllText("Code1.txt")
  4.         Dim input = TextBox3.Text
  5.         ' Create "code" literal to pass to the compiler.  
  6.         '  
  7.         ' Notice the <% = input % > where the code read from the text file (Code.txt)   
  8.         ' is inserted into the code fragment.  
  9.         Dim code = <code>  
  10.                        Imports System  
  11.                        Imports System.Windows.Forms  
  12.                        Public Class TempClass  
  13.                            Public Sub UpdateText(ByVal txtOutput As TextBox, ByVal txtbox1 As TextBox, ByVal txtbox2 As TextBox)  
  14.                                <%= input %>  
  15.                            End Sub
  16.                        End Class 
  17.                    </code>
  18.         ' Create the VB.NET compiler.  
  19.         Dim vbProv = New VBCodeProvider()
  20.             ' Create parameters to pass to the compiler.  
  21.             Dim vbParams = New CompilerParameters()
  22.             ' Add referenced assemblies.  
  23.             vbParams.ReferencedAssemblies.Add("mscorlib.dll")
  24.             vbParams.ReferencedAssemblies.Add("System.dll")
  25.             vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
  26.             vbParams.GenerateExecutable = False
  27.             ' Ensure we generate an assembly in memory and not as a physical file.  
  28.             vbParams.GenerateInMemory = True
  29.             ' Compile the code and get the compiler results (contains errors, etc.)  
  30.             Dim compResults = vbProv.CompileAssemblyFromSource(vbParams, code.Value)
  31.             ' Check for compile errors  
  32.             If compResults.Errors.Count > 0 Then
  33.                 ' Show each error.  
  34.                 For Each er In compResults.Errors
  35.                     MessageBox.Show(er.ToString())
  36.                 Next
  37.             Else
  38.                 ' Create instance of the temporary compiled class.  
  39.                 Dim obj As Object = compResults.CompiledAssembly.CreateInstance("TempClass")
  40.             ' An array of object that represent the arguments to be passed to our method (UpdateText).  
  41.             Dim args() As Object = {Me.txtoutput,Me.txtbox1,Me.txtbox2}
  42.             ' Execute the method by passing the method name and arguments.  
  43.             Dim t As Type = obj.GetType().InvokeMember("UpdateText", BindingFlags.InvokeMethod, Nothing, obj, args)
  44.         End If
  45.     End Sub
Dec 18 '19 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Senthil Kumar via .NET 247 | last post by:
Hi Frends, I have a "a.aspx.cs" and "b.aspx.cs", can i compile b.aspx.cs from a.aspx.cs? Thanks in advance Best Regards, Senthil kumar Ramachandran -----------------------
0
by: Andreas Klemt | last post by:
Hello, I have an ASPX Page with 8 Web User Controls on it. Some are Visible = False and some are Visible = True Now I wrote in each Web User Control code: Sub Page_Load() If Me.Visible...
4
by: Andy G | last post by:
I am tring to loop through a dataset to find records that exist. If the record exists then make the corresponding image visible. So I try to take the row value and concatenate it onto a string...
4
by: Jaime Stuardo | last post by:
Hi all... I need to add some JavaScript code that is dynamically generated to some point of the page. Currently I'm using ClientScript.RegisterStartupScript(GetType(), "menu", "<script...
2
by: gn | last post by:
I have a simple app that asks a user to enter text in a multiline textbox, (runat server) then submit, after submitting this text is then assigned to a variable as a string. This works fine with...
6
by: salo | last post by:
Can anybody say me for writing javascript code for textbox key press event such a way that it should allow only alphabets,dot and spaces
3
by: kj | last post by:
I've tried a bazillion ways to code dynamically generated methods, to no avail. The following snippet is a very simplified (and artificial) demo of the problem I'm running into, featuring my...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
0
by: =?Utf-8?B?amVmZmVyeQ==?= | last post by:
i need help compiling code dynamically it may involve some reflection so if any one is any good in that field or compiling code this would be a great time to show me what you know. by the way my...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.