Please forgive me if this should be phrased differently in a programming context - I have no idea what to search for other than exactly what I used to title my question.

Context - I am attempting to build a word search game involving a 4x4 grid of random letters. I'm currently using a Label_Click event to change the background color of the label containing a given letter. In the future there will be many other actions than just this (evaluating whether an array of clicks forms a legitimate word, for instance), but I'm fundamentally stuck with this problem:

// pseudo-code

if (lblA1_wasClicked || lblB1_wasClicked || ... lblD4_wasClicked)
{ 
    whichever_wasClicked... //perform actions
}

Perhaps I need to learn about creating an event handler for this situation? I have never created a custom event handler, I just know what they are in theory. I am kind of stuck and at this point it's tough to know what to search for to keep learning. I know only enough to be dangerous, so please go easy on me if the answer is as straight-forward as my question feels. :)

有帮助吗?

解决方案

If you are in the ui, you can go to the properties of each label and set their Click handler to the same method. so if you have a method with a signature like this.

public void label_Click(object sender, EventArgs e)
{
}

Then assign that to each label and then use that to process each one. This is a naive example, and there are much better methods that use established patterns for creating windows forms applications. Check out this question or search for MVP pattern windows forms to get more info on it!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top