Frage

I have a function, private void sentEmail() in Form1. And I want to use that function to a label in a InputBox.

The InputBox that I use is this: http://www.csharp-examples.net/inputbox/

I creat a new class and add the code of InputBox.

In InputBox I creat the label:

Label label2 = new Label();
label2.Text = mesaj;
label2.Cursor = Cursors.Hand;

Now I need to make this: when the InputBox will appears, when I will click on label2, to use the function from Form1. The label2 is a text for recive the password, function work, but I need to use the function in InputBox but I don't know how to do this. I think that I need to creat a new Handler, but I don't know how.

War es hilfreich?

Lösung

Change the InputBox method to

public static DialogResult InputBox(string title, string promptText, ref string value, Action labelClickCallback)

and add that callback to the Click event.

Label label2 = new Label();
label2.Text = mesaj;
label2.Cursor = Cursors.Hand;
label2.Click += (s, e) => labelClickCallback(); // Create a new event handler

When calling the InputBox method, you can now pass a method to the method which will be called when the user clicks on the label. Call InputBox like this:

InputBox("Title", "Text", ref result, sentEmail);

Andere Tipps

Label has a Click event.

label2.Click += delegate {
    form1.sendEmail();
}

I suggest you have the SendMail() method in an other class, not in Form1, you may want to use it somewhere else.

public class MailController
{
    public void SendEmail()
    {
        //Code
    }
}

And in your form, you just call it after instantiate the class containing it

//Your form containing the InputBox
private void Form1_Load(object sender, EventArgs e)
{
    Label label2 = new Label();
    label2.Text = mesaj;
    label2.Cursor = Cursors.Hand;

    label2.Click += OnLabelClick;
}

private void OnLabelClick(object sender, EventArgs e)
{
    MailController controller = new MailController();
    controller.SendMail();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top