So I'm using this Spotify wrapper here:
Wrapper
and I'm attempting to convert the example provided to VB .NET from C#. I've converted every piece of code except for this one handler section.
Here is the C#:
eh.OnTrackChange += New SpotifyEventHandler.TrackChangeEventHandler(trackchange);
and here is my attempt of converting it to VB:
AddHandler eh.OnTrackChange, New SpotifyEventHandler.TrackChangeEventHandler(AddressOf trackchange)
No errors are thrown, however it simply doesn't work.
Here is TrackChange:
Private Sub trackchange(e As TrackChangeEventArgs)
status.Text = "Now Playing: " + mh.GetCurrentTrack().GetTrackName() + " by " + mh.GetCurrentTrack().GetArtistName()
speak(status.Text)
End Sub
The status text doesn't change and the speak method isn't called. Any help would be appreciated.
Did you set eh.ListenForEvents = true?
Without this set, events won't fire.
Regarding event-syntax, this should be correct:
AddHandler eh.OnTrackChange, AddressOf trackchange
Related
Is it possible to ask Excel to start a C# method?
How would you implement such a call in Excel?
(i.e. instead of programming in VB, I would like to program in C#)
I can imagine using a VB-macro to start a C# application in the background but maybe you know a nicer way?
For example, the C#-code shall be executed upon a click in a particular Excel cell.
Well you could open a program via VBA. This VBA script gets called by clicking on the Excel-Cell:
var Path = "MYPROGRAMPATH"
var Argument = "MYARGUMENT"
x = Shell("""" & Path & """ """ & Argument & """", vbNormalFocus)
To react on a cell change, use the following event:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'YOUR CODE
End Sub
Then program your C# application and let it determine the arguments.
Your program should react according to the filtered arguments.
This can be done with the Environment.GetCommandLineArgs-Method.
public static void Main()
{
Console.WriteLine();
// Invoke this sample with an arbitrary set of command line arguments.
String[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
//Handling of arguments here, switch-case, if-else, ...
}
I've found a solution to the VBA macro:
Sub Button1_Click()
Dim pathStr As String
Dim argumentStr As String
pathStr = "C:/Users/.../Desktop/temp/Trial02.exe"
argumentStr = "Any argument string"
Call Shell("""" & pathStr & """ """ & argumentStr & """", vbNormalFocus)
End Sub
I have a form named "form1" in vb.net. This form has many controls. I opened the form1.designer.vb file and put in an if else expression such as:
If getLanguage() = "en" then label1.text = "Good Morning" Else label1.Text = "Bonjour"
This works perfectly fine in runtime.
If I open the Form1.vb [Design] page in design, and make any changes, the code above disappears.
Is there a way I can keep any code I put in the designer page?
I don't want to put them in the load event in the form1.vb file.
Locate this code :
Public Sub New()
Me.InitializeComponent()
End Sub
Then add a line :
Public Sub New()
Me.InitializeComponent()
Me.MakeComponentsChanges()
End Sub
Then create the method in form1.vb or another Partial Class copy of your creation :
Private Sub MakeComponentsChanges()
If getLanguage() = "en" then
label1.text = "Good Morning"
Else
label1.Text = "Bonjour"
End If
End Sub
Don't touch the form1.designer.vb (.cs)
And as stated in other answers, better use .Localizable Property in the IDE and change it from False to True. Then you'll gain access to several default languages. You don't have to bother writing code.
Select ONE language to start with : English for example.
Then edit each one of your controls Text : write "Hello" in a button, "Good Morning" in a Label, "Because I'm Happy" in a MenuItem etc.
Then change the language again, select French.
Then edit again each control and write "Bonjour", "Je vous souhaite un bon matin", "Parceque je suis de bonne humeur".... YES you've lost the previous text but have faith !
Compile your project without launching it, and you'll see the IDE has created two new files : Form1.en.resx and Form1.fr.resx (or so) along with Form1.vb and Form1.Designer.vb. Don't edit them !
If you open the en.resx or fr.resx, you'll see that the edits you've made are in there. Those files are used to store inbuilt Lang-related ressources for your form. That's flatly called Globalization.
Then locate again the constructor of your Form.
Public Sub New()
Me.InitializeComponent()
'Me.MakeComponentsChanges()
' Now you know about some Globalization,
' you may get rid of that Method.
' Add two variables :
Dim OriginalCulture As CultureInfo
Dim CurrentOSCulture As CultureInfo
' Initialize them
OriginalCulture = Thread.CurrentThread.CurrentCulture
CurrentOSCulture = CultureInfo.CurrentCulture
' Do this test :
Try
Thread.CurrentThread.CurrentCulture = CurrentOSCulture ' may fail
Thread.CurrentThread.CurrentUICulture = CurrentOSCulture ' may fail
' Attempt to match the current Thread culture to the Operating System one.
Catch CurrentException As Exception
Thread.CurrentThread.CurrentCulture = OriginalCulture
Thread.CurrentThread.CurrentUICulture = OriginalCulture
' If it fails, revert back to default as defined in your IDE
End Try
End Sub
Don't forget to add on top of your Class declaration those two namespaces :
Imports System.Globalization
Imports System.Threading
And voilà ! I know I said you don't have to bother writing code, but the bits above in the constructor are enough to handle the selection of a language. Plus besoin de taper du code superflu après ça.
For your example maybe better will be using property of the form
.Localizable = true
.Language = Default(French)/English/or others
Visual Studio create two files of one class(which is your form) - Partial Class
So it is doesn't matter in which file you write your code.
Only visual studio use designer.vb file for generating form changes you made by designer
Code from designer.vb contain method InitializeComponent which executed in the constructor.
Create your own method and call it right after InitializeComponent in constructor.
Or create third file for your code if you don't want put in the yourform.vb
File yourForm.MyDesigner.vb
'Using Keyword Partial not necessary anymore,
'because it is used in the `yourForm.Designer.vb`
Public Class yourForm
Public Sub Changelabel()
If getLanguage() = "en" Then
label1.text = "Good Morning"
Else
label1.Text = "Bonjour"
End If
End Sub
End Class
designer.vb files are created automatically by Visual Studio. Every time you edit the design of the form, the file is re-written.
Now, why you don't want to have that code on Load? The load event or the constructor (as already stated) are the right places where to put any initialization you need.
I'm working on C# and now VB.NET ports of an old VBA program. It has lots of MSForms/OleObjects embedded in it like CommandButton or even images.
My first thought was to declare all the buttons as Microsoft.Vbe.Interop.Forms.CommandButtonbut that leads to a COM exception that the System._COM type can't be cast to ...Forms.CommandButton. If I try a more generic version of this solution, I don't find any items, and if I try to go through all VBComponets I note that they are all the sheets in the workbook, but none of the controls:
foreach (VBComponent xxx in Globals.ThisWorkbook.VBProject.VBComponents) {
Interaction.MsgBox(xxx.Name);
Interaction.MsgBox(xxx.ToString);
}
Thus all of these controls are not in .VBComponets, but I can find them as OLEobjects in thisworkbook.worksheets(n).OLEobjects (this is counterintutive to me, but I probably don't understand the system to begin with).
How do I handle the Click action from such an object?
I'm assuming that I need to be using the Excel.OLEObjectEvents_Event interface, but I can't seem to figure out how. If I try to make custom events with delegates, I don't seem to be able to assign them to OleObjects. If I use ActionClickEventHandler.CreateDelegate I can get a huge variety of errors that makes me think that's a dead end.
The official documentation from MS doesn't seem that helpful, though it did introduce me to the idea of Verb, which I'm looking into. So far that has only produced COM errors along the lines of "Application Failed to start."
Even just trying to use one of the two standard events, .GotFocus, I always pull a 0x80040200 error.
Example:
Excel.OLEObject ButtonCatcher = Globals.ThisWorkbook.Worksheets(1).OLEObjects("CommandButton1");
ButtonCatcher.GotFocus += CommandButton1_Click;
Throws a COMException Exception from HRESULT: 0x80040200 at the second line. The button is enabled, which is I checked after looking up the code number from the office dev site.
Trying a more generic approach within the code for a sheet containing controls:
object CommandButtonStart = this.GetType().InvokeMember("CommandButton1", System.Reflection.BindingFlags.GetProperty, null, this, null);
Throws a Missing Method error.
Any help is greatly appreciated, this seems like this should be obvious and I'm missing it.
**Edit: I have also found that I can cast these controls into Excel.Shape but that doesn't actually get me any closer to running a function or sub from the VSTO. I'm playing with Excel.Shape.OnAction but this requires a VBA sub to be called. Presumably, I could call a VBA sub which calls a sub from the VSTO as long as the VSTO was COM visible. This seems really round-about and I'd only like to do it as a last resort.
Solution Type: VSTO Document-Level
Scenario:
1.) Excel.Worksheet created at run-time. (not a Worksheet Host Item)
2.) Add a button on the Worksheet at run-time that triggers C# code when clicked.
Assembly References:
Microsoft.Vbe.Interop (Microsoft.Vbe.Interop.dll)
Microsoft.Vbe.Interop.Forms (Microsoft.Vbe.Interop.Forms.dll)
Microsoft.VisualBasic (Microsoft.VisualBasic.dll)
Tested / Working Code:
using MSForms = Microsoft.Vbe.Interop.Forms;
using System.Windows.Forms;
...
Microsoft.Vbe.Interop.Forms.CommandButton CmdBtn;
private void CreateOLEButton()
{
Excel.Worksheet ws = Globals.ThisWorkbook.Application.Sheets["MyWorksheet"];
// insert button shape
Excel.Shape cmdButton = ws.Shapes.AddOLEObject("Forms.CommandButton.1", Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing, 500, 5, 100, 60);
cmdButton.Name = "btnButton";
// bind it and wire it up
CmdBtn = (Microsoft.Vbe.Interop.Forms.CommandButton)Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(ws, null, "btnButton", new object[0], null, null, null);
CmdBtn.Caption = "Click me!";
CmdBtn.Click += new MSForms.CommandButtonEvents_ClickEventHandler(ExecuteCmd_Click);
}
private void ExecuteCmd_Click()
{
MessageBox.Show("Click");
}
Have you tried using NewLateBinding.LateGet?
using MSForms = Microsoft.Vbe.Interop.Forms;
using Microsoft.VisualBasic.CompilerServices;
...
MSForms.CommandButton CommandButton1 = (MSForms.CommandButton)NewLateBinding.LateGet(Globals.ThisWorkbook.Worksheets(1), null, "CommandButton1", new object[0], null, null, null);
CommandButton1.Click += new Microsoft.Vbe.Interop.Forms.CommandButtonEvents_ClickEventHandler(CommandButton1_Click);
It's referenced on MSDN in the VSTO forums and in an old blog post.
Can you programmatically add code to a CodeModule in the Workbook, like this?
Private Sub CommonButton_Click(ByVal buttonName As String)
MsgBox "You clicked button [" & buttonName & "]"
End Sub
Private Sub CreateEventHandler(ByVal buttonName As String)
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim codeText As String
Dim LineNum As Long
Set VBComp = ThisWorkbook.VBProject.VBComponents(Me.CodeName)
Set CodeMod = VBComp.CodeModule
LineNum = CodeMod.CountOfLines + 1
codeText = codeText & "Private Sub " & buttonName & "_Click()" & vbCrLf
codeText = codeText & " Dim buttonName As String" & vbCrLf
codeText = codeText & " buttonName = """ & buttonName & "" & vbCrLf
codeText = codeText & " CommonButton_Click buttonName" & vbCrLf
codeText = codeText & "End Sub"
CodeMod.InsertLines LineNum, codeText
End Sub
Use the interop forms toolkit. It is free from Microsoft. It provides com wrappers and event messenger class that communicates the event data to and from .NET to VBA. I have used it to handle control events from VBA in .NET, and events from .NET to VBA. You would use the interop
http://www.codeproject.com/Articles/15690/VB-C-Interop-Form-Toolkit.
From the toolkit ducoumentation:
Interop UserControls provide a basic set of intrinsic events (Click, GotFocus, Validate, etc.) in Visual Basic 6.0. You can define your own custom events in Visual Studio .NET and raise them using RaiseEvent. In order to handle the events in Visual Basic 6.0, you need to add a project reference and add a WithEvents variable declaration in your code, and then handle the events using the WithEvents variable rather than the control itself.
How To Handle Interop UserControl Custom Events
1.
In Visual Basic 6.0, on the Project menu, click References. Note that there is already a reference to ControlNameCtl, where ControlName is the name of your Interop UserControl.
2.
In the Available References list, locate a reference for ControlName and check it, and then click OK.
3.
In the Code Editor, add a declaration for a WithEvents variable:
Dim WithEvents ControlNameEvents As ControlLibrary.ControlName
In the Form_Load event handler, add the following code to initialize the WithEvents variable:
Private Sub Form_Load()
Set ControlNameEvents = Me.ControlNameOnVB6Form
End Sub
Add code to handle your custom event in the WithEvents variable's event handler:
Private Sub ControlNameEvents_MyCustomEvent()
MsgBox("My custom event fired")
End Sub
Thanks Leo Gurdian
In VB.Net look like this:
Assembly References:
Microsoft.Vbe.Interop (Microsoft.Vbe.Interop.dll)
Microsoft.Vbe.Interop.Forms (Microsoft.Vbe.Interop.Forms.dll)
Microsoft.VisualBasic (Microsoft.VisualBasic.dll)
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.VisualBasic
Imports MSForms = Microsoft.Vbe.Interop.Forms
Private Sub CreateOLEButton()
Dim ws As Excel.Worksheet = (CType(Application.ActiveSheet, Excel.Worksheet))
Dim cmdButton As Excel.Shape = ws.Shapes.AddOLEObject("Forms.CommandButton.1", Type.Missing, False, False, Type.Missing, Type.Missing, Type.Missing, 500, 5, 100, 60)
cmdButton.Name = "btnButton"
Dim CmdBtn As Microsoft.Vbe.Interop.Forms.CommandButton = CType(Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(ws, Nothing, "btnButton", New Object(-1) {}, Nothing, Nothing, Nothing), Microsoft.Vbe.Interop.Forms.CommandButton)
CmdBtn.Caption = "Click me!"
AddHandler CmdBtn.Click, AddressOf ExecuteCmd_Click
End Sub
Private Sub ExecuteCmd_Click()
'do something
End Sub
I have an AxShockwaveFlash object in a Windows Forms application, and load a (AS3) movie into it with LoadMovie. The movie plays correctly, but I am having a problem getting a variable from flash.
I have tried using GetVariable but it always returns an empty string. How can I get the value of a variable from flash?
I think the new security policy for AVM2 requires you to explicitly expose the variables/functions to the container application using ExternalInterface.
If you can't edit the swf, I can't think of a way to get access to them. It was really easy with AS2 though, if you defined it, you could get and set it via javascript/C#/whatever without any extra code in the swf.
Yo can use fscommand method to talk to C# from shockwave player:
fscommand("sendCmd", arg);
to catch value in C# use
flashPlayer.FSCommand += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(flashPlayer_FSCommand);
....
void flashPlayer_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
{
tbOut.Text += e.command + " (" + e.args + ")" + "\r\n";
}
How could I execute VBScript's code from TextBox control placed in C# application?
like , let's assume that I have a C# Windows Application(form)
and has two controls!
textbox (txtCode)
button (btnExecute)
txtCode has VBScript code
and I want when clicking at btnExecute to execute the VBScript code!!
You can pass VBS/JS directly to the scripting runtime & pass code & objects around.
Add a ref to the Microsoft Scripting Control (COM) then you can;
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
object[] anyParams = { "Bob"};
string expr = #"function foo(arg)
dim x: x = ""Hello "" & arg
msgbox x
foo = 12345
End function";
sc.Language = "VBScript";
sc.AddCode(expr);
object result = sc.Run("foo", ref anyParams);
//also
sc.Reset();
result = sc.Eval("1 + 2 / 3 + abs(-99)");
You will need to use WebBrowser Control to do that.
And since i don't think you can inject VBScript code in the loaded page. So what you can do is Create a Temp .html page, save your TextBox's script in it and then Load it in the the WebBrowser Control.
Couldn't you simply write it to a file and execute it using the default Windows behavior or something like that? It's the only way I can think of.
Check out this two articles:
http://msdn.microsoft.com/en-gb/magazine/cc301954.aspx
http://msdn.microsoft.com/en-us/library/ms974577.aspx