Getting error in Event Registration for Arguments - c#

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);
}

Related

Operator += cannot be applied

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;

Binding Form Closing event in class

Hello I'm trying to make a class that can handle the form closing event for my winForm
I have figured out how to work the event handlers, like this for a ContextMenuStrip items click event:
mnuItemShow.Click += new EventHandler(mnuItemShow_Click);
private void mnuItemShow_Click(object sender, EventArgs e)
{
}
But I can't figure out how to bind the Form Closing event..
I have tried it like this:
this.form.FormClosing += new EventHandler(closing);
private override void closing(EventArgs e)
{
}
But I get this error message:
No overload for 'closing' matches delegate 'System.EventHandler'
This works:
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Main_FormClosing);
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
}
it should be like this:
private void closing(object sender, FormClosingEventArgs e)
{
}
and you have to add handler like below
frm.FormClosing += new FormClosingEventHandler();

How to open a form only once?

I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried
private void SystemParametersClick(object sender, EventArgs e)
{
Xpan sp = new Xpan();
sp.CurrentItem = this.GetCaller(sender);
if (sp.Visible==false)
{
sp.Show();
}
}
It doesnt work because it is not the same instance. :(
How do i make it only pop once?
Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.
public class MainForm : Form
{
private Xpan _Xpan;
public MainForm()
{
InitializeComponent();
_Xpan = new Xpan();
}
private void SystemParametersClick(object sender, EventArgs e)
{
_Xpan.Show();
}
}
Maybe this simple approach would suffice?
private bool has_been_shown = false;
private void SystemParametersClick(object sender, EventArgs e)
{
if(!has_been_shown)
{
has_been_shown = true;
Xpan sp = new Xpan();
}
}
First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.
private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
Then define your Xpan form as a class member of the parent form, e.g.:
private readonly Xpan _sp = new Xpan();
And finally defile your Click handler this way:
private void SystemParametersClick(object sender, EventArgs e)
{
if (!_sp.Visible)
{
_sp.Show();
}
else
{
_sp.Activate();
}
}
That's it.

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;

C# .net framework2.0 how to dynamically assign methods to an event

VS2008, C#, .NET FRAMEWORK2.0
I want this: click button1, webbrowser1._DocumentCompleted() event revokes doA(); click button2, it revokes doB(); click button3, it revokes doC().
I know how to do it using JAVA and I guess C# has this mechanism too. Could anyone give me some idea or better, show me some example?
myButton.Click += myButton_Click;
protected void myButton_Click(object sender, EventArgs e) {}
To Add a handler
button.Click += buttonClickEventHandler;
To remove a handler
button.Click -= buttonClickEventHandler;
To add to these answers, you can also add an anonymous method to an event:
myButton.Click += (object sender, EventArgs e) => {
MessageBox.Show("MASSIVE ERROR!");
};
What this means is that you can effectively call a method even if it does not match the appropriate event handler signature:
myButton.Click += (object sender, EventArgs e) => {
DoA();
};
Or (without using a lamba expression):
myButton.Click += delegate(object sender, EventArgs e) {
DoA();
};
If you want to add event handlers to a control, which is what I think you are describing, you can easily do this. One common approach is to assign control event handlers in the code behind during page load:
protected void Page_Load(object sender, EventArgs e)
{
//add the event handler for the click event. You could provide other
//logic to determine dynamically if the handler should be added, etc.
btnButton1.Click += new EventHandler(btnButton1_Click);
btnButton2.Click += new EventHandler(btnButton2_Click);
btnButton3.Click += new EventHandler(btnButton3_Click);
}
protected void btnButton1_Click(object sender, EventArgs e)
{
//get the button, if you need to...
Button btnButton1 = (Button)sender;
//do some stuff...
DoA();
}
protected void btnButton2_Click(object sender, EventArgs e)
{
//do some stuff...
DoB();
}
protected void btnButton3_Click(object sender, EventArgs e)
{
//do some stuff...
DoC();
}
private void DoA() {}
private void DoB() {}
private void DoC() {}
Declaring an event
public class MyClass1
{
...
public event EventHandler<EventArgs> NotifyValidate;
protected void RaiseNotifyValidate(EventArgs e)
{
if (NotifyValidate != null)
{
NotifyValidate(this, e);
}
}
...
}
Firing that event in your code
...
RaiseNotifyValidate(new EventArgs()); //EventArgs could be more sophisticated, containing data etc..
Registering for that event in your code:
...
MyClass aObj = new MyClass();
aObj.NotifyValidate += new EventHandler(onNotifyValidate);
...
private void onNotifyValidate(object sender, EventArgs e)
{
//do what you need to
}
As Dan pointed out, with Lambda expressions you can define events like
aObj.NotifyValidate += (s,ev) =>
{
//handle your event
};

Categories

Resources