2012-08-01 129 views
0

我有一个功能,private void sentEmail()Form1。我想用这个函数来处理InputBox中的标签。创建标签点击输入框

,我使用InputBox是这样的:http://www.csharp-examples.net/inputbox/

我创造一个新的类,并添加的InputBox的代码。

在我的InputBox创造标签:

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

现在我需要做这样的:当出现的InputBox会,到时我会点击label2,使用的功能,从Form1label2是一个文本recive密码,功能工作,但我需要使用InputBox中的功能,但我不知道如何做到这一点。 我认为我需要创建一个新的处理程序,但我不知道如何。

回答

1

更改InputBox方法

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

而且回调添加到Click事件。

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

当调用InputBox方法,您现在可以通过一个方法,当用户点击该标签将在其上调用的方法。呼叫InputBox这样的:

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

非常感谢你!完美的作品! – AnDr3yy 2012-08-01 08:54:18

1

Label有一个Click事件。

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

功能已经寄出不是在输入框后级,在Form1中。不要以你的方式工作。 label2.Click + =委托{sentEmail(); } - 错误:;预计 – AnDr3yy 2012-08-01 08:30:00

+0

@ AnDr3yy您没有说出该标签是否在Form1中创建,或者不是我刚刚设想的。查看更新。 – James 2012-08-01 09:10:21

0

我建议你有SendMail()方法在其他类,而不是在Form1,你可能要到别的地方使用它。

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

而且在你的表格,你只需要调用它含有实例化它

//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(); 
} 
+0

Tnx为答案。我不尝试你的方法,因为BigYellowCactus的方法完美地工作。但tnx! – AnDr3yy 2012-08-01 08:55:22

+0

林间空地,你解决了这个问题;) – SidAhmed 2012-08-01 09:02:43