Lua in Visual Basic.net - vb.net

I'm trying to just get a VB.net app to be able to execute a lua script in a external file, and be able to add some functions to lua too, To do this I have this code:
Imports LuaInterface
Public Class Form1
Public luascripting As New Lua()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
luascripting.RegisterFunction("DisplayText", Me, Me.GetType().GetMethod("DisplayText"))
luascripting.DoFile("script.lua")
End Sub
End Class
But it errors on the register function, saying "Object reference not set to an instance of an object." Do you know of a example VB.net project that uses lua? Or know how to fix this?

You are registering a function but you forgot to write it. Paste this into your form code:
Public Sub DisplayText()
MsgBox("Works")
End Sub

Related

WinForm ".Show()" method doesn't show up in Visual studio

I am pretty a beginner in using visual studio so I may missed a lot of things. I want to create a custom group in MS Project with a button which I want when I click on it, it would open up a WinForm with some buttons on it. So I created a VSTO project for MS project then added a Ribbon(Visual designer) and a form.
My custom group in Task tab created with a button in it. I double clicked on this button to jump right into it's click handler code, then tried to write a simple code to show my form but it seems it lacks something because intellisense doesn't show up .Show() method and it can not be built. The error I got when I try to build is this:
error BC30469: Reference to a non-shared member requires an object reference.
My form label is Form1 and the simplest code which I wrote in my button click event handler is as follow:
Imports Microsoft.Office.Tools.Ribbon
Imports System.Windows.Forms '(I added this line after the hints)
Public Class Ribbon1
Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e
As RibbonUIEventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As
RibbonControlEventArgs) Handles Button1.Click
Form1.Show() '(The Line which error occurs)
End Sub
End Class
First of all, you need to check references of your add-in project. By default VSTO add-in projects don't include the System.Windows.Forms assembly reference. If the reference is not there, you need to add it manually. Then in the code you need to use the fully qualified namespace or include it in the beginning of the code file (by using the Imports keyword).
To be able to call the Show method you need to create the Form1 instance in the code first. The method is not static, so you can't invoke it on the class declaration. You must have an object reference.
The code may look like that:
Private Sub Button1_Click(sender As Object, e As
RibbonControlEventArgs) Handles Button1.Click
Dim f as Form1 = new Form1()
f.Show()
End Sub

Why does the main form have different hashes from different threads?

