简单的东西。该应用程序将12个不同单元格中的12个GIF随机放入TableLayoutPanel中。所有都是隐藏的,但是如果你点击它们,它们就会出现。
如果我将可见性从开头设置为true,它们会显示出来,但一旦隐藏,无论如何,它们都不会重新出现。有什么想法吗?
搜索高低,但没有成功。
提前致谢
public partial class Form1 : Form
{
// Use this Random object to choose random icons for the squares
Random random = new Random();
// Create timer int
private int counter = 0;
// Each of these letters is an interesting icon
// in the Webdings font,
// and each icon appears twice in this list
List<string> pictures = new List<string>()
{
"images/baneling.gif",
"images/baneling.gif",
"images/scv.gif",
"images/scv.gif",
"images/marine.gif",
"images/marine.gif",
"images/probe.gif",
"images/probe.gif",
"images/zealot.gif",
"images/zealot.gif",
"images/zergling.gif",
"images/zergling.gif"
};
// firstClicked points to the first Label control
// that the player clicks, but it will be null
// if the player hasn't clicked a label yet
PictureBox firstClicked = null;
// secondClicked points to the second Label control
// that the player clicks
PictureBox secondClicked = null;
public Form1()
{
InitializeComponent();
AssignPicturesToSquares();
}
/// <summary>
/// Assign each icon from the list of icons to a random square
/// </summary>
private void AssignPicturesToSquares()
{
// The TableLayoutPanel has 16 labels,
// and the icon list has 16 icons,
// so an icon is pulled at random from the list
// and added to each label
foreach (Control control in tableLayoutPanel1.Controls)
{
PictureBox currentPicture = control as PictureBox;
if (currentPicture != null)
{
int randomNumber = random.Next(pictures.Count);
currentPicture.Image = Image.FromFile(pictures[randomNumber]);
currentPicture.Visible = true; // hide images after generation
pictures.RemoveAt(randomNumber);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void picture_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
return;
PictureBox clickedPicture = sender as PictureBox;
clickedPicture.Visible = true;
if (clickedPicture != null)
{
if (clickedPicture.Visible == true)
return;
if (firstClicked == null)
{
firstClicked = clickedPicture;
firstClicked.Visible = true;
return;
}
secondClicked = clickedPicture;
secondClicked.Visible = true;
// Check to see if the player won
CheckForWinner();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
return;
}
timer1.Start();
}
}
/// <summary>
/// This timer is started when the player clicks
/// two icons that don't match,
/// so it counts three quarters of a second
/// and then turns itself off and hides both icons
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
// Stop the timer
timer1.Stop();
// Hide both icons
firstClicked.Visible = false;
secondClicked.Visible = false;
// Reset firstClicked and secondClicked
// so the next time a label is
// clicked, the program knows it's the first click
firstClicked = null;
secondClicked = null;
}
/// <summary>
/// Check every icon to see if it is matched, by
/// comparing its foreground color to its background color.
/// If all of the icons are matched, the player wins
/// </summary>
private void CheckForWinner()
{
// Go through all of the labels in the TableLayoutPanel,
// checking each one to see if its icon is matched
foreach (Control control in tableLayoutPanel1.Controls)
{
PictureBox currentPicture = control as PictureBox;
if (currentPicture != null)
{
if (currentPicture.Visible == false)
return;
}
}
// If the loop didn’t return, it didn't find
// any unmatched icons
// That means the user won. Show a message and close the form
MessageBox.Show("You matched all the pictures in just " + counter + " seconds!","Congratulations");
Close();
}
private void timer2_Tick(object sender, EventArgs e)
{
counter++;
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
答案 0 :(得分:1)
您无法单击不可见的控件。鼠标事件转到TLP。因此,为其MouseClick事件实现一个处理程序以找回控件。像这样:
private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e) {
foreach (Control ctl in tableLayoutPanel1.Controls) {
if (ctl.Bounds.Contains(e.Location)) {
ctl.Visible = true;
break;
}
}
}