I am trying to call javascript using OpenWebKitSharp from WinForms with .NET 4
Here is the code I am trying to use.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WebKit;
using WebKit.Interop;
using WebKit.JSCore;
using webkitForm.Properties;
namespace webkitForm
{
public partial class Form1 : Form
{
WebKitBrowser webKitSharpBrowser = new WebKitBrowser();
public Form1()
{
InitializeComponent();
this.Controls.Add(webKitSharpBrowser);
webKitSharpBrowser.Width = 600;
webKitSharpBrowser.Height = 400;
}
private void button1_Click(object sender, EventArgs e)
{
webKitSharpBrowser.Preferences.AllowPlugins = true;
webKitSharpBrowser.UseJavaScript = true;
webKitSharpBrowser.Navigate("http://sandbox.icontact.com");
webKitSharpBrowser.GetScriptManager.EvaluateScript("alert('An alert from C#!');"); //Call javascript?
}
}
}
I can't get javascript to fire for anything... there must be something that I am missing.
Thanks in advance.
Well, it seems like it can't be done the way you want to:
if you are using .NET 4, calling a function is possible by using:
<webkitbrowser>.GetScriptManager.CallFunction("name", new Object[] { arg1, arg2, ...});
If you want to use .NET 2 you can use:
<webkitbrowser>.StringByEvaluatingJavaScriptFromString("name(arguments)")
- Open Webkit Sharp Issues
I tested your code, it IS working, but calling the alert function only triggers an event (WebKitBrowser.ShowJavaScriptAlertPanel), you are responsible for handling that event and showing a message or updating a label, or anything else.
For example:
Browser.ShowJavaScriptAlertPanel += Browser_ShowJavaScriptAlertPanel;
and then handle the event:
private void Browser_ShowJavaScriptAlertPanel(object sender, WebKit.ShowJavaScriptAlertPanelEventArgs e)
{
MessageBox.Show(e.Message);
}
Related
I am trying to pass a string my function in Windows Form Application in order to invoke InteropAssembly.dll from a LabVIEW script. The script takes a string for opening image and do some process and then return 2 different images. But I am getting the error The type 'LVBaseRefnum' is defined in an assembly that is not referenced. You must add a reference to assembly 'NationalInstruments.LabVIEW.RefnumTypes'. I have checked the forum and so and found this . But it was posted quite long time ago and still no valid answer. Have you ever encounter this error any solution? Thanks in advance and my code as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InteropAssembly;
namespace testLabview
{
public partial class Form1 : Form
{
private object img1;
private object img2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.pictureBox1 = null;
this.pictureBox2 = null;
string text = this.textBox1.ToString();
LabVIEWExports.test(text, out Image img1, out Image img2);
this.pictureBox1.Image = img1;
this.pictureBox2.Image = img2;
}
}
}
I'm trying to code a Skype Tool in C# with the SKYPE4COMLib. It is showing me the messages in Skype, but they keep loading all the time. Any ideas how to fix that?
My Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SKYPE4COMLib;
namespace SkypeTool
{
public partial class mainmenu : Form
{
Skype skype = new Skype();
public mainmenu()
{
InitializeComponent();
}
private void connect_Click(object sender, EventArgs e)
{
skype.Attach(5, false);
status.Text = "connected";
}
private void button1_Click_1(object sender, EventArgs e)
{
skype.SendMessage("censored", "Test");
}
}
}
This is happening for almost everyone. I think it's because of the skype update. There is a way to "Fix It" you can use open message dialog in skype4com. It will look like this.
Skype.Client.OpenMessageDialog(name goes here, message goes here);
Skype.Client.Focus();
Sendkeys.Send("Key that u want to use to send message")
You Can Also See this here How To: Fix Skype4Com SendMessage Problem!
I read this answer and tried playing a single note with NAudio.Midi:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NAudio.Midi;
using System.Threading;
namespace SoundVision10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Thread.Sleep inside GUI is just for example
using (MidiOut midiOut = new MidiOut(0))
{
midiOut.Volume = 65535;
midiOut.Send(MidiMessage.StartNote(60, 127, 0).RawData);
MessageBox.Show("Sent");
Thread.Sleep(1000);
midiOut.Send(MidiMessage.StopNote(60, 0, 0).RawData);
Thread.Sleep(1000);
}
}
}
}
It displays "Sent" and nothing else happens.
Sound is turned on.
The name of the first Midi output device is "Microsoft Gs Wavetable Synth"
Did I forget something?
Change the channel number to 1 and it works.
midiOut.Send(MidiMessage.StartNote(60, 127, 1).RawData);
I replaced NAudio.dll 1.7.3.0 with NAudio.dll 1.3.8.0 and it fixed the issue.
I am working on a project using Visual Studio(c#). I want to create a startup form when i install my application with a progress bar. And after progress bar completed this form should be hide and a new form should be open. can u help me about this problem?
Edit:
I've just made a sample application trying to use exactly the code that you've specified. It worked fine besides just one tweak:
Form1().Show(); should be new Form1().Show();
The only way this code does not execute is if you forgot to set timer1 to enabled state in design view which causes the code to never fire up.
Are you sure the code is firing up? have you done a break-point on this piece of code?
On a sidenote: timer1 is not on a separate thread so you don't need to use Invoke (you can see if you actually need it by looking InvokeRequired property of a control)
Suggested improvement: if you are not going to use Form2 again and judging from your code, it is likely you won't; perhaps you should call Close() on Form2 instead of Hide() and release the resources. I've had times when my application kept running in background because I hid the form but never closed it and application was on "exit when last window closes" which never happened.
So to be sure, here is the final code that does work on my machine:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//enable timer1 here or in designer
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
//disable timer1 first thing, otherwise it can end up ticking
//multiple times before you've had a chance to disable it
//if the timespan is really short
timer1.Enabled = false;
int d;
for (d = 0; d <= 100; d++)
progressBar1.Value = d;
Hide();
//create a new Form1 and then show it
new Form1().Show();
}
}
}
Create your form and add your progress bar
Set up event handlers on the parts of the form that should effect the progress bar
Update the progree bar to reflect the amount of work that is done
When the form is complete close it
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
int d;
for (d = 0; d <= 100; d++)
progressBar1.Value = d;
this.Hide();
Form1().Show();
timer1.Enabled = false;
}
}
}
There is a post in here about that ...but it doesn't work for me. I have added a system.speech.dll that I found in the internet but i cannot use System.speech , because it doesn't appear.
Error 1 The type or namespace name 'SpeechRecognizer' could not be found (are you missing a using directive or an assembly reference?)
Error 2 The type or namespace name 'SpeechRecognizedEventArgs' could not be found (are you missing a using directive or an assembly reference?)
I have used this code. I am using Windows Vista 64
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using System.Threading;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
SpeechRecognizer rec = new SpeechRecognizer();
public Form1()
{
InitializeComponent();
rec.SpeechRecognized += rec_SpeechRecognized;
}
void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
lblLetter.Text = e.Result.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
var c = new Choices();
// Doens't work must use English words to add to Choices and
// populate grammar.
//
//for (var i = 0; i <= 100; i++)
// c.Add(i.ToString());
c.Add("one");
c.Add("two");
c.Add("three");
c.Add("four");
c.Add("Five");
c.Add("six");
c.Add("seven");
c.Add("eight");
c.Add("nine");
c.Add("ten");
// etc...
var gb = new GrammarBuilder(c);
var g = new Grammar(gb);
rec.LoadGrammar(g);
rec.Enabled = true;
}
}
}
I agree with James Ogden. Also, you should add a "using" statement:
using System.Speech.Recognition
Or, fully qualify your class names.
1) You need to add a reference to System.Speech in your project
2) You shouldn't have had to find 'System.Speech.dll' on the Internet, it should be in .Net 3 (or 3.5, but get 3.5 anyway unless you've a compelling reason not to)
Edit:
You might want to look here:
http://dotnet.org.za/beta/archive/2008/01/06/system-speech-recognition.aspx
Check that you have a language engine matching the language you have configured in Vista. See http://support.microsoft.com/kb/934377
While not directly applicable to the above question - it is worth noting that the Speech SDK will not nessecarily be availible on each clients machines. While Vista includes a speech recognizer, XP does not. A possible way to correct this is to get the XP users to install the Speech SDK, which includes one. The other is to add Office 2003 (not 2007) as a dependency.
Im having problem about SpeechRecognizer class on Windows XP. sometimes it works but sometimes it doesnot work, and need to restart pc. on windows 7 its working fine. I think its some problem in speech engine itself, cause when I run my application several times it stops working.
Im using this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using System.Threading;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
SpeechRecognizer rec = new SpeechRecognizer();
public Form1()
{
InitializeComponent();
rec.SpeechRecognized += rec_SpeechRecognized;
}
void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
lblLetter.Text = e.Result.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
var c = new Choices();
c.Add("one");
c.Add("two");
c.Add("three");
c.Add("four");
c.Add("Five");
c.Add("six");
c.Add("seven");
c.Add("eight");
c.Add("nine");
c.Add("ten");
// etc...
var gb = new GrammarBuilder(c);
var g = new Grammar(gb);
rec.LoadGrammar(g);
rec.Enabled = true;
}
}
}