Hi everyone, I trying to figure out how to execute commands stored in string, let's say i have string called "command" and a textbox, so i want vb to run command that i typed in textbox, e.g. i type in textbox "label1.text = "bla bla"", and then type like "msgbox.show". Is that even possible? I hope you understand what i mean. Thanks.

Recommended Answers

All 12 Replies

Do you type in textBox both vlaues?
So your textbox after finishing writing looks like:

textbox "label1.text = "bla bla"" "msgbox.show"

With or without quotation marks?

No, i just want it to execute my commands typed in textbox. Look at the picture. When i type to textbox msgbox.show, i want it to show, if i type me.text = "bla bla", i want it to change the form text, it's just the examples, i want it to run all commands i type to textbox
http://i55.tinypic.com/21mclu0.png

On a button click or on textChnaged event?

It's not that important, it could be button click, textchanged event, i need to know if it possible to run almost any vb function through textbox.

Ok, I have choosen a button click, but you can easiy use this code in TextChanged event too:

UPDATED:

Private Sub button1_Click(sender As Object, e As EventArgs)
	ExecuteMyCode(textBox1.Text)
End Sub

Private Sub ExecuteMyCode(text As String)
	Dim data As String() = text.Split(" "C)
	If data.Length > 1 Then
		Select Case data(0)
			Case "msgbox.show"
				MessageBox.Show(data(1))
				Exit Select
			Case "me.text"
				Me.Text = data(1)
				Exit Select
			Case Else
				'do nothing is non of above
				Exit Select
		End Select
	Else
		MessageBox.Show("Data is missing to show.")
	End If
End Sub

Still some work to do on to show correct result! We need to gather data!

This one would be a better choice:

Private Sub button1_Click(sender As Object, e As EventArgs)
	ExecuteMyCode(textBox1.Text)
End Sub

Private Sub ExecuteMyCode(text As String)
	If text.Contains("me.text") Then
		Dim array As String() = text.Split("="C)
		Me.Text = array(1)
	Else
		MessageBox.Show(text)
	End If
End Sub

Thanks for the code, but it is the only way to executing commands by making "cases" or "if statements"? I've tried System.Diagnostics.Process.Start(string), but it showed error

Process.Start(); executes the code to start some application or something.

Dim myProcess As System.Diagnostics.Process = System.Diagnostics.Process.Start("Notepad")

will start the Notpad.
What did you mean to start?

I tried to execute commands like msgbox, but it only work if it is like this, but causes error too.
dim text as string = msgbox("lala", "lala")
System.Diagnostics.Process.Start(text)

it don't work other ways.

why would you want to execute commands from a text-box when you can simply declare variables to hold the commands.

commented: And how do you execute all VB.Net commands from variables? -1

No, i just want it to execute my commands typed in textbox. Look at the picture. When i type to textbox msgbox.show, i want it to show, if i type me.text = "bla bla", i want it to change the form text, it's just the examples, i want it to run all commands i type to textbox
http://i55.tinypic.com/21mclu0.png

It doesn't work this way. You can't issue VB commands on the fly. VB .Net is one of the languages that require compiling. Compilation is the process that "translates" the human readable instruction set (The commands you write) to instructions that the processor can understand.

What you describe can be only done if the commands are queries to a db or something like that. Queries are "translated" by the db service on the fly.

The cases and the IF statements are one way to run a predefined code, already "translated" for use by the CPU, but can't cover your requirement for "all commands" you type in the textbox. It can only work for the ones you've already written code for.

Ok, thanks for the answer adam k, i'll mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.