As far as I was aware, the main form (here I'll call it Form1 as is default) of a .Net windows form application was a bit like a singleton. It seems to be special in that you can access the instance from anywhere just by using Form1; even though you only have one form instance, you can access that without passing a variable around.
However, I was suprised to find, that if I use the TPL to make a number of tasks, and run them together, and each of them call Form1.GetHashCode they return different values.
Furthermore, if I place a public member object inside mainform, then set a value on one of its properties, that won't be reflected in the tasks.
What is going on here - it is as though there is a new instance for each task, but that can't be right? That would take ton's of memory/initialisation surely, and also I can't see lots of forms. I know I can't access controls from other threads, but this is just an integer. What is happening?
Example Code
Just make a new project and stuff this into Form1 one, with a single button on that form.
Imports System.Threading.Tasks
Imports System.Threading
Public Class Form1
Public foo As New Test
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
foo.bar = 99
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ThisCouldBeAnywhere()
Dim Task1 As New Task(AddressOf ThisCouldBeAnywhere)
Dim Task2 As New Task(AddressOf ThisCouldBeAnywhere)
Dim Task3 As New Task(AddressOf ThisCouldBeAnywhere)
Task1.Start()
Task2.Start()
Task3.Start()
End Sub
End Class
Public Class Test
Public bar As Integer = 4
End Class
Public Module TestMod
Public Sub ThisCouldBeAnywhere()
MsgBox(Form1.GetHashCode & vbCrLf & Form1.foo.bar)
End Sub
End Module

"http is a type and can not be used as an expression" VB.NET

I'm having trouble with a program that I'm trying to make. The program is supposed to play an mp3 file from a URL, but I keep getting an saying "http is a type and can not be used as an expression". I've been trying to figure this out for the past hour, but I can't. I tried Google to see if there might be a solution to my problem, but I couldn't find anything to fix my problem.
Imports System.Net.WebRequestMethods
Imports System.Net
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AxWindowsMediaPlayer1.URL = http://keygenjukebox.com/?i=ACME_-_UltraEdit_12.xkg.mp3
End Sub
End Class
Any help would be greatly appreciated.
You have forgotten to wrap the string in quotes:
AxWindowsMediaPlayer1.URL = "http://keygenjukebox.com/?i=ACME_-_UltraEdit_12.xkg.mp3"

using GoogleEarth plugin from VB.NET through FC.GEPluginCtrls

I'm really looking for a simple way to build VB.NET apps that use the GEplugin. So I have found this project that seems to do the dirty job I need: http://code.google.com/p/winforms-geplugin-control-library/
Well, all the code posted there around works on C#, but I need to have it on VB.NET. So I have tried this:
created a new 32-bit solution from VB.NET 2010 Express (I simply added
<PlatformTarget>x86</PlatformTarget >
inside the .vbproj file)
added a reference to FC.GEPluginCtrls.dll
inserted a GeWebBrowser control on the form
at the top of the code, added
Imports FC.GEPluginCtrls
then, in the form Load event, put this code:
InitializeComponent()
GeWebBrowser1.LoadEmbeddedPlugin()
Do
Loop Until GeWebBrowser1.PluginIsReady = False
GeWebBrowser1.CreateInstance(ImageryBase.Earth)
that, I think, would be equivalent to
http://code.google.com/p/winforms-geplugin-control-library/wiki/CreateInstance
So, the project compiles and doesn't get errors, but the GeWebBrowser control remains empty.
I actually wrote the library you are using. You are not listening for the PluginReady event. http://code.google.com/p/winforms-geplugin-control-library/wiki/PluginReady
To use it with VB simply convert the basic examples to VB -
http://code.google.com/p/winforms-geplugin-control-library/wiki/ExampleForm
Also, a loop polling PluginIsReady is totally unnecessary as the PluginReady event is asynchronous.
To show the earth all you would need is the following.
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GeWebBrowser1.LoadEmbeddedPlugin()
End Sub
To use the plugin when it has initialzesd use the PluginReady event. Something like.
Option Strict Off
Public Class Form1
Private Dim _ge as Object = Nothing
Private Sub GeWebBrowser1_PluginReady( ByVal sender As System.Object, ByVal e As FC.GEPluginCtrls.GEEventArgs) Handles GeWebBrowser1.PluginReady
_ge = e.ApiObject ' reference to the Google Earth Plugin object
MessageBox.Show(_ge.getApiVersion()) ' _ge is the plugin -use it just as in the javascript api...
End Sub
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GeWebBrowser1.LoadEmbeddedPlugin() ' load the plugin
End Sub
End Class

Why won't this simple messagebox work?

Module Module1
Public cccounter = 9
End Module
Public Class frmNim
Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnSelect.Click
MsgBox(cccounter)
End Sub
End Class
Why does this generate errors? I can't figure out any other way to make a simple counter go up by clicking on a button. This is frustrating me to no end. Is there something very simple that I'm obviously missing?
Use MessageBox.Show(ccounter)
I think you're using the old VB6 coding. This won't work in VB.NET.
MSDN
If you need your counter to go up, you do need an extra line of code:
ccounter += 1
EDIT:
Missed the declaration in the module (VB.Net bit rusty now a days)
You should declare the ccounter as a variable as mentioned by #Eddie Paz)
I've made a quick sample that adds 1 at every click on the button.
Module Module1
Public ccounter As Integer = 9
End Module
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ccounter += 1
MessageBox.Show(ccounter)
End Sub
End Class
You're declaring cccounter as a variant in Module1. You should specify the type such as integer. In the btnSelect:
cccounter = cccounter + 1
MessageBox.Show(cccounter)
I think MsgBox still works in VB.Net, but I don't remember. I try to use the .NET way now.