Reputation: 239
I have a function private void change ()
that I want to run when I will press a button. I have Button schimbare = new Button();
and if I will press, to run the function.
I try schimbare.Click += change();
but don't work. What is the good comand ?
This is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace Programarii
{
class InputBoxOptiuni
{
static TextBox textBox1 = new TextBox();
/// <summary>
/// Displays a dialog with a prompt and textbox where the user can enter information
/// </summary>
/// <param name="title">Dialog title</param>
/// <param name="promptText">Dialog prompt</param>
/// <param name="value">Sets the initial value and returns the result</param>
/// <returns>Dialog result</returns>
public static DialogResult Show(string title, string promptText, string informati, string mesaj, ref int ora, ref int minut30, ref int minut15, ref int douaore, ref int minut10, ref int minut5, ref int pornire2, ref int anuntare2, ref int cuparola, ref string parola, ref string email, ref int expirare, ref int cateminute, ref int vl, ref int culimba, string scurtaturi, string scurtaturi2, string format, ref int tipformat)
{
Button schimbare = new Button();
schimbare.Click += change;
}
private void change(object sender, EventArgs e)
{
}
}
}
For all that answer me, tnx.
I try with:
private void change(object sender, EventArgs e) and schimbare.Click += change;
but don't work. I try with with schimbare.Click += (s,e)=> { //your code };
and works !
Upvotes: 1
Views: 526
Reputation: 1258
You need to catch an event like this:
private void schimbare_Click(object sender, EventArgs e)
{
change();
}
Hope it helps...
Upvotes: 0
Reputation: 3528
Wait, wait!!! You're adding return value of "change" method to the Click event! So changing the code to this should solve your problem (parentheses are extra!). I don't know the platform you are working in, please be advised that the delegate of click must have some arguments.
Cheers
schimbare.Click += change;
Upvotes: 0
Reputation: 116118
Your methods signature should be something like this:
void change(object sender, EventArgs e)
and write as schimbare.Click += change;
you can also use this syntax
schimbare.Click += (s,e)=>
{
//your code
};
Upvotes: 3
Reputation: 498992
Drop the ()
- you are subscribing to an event handler, not invoking it (using the ()
will call change
).
schimbare.Click += change;
Note that (apart from the non-standard naming), your change
function should have the EventHandler
signature (which it doesn't have - it does not take parameters), change it to:
private void change(object sender, EventArgs e)
Upvotes: 2