[英]How can I change label Value by clicking in a picturebox in C#?
This is my first post in here and I m trying for 3-4 months now to learn c#.
这是我在这里的第一篇文章,我现在尝试3-4个月学习c#。 As the title says I have this simple method in a Guy class:
如标题所示,我在Guy类中有以下简单方法:
public void UpdateLabels()
{
MyLabel.Text = (Name + " has placed " + MyBet.Amount + "$ " + "on " );
}
In image as you can see the label updates by giving value to numericUp button but I want when I click a bear to get the name of the bear in label 在图像中,您可以看到通过向numericUp按钮赋值来更新标签,但是当我单击熊以在标签中获得熊的名称时,我想要
Thank you a lot . 非常感谢 。
You have to add the Click
event handler to the PictureBox
controls, like shown in the following compact Lambda-style notation sample: 您必须将
Click
事件处理程序添加到PictureBox
控件中,如下面的紧凑型Lambda样式符号示例所示:
img1.Click+=(s,e)=>{ UpdateLabels();}
where img1
is a PictureBox
control name of that one containing "Bear" image. 其中
img1
是包含“熊”图像的那个的PictureBox
控件名称。 The same technique you can apply to other PictureBox
controls. 您可以将相同的技术应用于其他
PictureBox
控件。
Note : you may need to modify the UpdateLabels(s,e)
to extract sender s
information (like: PictureBox _px = s as PictureBox
) and apply the updates to the corresponding Label
, thus, the event subscription will look like: 注意 :您可能需要修改
UpdateLabels(s,e)
来提取发件人s
信息(例如: PictureBox _px = s as PictureBox
),并将更新应用于相应的Label
,因此,事件订阅将如下所示:
img1.Click+=(s,e)=> UpdateLabels(s,e);
img2.Click+=(s,e)=> UpdateLabels(s,e);
.....
pointing to the same event handler (btw, in case of WPF I would recommend to use TextBlock
instead of Label
control; correspondingly it could be Image
control instead of PictureBox
). 指向相同的事件处理程序(顺便说一句,在WPF中,我建议使用
TextBlock
代替Label
控件;相应地,它可以是Image
控件而不是PictureBox
)。
Hope this may help. 希望这会有所帮助。
Double Click the PictureBoxes to get their Click()
event. 双击PictureBoxes以获取其
Click()
事件。 From there you can set the Label to the correct value: 在这里,您可以将Label设置为正确的值:
public void UpdateLabels()
{
MyLabel.Text = (Name + " has placed " + MyBet.Amount + "$ " + "on " + label1.Text);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
label1.Text = "Brown Bear";
UpdateLabels();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
label1.Text = "Red Bear";
UpdateLabels();
}
// ... etc ...
*Change label1
to the name of your label (which I hope is better named). *将
label1
更改为标签的名称(希望更好地命名)。
那是在我眼前。我试图将图片框视为错误的文本。我可以看到它们,但C#无法看到,因此我制作了文本,使C#可以看到它们并将其应用于图片框选择。谢谢大家您的时间,并帮助我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.