C# 将按下的图片框连接到某个字母
我的程序中有一些问题是混乱的单词,用户试图将它们解开。我希望他们通过点击多张图片来完成这项工作,这些图片上有特定的字母,形成单词,但我不知道如何将图片连接到特定的字母(例如,假设我有一张字母“a”的图片,我如何连接它,以便程序知道当按下该图片时,字母a被按下) 这是我到目前为止所做的编码(将问题随机化并将答案与之链接) *此外,我对编码非常陌生,因此,请您提供任何建议,以明确说明要添加/更改我的代码的内容C# 将按下的图片框连接到某个字母,c#,winforms,C#,Winforms,我的程序中有一些问题是混乱的单词,用户试图将它们解开。我希望他们通过点击多张图片来完成这项工作,这些图片上有特定的字母,形成单词,但我不知道如何将图片连接到特定的字母(例如,假设我有一张字母“a”的图片,我如何连接它,以便程序知道当按下该图片时,字母a被按下) 这是我到目前为止所做的编码(将问题随机化并将答案与之链接) *此外,我对编码非常陌生,因此,请您提供任何建议,以明确说明要添加/更改我的代码的内容 public partial class Level1 : Form { Rand
public partial class Level1 : Form
{
Random rnd = new Random(); //sets the random variable
List<string> strStrings = new List<string>() { "ZUZB", "HXAO", "MXAE", "KYCU", "CWEH", "PHIC", "HOCP", "SXIA", "ISHF", "KOJE" };//displays wprds om a list
Dictionary<string, string> dictStrings = new Dictionary<string, string>() //dictionary containing the word as the key, and the answer as the value in the key/value pair.
{
{ "ZUZB", "BUZZ" },
{ "HXAO", "HOAX" },
{ "MXAE", "EXAM" },
{ "KYCU", "YUCK" },
{ "CWEH", "CHEW" },
{ "PHIC", "CHIP" },
{ "HOCP", "CHOP" },
{ "SXIA", "AXIS" },
{ "ISHF", "FISH" },
{"KOJE", "JOKE" }
};
public Level1()
{
InitializeComponent();
}
int skip; //declares the skip variable
int score; //declares the score variable
int question; //decalres the question variable
private void nextButton_Click(object sender, EventArgs e)
{
if (strStrings.Count > 0)
{
string rndWord = strStrings[rnd.Next(0, strStrings.Count())];
lbljumble.Text = rndWord;
strStrings.Remove(rndWord);
}
else
{
lbljumble.Text = "No more questions!";
}
answerLabel.Text = ""; //randomly displays questions in the label until there are no more questions left to ask
score += 10; //add 10 to score and display in label
lblscore.Text = Convert.ToString(score);
question += 1; //add one to question number and display in label
lblqnum.Text = Convert.ToString(question);
tmrtime.Interval = (tmrtime.Interval) - 100; //amount of time taken after each question
}
private void answerButton_Click(object sender, EventArgs e)
{
string answer = (dictStrings.TryGetValue(lbljumble.Text, out answer)) ? answer : "";
answerLabel.Text = answer; //displays answer in label after the answer button is pressed to display the corresponding answer to the question
}
private void btnskip_Click(object sender, EventArgs e)
{
skip = 1; //skip equals 1
if (skip == 1) //of skip equals one
{
skip--; //take one from skip
lblskip.Text = " remaining: no"; //display that no skips are available
}
else
{
lblskip.Text = "No skips remaining"; //display no skips remaining
}
}
}
公共部分类级别1:表单
{
Random rnd=new Random();//设置随机变量
List strStrings=new List(){“ZUZB”、“HXAO”、“MXAE”、“KYCU”、“CWEH”、“PHIC”、“HOCP”、“SXIA”、“ISHF”、“KOJE”};//显示列表中的WPRD
Dictionary dictStrings=new Dictionary()//包含单词作为键,答案作为键/值对中的值的字典。
{
{“ZUZB”,“BUZZ”},
{“HXAO”,“HOAX”},
{“MXAE”,“考试”},
{“KYCU”,“恶心”},
{“CWEH”,“CHEW”},
{“PHIC”,“CHIP”},
{“HOCP”,“CHOP”},
{“SXIA”,“AXIS”},
{“ISHF”,“FISH”},
{“KOJE”,“笑话”}
};
公共级别1()
{
初始化组件();
}
int skip;//声明skip变量
int score;//声明score变量
int-question;//删除问题变量
private void nextButton_Click(对象发送方,事件参数e)
{
如果(strString.Count>0)
{
字符串rndWord=strStrings[rnd.Next(0,strStrings.Count())];
lbljumble.Text=rndWord;
strStrings.移除(rndWord);
}
其他的
{
lbljumble.Text=“没有更多问题!”;
}
answerLabel.Text=“”;//在标签中随机显示问题,直到没有问题可问为止
分数+=10;//将10添加到分数并显示在标签中
lblscore.Text=Convert.ToString(分数);
问题+=1;//将一个问题添加到问题编号并显示在标签中
lblqnum.Text=Convert.ToString(问题);
tmrtime.Interval=(tmrtime.Interval)-100;//每个问题后花费的时间量
}
私有无效应答按钮\单击(对象发送者,事件参数e)
{
字符串答案=(dictStrings.TryGetValue(lbljumble.Text,out-answer))?答案:“”;
answerLabel.Text=answer;//按下“答案”按钮后,在标签中显示答案,以显示问题的相应答案
}
私有无效btnskip_单击(对象发送者,事件参数e)
{
skip=1;//skip=1
如果(skip==1)//的skip等于1
{
skip--;//从skip中取一个
lblskip.Text=“剩余:否”//显示没有可用的跳过
}
其他的
{
lblskip.Text=“无剩余跳过”;//显示无剩余跳过
}
}
}
}您可以添加一些图片框,并将它们命名为
字母a\u pb
,字母b\u pb
等,创建单击事件,然后创建if
语句以检查单击了哪个图片框,以及它是否等于字母“a”
例如:
string question = "a";
private void Letter_a_pb_Click(object sender, EventArgs e)
{
if (question == "a")
MessageBox.Show("Excellent!");
else
MessageBox.Show("Try Again!");
}
让我知道这是否是您搜索的内容。如果我正确理解您的问题,您想知道用户选择/单击了哪个字母
您可以动态创建图片框,将图片框的.Name
指定给希望图片框具有的值,然后为每个图片框订阅单击
事件
在单击事件中,检查发送图片框的名称对象发送者
。(如果您不想使用pictureBx.Name
,也可以使用pictureBx.Tag
)
下面是一个示例表单
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
lblAnswer.Text = "";
DrawLeters();
}
string checkAnswer = "check";
void DrawLeters()
{
this.SuspendLayout();
string[] alphabet = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".Split(",");
var pictureLocation = new Point(0, 0);
for (int i = 0; i < alphabet.Length; i++)
{
//Create a picture box
PictureBox pictureBx = new PictureBox();
//set the default location of the box (0,0) in this scenario
pictureBx.Location = pictureLocation;
//make the box 16 by 16
pictureBx.Size = new Size(16, 16);
//set the name of the box to that of the letter it represents
pictureBx.Name = alphabet[i];
//assign a click event to the box
pictureBx.Click += PictureBx_Click;
//now create the image that will fill the box
Image img = new Bitmap(16, 16);
using (Graphics graph = Graphics.FromImage(img))
{
graph.Clear(Color.White);
Brush textBrush = new SolidBrush(Color.Black);
graph.DrawString(pictureBx.Name, this.Font, textBrush, 0, 0);
graph.Save();
}
//assign the image to the box
pictureBx.Image = img;
//add the box to the form
this.Controls.Add(pictureBx);
//change the location for the next box
pictureLocation.X += 17;
if (i % 10 == 0 && i > 0)
{
pictureLocation.Y += 17;
pictureLocation.X = 0;
}
}
this.ResumeLayout(false);
this.PerformLayout();
}
private void PictureBx_Click(object sender, EventArgs e)
{ // assign the clicked value to the answer label
if (sender is PictureBox pbx)
lblAnswer.Text += pbx.Name;
}
private void checkAnswerBtn_Click(object sender, EventArgs e)
{
//check the answer
if (lblAnswer.Text == checkAnswer)
lblFeedback.Text = "Correct!!";
else
lblFeedback.Text = "NO!!";
}
private void clearBtn_Click(object sender, EventArgs e)
{
//clear the answer label
lblAnswer.Text = "";
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
lblAnswer.Text=“”;
拉拔器();
}
字符串checkAnswer=“check”;
无效提款人()
{
这个.SuspendLayout();
string[]alphabet=“a、b、c、d、e、f、g、h、i、j、k、l、m、n、o、p、q、r、s、t、u、v、w、x、y、z”。拆分(“,”;
var pictureLocation=新点(0,0);
for(int i=0;i0)
{
图片位置Y+=17;
pictureLocation.X=0;
}
}
此选项为.resume布局(false);
这个。执行布局();
}
私有无效图片单击(o)