1. first look at the figure
2. The idea is simple:
Place a timer control on the form, add a label control to the form at a specified time interval, and use the label control to display randomly generated letters;
When you press the key, traverse all the label controls in the form. If the text value is the same as that of the keyboard, remove the label control;
Note: once found, the loop will be terminated; otherwise, all text values and the labels with the same letters will be clear;
Here, only the upper-case letter A-Z is applied, its ASCII code is 65-90, easy to generate random letters
3,Code
Using System;
Using System. drawing;
Using System. Windows. forms;
namespace printletter
{< br> Public partial class letter: form
{< br> Public letter ()
{< br> initializecomponent ();
}
private void letter_load ( Object sender, eventargs e)
{< br> /// A-Z 65-90
/// A-Z 97
timer1.start ();
}
Private Void Timereffectick ( Object Sender, eventargs E)
{
// Add a label control in the form.
// Assign random letters to the text attribute of the label.
Random rdletter = New Random ();
Label LBL = New Label ();
LBL. Text = Convert. tostring (( Char ) Rdletter. Next ( 65 , 90 ));
LBL. Location = New Point (rdletter. Next ( 10 , This . Clientsize. Width - 10 ), 0 );
LBL. Size = New Size ( 20 , 20 );
LBL. Font = New Font ( " " , 15f );;
This . Controls. Add (LBL );
Foreach (Control C In This . Controls)
{
Label lblcontrol = (Label) C;
Lblcontrol. Location = New Point (lblcontrol. Location. X,
Lblcontrol. Location. Y + 5 );
}
}
Private Void Letter_keyup ( Object Sender, keyeventargs E)
{
// Traverses all label controls in the current form,
// If the text value of the label control is equal to the value of the currently pressed key
// Then the control is removed and the total score is + 5.
Foreach (Control C In This . Controls)
{
Label lblcontrol = (Label) C;
If (Lblcontrol. Text = E. keycode. tostring ())
{
This . Controls. Remove (C );
This . Text = Convert. tostring ( Int . Parse ( This . Text) + 5 );
Break ;
}
}
}
}
}