很容易的问题,我假设.. [英] Pretty easy question, i'd assume..

查看:76
本文介绍了很容易的问题,我假设..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个表单,用于输入一个名称,按钮中的字母从A到Z.


当你点击一个按钮,它应该将与该按钮相关联的文本附加到文本框中。


而不是每个按钮都有一个带有单击事件的EventHandler

of:


private void btnA_Click(object sender,System.EventArgs e)

{

txtNameField.AppendText(" A");

}


依旧...


是有没有办法缩短这个,比如只有1个事件处理程序

所有按钮?我正在思考这个问题:


private void buttonLetter_Click(object sender,System.EventArgs e)

{

txtNameField.AppendText(letter);

}


private void AppendLetter(letter)

{

txtNameField.AppendText(letter);

}


我希望你能意识到我在这里所说的话。

解决方案

Brian< br ************ @ gmail.com>写道:

我有一个表格用于输入一个名字,按钮中的字母从A到Z.

当你点击一个按钮时,它应该附加与该按钮到一个文本框。

而不是每个按钮都有一个EventHandler点击事件


private void btnA_Click(object sender, System.EventArgs e)
{
txtNameField.AppendText(" A");
}

等等...
有没有办法缩短这个,例如只有1个事件处理程序
所有按钮?




绝对。使用一个事件处理程序,并使用发件人;参数

找出按下了哪个按钮。


-

Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件




" Brian"写道......

我有一个表单,用于输入一个名称,按钮中的字母从A到Z.

当你点击一个按钮时,它应该附加与文本框中的按钮相关联的文本。

而不是每个按钮都有一个带有点击事件的EventHandler


私有void btnA_Click(object sender,System.EventArgs e)
{txtNameField.AppendText(" A");
}

依此类推...

有没有办法缩短这个,例如只有1个事件处理程序
所有按钮?




我想这个你有每个字母作为按钮的标签吗?


在这种情况下你可以有一个方法:


private void buttonLetter_Click(object sender,System.EventArgs e)

{

按钮b =(按钮)发件人;

txtNameField.AppendText(b.Text); < br $>
}


如果您的标签上还有其他东西,那么可能会使用按钮的标签

属性。


// Bjorn A

而不是使用DoubleClick按钮创建事件处理程序。您可以在属性窗口中使用名称中的事件处理程序bij键入

.


在evenhandler中,您可以将sender参数类型转换为按钮并且

读取

描述文本或标签等属性。


string letter =((Button)Sender).Tag

Hello all,

I have an form to Enter a Name with letters from A to Z in buttons.

When you click a button, it should append the text associated with
that button to a text box.

Instead of having an EventHandler for every button with a click event
of:

private void btnA_Click(object sender, System.EventArgs e)
{
txtNameField.AppendText("A");
}

And so on...

Is there a way to shorten this, such as have just the 1 Event Handler
for all buttons? I was thinking something along the lines of this:

private void buttonLetter_Click(object sender, System.EventArgs e)
{
txtNameField.AppendText(letter);
}

private void AppendLetter(letter)
{
txtNameField.AppendText(letter);
}

I hope you realise what i''m talking about here.

解决方案

Brian <br************@gmail.com> wrote:

I have an form to Enter a Name with letters from A to Z in buttons.

When you click a button, it should append the text associated with
that button to a text box.

Instead of having an EventHandler for every button with a click event
of:

private void btnA_Click(object sender, System.EventArgs e)
{
txtNameField.AppendText("A");
}

And so on...

Is there a way to shorten this, such as have just the 1 Event Handler
for all buttons?



Absolutely. Use one event handler, and use the "sender" parameter to
find out which button was pressed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



"Brian" wrote...

I have an form to Enter a Name with letters from A to Z in buttons.

When you click a button, it should append the text associated with
that button to a text box.

Instead of having an EventHandler for every button with a click event
of:

private void btnA_Click(object sender, System.EventArgs e)
{
txtNameField.AppendText("A");
}

And so on...

Is there a way to shorten this, such as have just the 1 Event Handler
for all buttons?



I guess that you have each letter as the Label of the buttons?

In that case you could have a method like:

private void buttonLetter_Click(object sender, System.EventArgs e)
{
Button b = (Button) sender;
txtNameField.AppendText(b.Text);
}

If you have something else in your labels, you could possibly use the Tag
property of the buttons instead.

// Bjorn A


Instead of DoubleClick on yout button to create an eventhandler. You can make
an eventhandler bij typing in a name in the propertieswindow.

In the evenhandler you could typecast the sender parameter to a button and
read a
describing property like Text or Tag.

string letter = ((Button)Sender).Tag


这篇关于很容易的问题,我假设..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