Operator += cannot be applied - c#

I am creating a C# WinForms app that should closes the application using the voice command "exit".
However it gives me an exception:
Operator += cannnot be applied to operands of System speech or main method
In this code:
public partial class Form1 : Form
{
SpeechRecognitionEngine sRecongize = new SpeechRecognitionEngine();
private void Form1_Load(object sender, EventArgs e)
{
// Compiler error here:
sRecongize += sRecongize_SpeechRecognized;
}
private void sRecongize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text == "exit")
{
Application.Exit();
}
}
}
How can I subscribe to an event?

You need to subscribe to a specific event, in this case SpeechRecognized, not the entire class:
sRecongize.SpeechRecognized += sRecongize_SpeechRecognized;

Related

Getting error in Event Registration for Arguments

I'm creating a project where I using Event registration for calling methods between MainForm and multiple UserControl(s) where every method is working fine. But, when I create event to close MainForm by using a Close button at the UserControl using event registration then I am getting error (Overloading and invalid arguments etc.). I tried to arguments (sender, e) but it does not work.
MainForm Code:
private void Open_ucGateEntry(object o1, object o2)
{
pnlReplace.Controls.Clear();
ucGateEnrty objucGateEnrty = new ucGateEnrty();
pnlReplace.Controls.Add(objucGateEnrty);
this.Text = "GRN Creation";
objucGateEnrty.callBackEvent += new EventHandler(Open_ucGateEntryDetail);
objucGateEnrty.frmClose += new EventHandler(Form1_FormClosed); //Error is here
}
private void Open_ucGateEntryDetail(object o1, object o2)
{
pnlReplace.Controls.Clear();
ucGateEntryDetail objucGateEnrtyDetails = new ucGateEntryDetail();
pnlReplace.Controls.Add(objucGateEnrtyDetails);
this.Text = "GRN Creation Detail (Log Data)";
objucGateEnrtyDetails.callBackEvent += new EventHandler(Open_ucGateEntry);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//some code and close
}
UserControl is this:
private void btnCancel_Click(object sender, EventArgs e)
{
frmClose(this, e);
}

Events occur only after construction?

I try to understand why i can't invite event in the last line of constructor?
Does the event "formload" occur after construction ends?
public Form1()
{
InitializeComponent();
button1.Text = "a";
button1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "b";
}
This behavior is because if controls are built on the windows messages, which only works if the form instance exists and has a handle, which is not while it is still built.
The form must be open to be sure that the controls are working properly.
And it's not best practise to raise event on your constructor
Tested. Not dependent on the control created.
public Form1()
{
InitializeComponent();
button1.Text = "A";
button1_Click_helper();
}
private void button1_Click(object sender, EventArgs e)
{
button1_Click_helper();
}
private void button1_Click_helper()
{
button1.Text = "B";
}

Enlisting the Gtk.Dialog.Close event from another dialog is never triggered (C# mono)

I'm enlisting to the Gtk.Dialog close event from another dialog like this:
AddTask dialog = new AddTask (task); //AddTask inherits from Gtk.Dialog
dialog.Close += HandleDialogClose;
dialog.Show();
void HandleDialogClose (object sender, EventArgs e)
{
Refresh(); //Does some stuff in the calling dialog
}
When I close the dialog (the one I created above), the HandleDialogClose event is never triggered. Any idea why?
Here is some sample code, and I solved it now myself. I was enlisting to the Close() event, when I should have been enlisting the Destroyed() event. The code below now works (the commented code is what was not working).
using System;
using Gtk;
namespace test
{
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
Button button1 = new Button();
button1.Label = "Open About";
this.Add(button1);
button1.Show();
button1.Clicked += HandleButton1Clicked;
}
void HandleButton1Clicked (object sender, EventArgs e)
{
About dialog = new About();
//dialog.Close += HandleAboutClose;
dialog.Destroyed += HandleAboutClose;
dialog.Show();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
void HandleAboutClose (object sender, EventArgs e)
{
Console.WriteLine("About Closed");
}
}
}
I needed to use
dialog.Destroyed += HandleAboutClose;
instead of
dialog.Close += HandleAboutClose;

form closing event in multiple windows form application using visual c#

I have a multiple windows form application (frmMian, frmSteg and frmCrypt) in c#. From the main form (frmMain), I can call the other (two) forms. How can Iuse the form closing event similar to that used in VB to return to the main form whenever I exit any of the two forms?
FrmSteg frmstego = new FrmSteg();
FrmCrypto frmcrypt = new FrmCrypto();
private void btnsteg_Click(object sender, EventArgs e)
{
frmstego.Show();
this.Hide() ;
}
private void btncrypto_Click(object sender, EventArgs e)
{
frmcrypt.Show();
this.Hide();
}
Use the Form.Closed event on the child windows.
frmstego.Closed += (s, e) => {
this.Show();
};
frmcrypt.Closed += (s, e) => {
this.Show();
};
Try placing a Singleton accessor on the main form like this. And then in the constructor of the main form set _instance = this.
private static <MainFormType> _instance;
public static <MainFormType> Instance
{
get
{
return _instance;
}
}

WinForm Applications event handler

I am just trying my hand at some WinForm Applications and was creating a simple event handler, but I get an error message. Code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public delegate void MyHandler1(object sender, EventArgs e);
public Form1()
{
InitializeComponent();
List<string> names = new List<string>();
names.Add("S");
names.Add("I");
names.Add("G");
MyHandler1 onClicked = new MyHandler1(clicked);
listBox1.DataSource = names;
listBox1.Click += onClicked;
}
public void clicked(object sender, EventArgs e)
{
label1.ResetText();
label1.Text = listBox1.SelectedItem.ToString();
}
}
}
Error:
Error 1 Cannot implicitly convert type 'WindowsFormsApplication1.Form1.MyHandler1' to 'System.EventHandler'
The reason that your code doesn't compile is that implicit conversions do not exist between different delegate-types, even when the signatures are 'compatible'.
Try either of these:
// Implicit method-group conversion, should work from C# 2.0 or later.
// Essentially shorthand for listBox1.Click += new EventHandler(clicked);
listBox1.Click += clicked;
// Creating a delegate-instance from a 'compatible' delegate,
// a trick I recently learnt from his highness Jon Skeet
listBox1.Click += new EventHandler(onClicked);
As an aside, unless the intention is to learn how to use delegates, I suggest you don't create your own delegate-type when one that comes with the framework will do the job.
Just use this code instead:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public delegate void MyHandler1(object sender, EventArgs e);
public Form1()
{
InitializeComponent();
List<string> names = new List<string>();
names.Add("S");
names.Add("I");
names.Add("G");
listBox1.DataSource = names;
listBox1.Click += clicked;
}
public void clicked(object sender, EventArgs e)
{
label1.ResetText();
label1.Text = listBox1.SelectedItem.ToString();
}
}
}
You don't really need the EventHandler1 in order to listen to handle an event with clicked method.
You do not need to create an entirely new delegate type to subscribe to an existing event. The event you are subscribing to already uses the existing System.EventHandler delegate type.
You only need to do:
listBox1.Click += new EventHandler(onClicked);

Categories

Resources