我正在创造一笔交易或没有交易游戏。我有22个图片框。一侧11个,另一侧11个。使用用户可以在中间选择的框。当选中包含该金额的方框时,我不想删除或禁用相关的图片框。框中包含的金额是随机创建的。所以说例如方框15包含100000.我想删除包含100,000英镑图像的图片框。有什么方法可以做到这一点吗?
这是我的全局数组等:
double[] box = new double[22];
double[] values = new double[22] {0.01, 0.10, 0.50, 1, 5, 10, 50, 100, 250, 500, 750, 1000, 3000, 5000, 10000, 15000, 20000, 35000, 50000, 75000, 100000, 250000};
//PictureBox[] picturbox = new PictureBox[22];
Random myrand = new Random();
bool start = true;
//bool pick = true;
int count = 0;
以下是我创建随机值的代码:
private void frm_dealornodeal_Shown(object sender, EventArgs e)
{
txt_bankersOffer.Hide(); // hiding text until box is picked
txt_offer.Hide(); // hiding text until box is picked
bool Found = false;
int Rand_Loc = myrand.Next(22); //random number generator for the boxes
box[0] = values[Rand_Loc];
values[Rand_Loc] = 0;
for (int x = 1; x < 22; x++ )
{
Found = true;
while (Found)
{
Found = false;
Rand_Loc = myrand.Next(22);
if (values[Rand_Loc] != 0)
{
box[x] = values[Rand_Loc];
values[Rand_Loc] = 0;
}
else
{
Found = true;
}
}
}
}
&安培;最后这里是我们选择按钮点击事件的代码。所有其他框的代码是相同的。
private void btn_box_2_Click(object sender, EventArgs e)
{
if (start == true)
{
start = false;
pic_pick.Hide();
btn_box_2.Location = new Point(584, 543);
}
else
{
txt_boxContentsText.Show();
txt_boxcontentsnuber.Show();
txt_bankersOffer.Hide();
txt_offer.Hide();
txt_boxcontentsnuber.Text = Convert.ToString(box[2 - 1]);
btn_box_2.Enabled = false;
btn_box_2.BackColor = Color.Gray;
count++;
}
if (count == 5 || count == 8 || count == 11 || count == 14 || count == 17 || count == 20)
{
txt_boxContentsText.Show();
txt_boxcontentsnuber.Show();
txt_bankersOffer.Show();
txt_offer.Show();
txt_offer.Text = Convert.ToString(1000);
}
}
虽然我在这里可以有人想出一个好的配方,我可以用来给他们银行家的优惠吗?
干杯。
形式图片: (你不能看到左边的最后一个图片框750英镑或右边250,000英镑,但他们在那里。)
答案 0 :(得分:1)
您可以利用Control.Tag
财产。每个按钮的标签都可以包含相应PictureBox
的名称。然后,您可以在单击事件中禁用图片框。如果它们都做同样的事情,你也不需要22个单独的点击事件,因为object sender
参数告诉你点击了哪个按钮,Tag
属性会告诉你哪个PictureBox
禁用
要解决按钮值的随机分配问题,这里有一个解决方案:
为表示值的每个PictureBox.Tag
属性分配一个值(因为在我理解的情况下,PictureBox不会更改,只有按钮才会更改)。因此,如果值为0.01,则设为Tag值;如果它是0.50,则将0.5设置为Tag值; 1.0应设置为1,依此类推。
按名称区分“手提箱”按钮,例如“Suitcase1”,“Suitcase2”等。
使用以下技术将这些值随机分配给按钮(您可以在Form_Load事件中调用此函数):
private void assignButtonValues()
{
Random random = new Random();
List<double> values = new List<Double>( new[] {0.01, 0.10, 0.50, 1, 5, 10, 50, 100, 250, 500, 750, 1000, 3000, 5000, 10000, 15000, 20000, 35000, 50000, 75000, 100000, 250000} );
foreach (Button button in Controls.OfType<Button>())
{
//Is this a suitcase or just some button in the game?
if (button.Name.Contains("Suitcase"))
{
//select random value and remove it from the list of values to ensure it's assigned to only one button
double buttonValue = values[random.Next(0, values.Count)];
values.Remove(buttonValue);
//TODO: Your code to save the value in an array, etc, for later processing, etc.
//Assign corresponding picture to this button's tag
button.Tag = Controls.OfType<PictureBox>()
.Where<PictureBox>(p => p.Tag.ToString() == buttonValue.ToString())
.First<PictureBox>().Name;
}
}
}
使用processButtons
函数作为所有按钮的事件处理程序:
private void processButtons(object sender, EventArgs e)
{
//Get the clicked button
Button clickedButton = (Button)sender;
string correspondingPictureBoxName = clickedButton.Tag.ToString();
//Get the corresponding PictureBox
PictureBox correspondingPictureBox = (PictureBox)Controls.Find(correspondingPictureBoxName, true).First<Control>();
//Hide the PictureBox
correspondingPictureBox.Visible = false;
}