我试图在c#中创建一个项目,主要是一个记忆游戏。这一切都很顺利,直到我遇到了一个我没见过其他人的问题,这真让我感到困惑,因为我不认为我做了一件非常复杂的事情。在我的" Form"是16个标签,点击时都有一个方法。当我运行我的程序时,点击部分永远不会发生。按钮工作正常,点击它们时开始逐行调试。但是在标签上它只是什么都不做,从不调用它。这里有一些可能需要的东西:
namespace MatchingGame1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label16 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(567, 57);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(97, 69);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(570, 152);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(93, 81);
this.button2.TabIndex = 2;
this.button2.Text = "Hide";
this.button2.UseVisualStyleBackColor = true;
this.button2.Visible = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label5
//
this.label5.Dock = System.Windows.Forms.DockStyle.Fill;
this.label5.Location = new System.Drawing.Point(5, 129);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(125, 125);
this.label5.TabIndex = 4;
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label5.Click += new System.EventHandler(this.label1_Click);
//
// label7
//
this.label7.Dock = System.Windows.Forms.DockStyle.Fill;
this.label7.Location = new System.Drawing.Point(271, 129);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(125, 125);
this.label7.TabIndex = 6;
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label7.Click += new System.EventHandler(this.label1_Click);
//
// label9
//
this.label9.Dock = System.Windows.Forms.DockStyle.Fill;
this.label9.Location = new System.Drawing.Point(5, 256);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(125, 125);
this.label9.TabIndex = 8;
this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label9.Click += new System.EventHandler(this.label1_Click);
//
// label10
//
this.label10.Dock = System.Windows.Forms.DockStyle.Fill;
this.label10.Location = new System.Drawing.Point(138, 256);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(125, 125);
this.label10.TabIndex = 9;
this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label10.Click += new System.EventHandler(this.label1_Click);
//
// label11
//
this.label11.Dock = System.Windows.Forms.DockStyle.Fill;
this.label11.Location = new System.Drawing.Point(271, 256);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(125, 125);
this.label11.TabIndex = 10;
this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label11.Click += new System.EventHandler(this.label1_Click);
//
// label12
//
this.label12.Dock = System.Windows.Forms.DockStyle.Fill;
this.label12.Location = new System.Drawing.Point(404, 256);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(125, 125);
this.label12.TabIndex = 11;
this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label12.Click += new System.EventHandler(this.label1_Click);
//
// label13
//
this.label13.Dock = System.Windows.Forms.DockStyle.Fill;
this.label13.Location = new System.Drawing.Point(5, 383);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(125, 127);
this.label13.TabIndex = 12;
this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label13.Click += new System.EventHandler(this.label1_Click);
//
// label14
//
this.label14.Dock = System.Windows.Forms.DockStyle.Fill;
this.label14.Location = new System.Drawing.Point(138, 383);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(125, 127);
this.label14.TabIndex = 13;
this.label14.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label14.Click += new System.EventHandler(this.label1_Click);
//
// label15
//
this.label15.Dock = System.Windows.Forms.DockStyle.Fill;
this.label15.Location = new System.Drawing.Point(271, 383);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(125, 127);
this.label15.TabIndex = 14;
this.label15.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label15.Click += new System.EventHandler(this.label1_Click);
//
// label16
//
this.label16.Dock = System.Windows.Forms.DockStyle.Fill;
this.label16.Location = new System.Drawing.Point(404, 383);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(125, 127);
this.label16.TabIndex = 15;
this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label16.Click += new System.EventHandler(this.label1_Click);
//
// label6
//
this.label6.Dock = System.Windows.Forms.DockStyle.Fill;
this.label6.Location = new System.Drawing.Point(138, 129);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(125, 125);
this.label6.TabIndex = 5;
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label6.Click += new System.EventHandler(this.label1_Click);
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Location = new System.Drawing.Point(5, 2);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(125, 125);
this.label1.TabIndex = 0;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.Dock = System.Windows.Forms.DockStyle.Fill;
this.label2.Location = new System.Drawing.Point(138, 2);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(125, 125);
this.label2.TabIndex = 1;
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label2.Click += new System.EventHandler(this.label1_Click);
//
// label3
//
this.label3.Dock = System.Windows.Forms.DockStyle.Fill;
this.label3.Location = new System.Drawing.Point(271, 2);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(125, 125);
this.label3.TabIndex = 2;
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label3.Click += new System.EventHandler(this.label1_Click);
//
// label4
//
this.label4.Dock = System.Windows.Forms.DockStyle.Fill;
this.label4.Location = new System.Drawing.Point(404, 2);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(125, 125);
this.label4.TabIndex = 3;
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label4.Click += new System.EventHandler(this.label1_Click);
//
// label8
//
this.label8.Dock = System.Windows.Forms.DockStyle.Fill;
this.label8.Location = new System.Drawing.Point(404, 129);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(125, 125);
this.label8.TabIndex = 7;
this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label8.Click += new System.EventHandler(this.label1_Click);
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.HotTrack;
this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Inset;
this.tableLayoutPanel1.ColumnCount = 4;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Controls.Add(this.label8, 3, 1);
this.tableLayoutPanel1.Controls.Add(this.label13, 0, 3);
this.tableLayoutPanel1.Controls.Add(this.label14, 1, 3);
this.tableLayoutPanel1.Controls.Add(this.label15, 2, 3);
this.tableLayoutPanel1.Controls.Add(this.label16, 3, 3);
this.tableLayoutPanel1.Controls.Add(this.label12, 3, 2);
this.tableLayoutPanel1.Controls.Add(this.label11, 2, 2);
this.tableLayoutPanel1.Controls.Add(this.label10, 1, 2);
this.tableLayoutPanel1.Controls.Add(this.label9, 0, 2);
this.tableLayoutPanel1.Controls.Add(this.label5, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.label6, 1, 1);
this.tableLayoutPanel1.Controls.Add(this.label7, 2, 1);
this.tableLayoutPanel1.Controls.Add(this.label4, 3, 0);
this.tableLayoutPanel1.Controls.Add(this.label3, 2, 0);
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.label2, 1, 0);
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 4;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(534, 512);
this.tableLayoutPanel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
this.ClientSize = new System.Drawing.Size(671, 512);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Matching Game";
this.tableLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
}
}
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MatchingGame1
{
public partial class Form1 : Form
{
Random random = new Random();
private List<Image> takeIm()
{
string directory = @".\";
List<Image> ImageList = new List<Image>();
foreach (string myFile in Directory.GetFiles(directory, "*.jpg", SearchOption.AllDirectories))
{
ImageList.Add(Image.FromFile(myFile));
ImageList.Add(Image.FromFile(myFile));
}
return ImageList;
}
private void AssignImagesToSquares(List<Image> a)
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label imageLabel = control as Label;
if (imageLabel != null)
{
int randomNumber = random.Next(a.Count);
imageLabel.Image = a[randomNumber];
a.RemoveAt(randomNumber);
}
}
}
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Label clickedLabel = sender as Label;
clickedLabel.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
List<Image> a = new List<Image>();
a = takeIm();
AssignImagesToSquares(a);
button2.Visible = true;
button2.Enabled = true;
button1.Visible = false;
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button2.Visible = false;
foreach (Control control in tableLayoutPanel1.Controls)
{
Label imageLabel = control as Label;
imageLabel.Visible = false;
}
}
}
}
答案 0 :(得分:0)
我认为您的标签在尝试点击之前已隐藏
如果属性Click
设置为Visible
,则控件不会触发事件false
。
如果要通过单击显示/隐藏控件,请尝试在控件中播放背景颜色或图片
Hidden
- 没有图片/透明
Visible
- 将显示图片或一些背景颜色