如何选择对象并收集价值? [英] How to select object and collect the value ?

查看:74
本文介绍了如何选择对象并收集价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个图片框,他们是可点击的,他们模仿骰子它基本上是傻瓜。

欲望是点击特定的图片框并从该对象获取价值

某事喜欢:如果单击按钮然后将此值保存在某个变量或数组中我假设...



我尝试过:



我只是不知道如何在事件处理程序上使用这个关键字或者这是错误的方法。一些帮助将不胜感激!

I have 5 picturebox and they are clickable they mimic dice its basically yatzy.
The desire is to click the specific picturebox and get the value from that object
something like: if button is clicked then keep this value stored in some variable or array i assume...

What I have tried:

I just don't get my head wrapped around this concept about how to get to use this keyword on event handlers or is that the wrong approach. Some help would be much appreciated !

推荐答案

当您将图像放入PictureBox时 - 为了显示哪个面朝上,我假设 - 将数值设置为PictureBox .Tag属性。所有控件都有一个标记 - 它可用于存储与该特定实例相关的任何内容。

当您想知道模具的值时,请查看Tag属性并将其转换为整数:

When you put the image in the PictureBox - to show which face is up, I assume - set the numeric value into the PictureBox.Tag property. All Controls have a Tag - it's available for you to store anything related to that particular instance in.
When you want to know the value of a die, look at the Tag property and cast it to an integer:
if (p.Tag != null)
   {
   int dieValue = (int)p.Tag
   ...
   }


我建​​议您使用词典:
I'd suggest you use a Dictionary:
// assume you have five PictureBoxes

Dictionary<PictureBox,int> Dice = new Dictionary<PictureBox,int>();

// in the Form load event:

foreach(PictureBox pb in this.Controls.OfType<PictureBox>())
{
   Dice.Add(pb, 0);
}

然后在PictureBoxes的Click Eventhandler中,您可以使用PictureBox作为Key来获取值。并且,随机生成新值并设置适当的图像变得容易。

Then in your Click Eventhandler for the PictureBoxes, you can get the value by using the PictureBox as the Key. And, randomly generating new values, and setting the appropriate images becomes easy.


这篇关于如何选择对象并收集价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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