textbox,user cannot enter letter

Pavle

Well-known member
Joined
Oct 4, 2016
Messages
47
Programming Experience
1-3
How to make that when user enter word using given random letters from 12 labels in textbox,user cannot enter letter that does not exist in given letters!!!
 
You can handle the KeyDown event, test whether the depressed key combination represents a character not allowed and then suppress the KeyPress event to discard the character, e.g.
private string word = "Hello";

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    // Get the Keys value corresponding to each character in the word.
    var allowedKeys = word.Select(ch => (Keys) Convert.ToInt32(char.ToUpper(ch)));

    // Suppress the KeyPress event, i.e. discard the character, if the user is entering an upper- or lower-case version of a character not included in the word.
    e.SuppressKeyPress = !allowedKeys.Contains(e.KeyCode) && new[] {Keys.None, Keys.Shift}.Contains(e.Modifiers);
}
 
By way of explanation:

The Keys value for letter keys corresponds to the Unicode value for the upper-case version of the letter, e.g. Keys.A has a numeric value of 65 and the Unicode value of A is also 65. That means that the first line gets a list of Keys values that correspond to the letters in the word.

In the KeyDown event handler, e.KeyCode refers to the key that was just depressed and e.Modifiers refers to the modifier keys that were depressed at the time. For the user to be entering a letter, e.Modifiers must be either Keys.None, i.e. no modifier keys, or Keys.Shift, i.e. just the Shift key. If e.KeyCode is not one of the allowed keys and there are no modifiers or just the Shift key depressed then the key combination does not correspond to an allowed letter so the character should be discarded, which the second line does.
 
How to also make that user cannot enter same letter several times,for example if there is one 'A' in the word he can only type it once,if there is three 'A' he can only type it three times and how to allow to use backspace?
 
How to also make that user cannot enter same letter several times,for example if there is one 'A' in the word he can only type it once,if there is three 'A' he can only type it three times and how to allow to use backspace?

Did you think about it at all? There's no magic to it. You know how to ignore a letter so you need to count how many times the current letter is already present and whether that is already the number of times that letter appears in the word. Give it go. You don't know that you can't do it if you don't give it a try. You can always post here if what you try doesn't work but if you haven't tried anything then you haven't really tried.
 
Don't know how to do what exactly? Have you made any effort to break the problem down into smaller parts at all? I don't know how to build a house but, if I wanted to do it, I wouldn't just ask someone how to build a house. I'd break it down into parts and then those parts into smaller parts. I'd then tackle each part individually, researching each part, trying to implement the ones I could and then asking for help with only the specific parts I needed help with and each one individually. That's what you should be doing here.

The first thing to do with any programming problem is to forget that it's a programming problem. If you had been asked to do this manually, with pen and paper, I'll wager that you could do it. If you could it, then you could describe it. You could write out a list of steps that someone else could follow to do it too. That list of steps is the solution to your problem. You need to come up with that first. Once you have that solution, only then should you think about how to implement it in code. We can help with that last part but you should already have a solution to implement. If you don't then you haven't really tried.

I'm not having a go at you specifically because lots of people are the same. They expect to be able to pluck code out of the air without knowing, at a fine-grained level, what that code is actually supposed to do and when they can't, they expect others to tell them what to do. You need to think about what the code needs to do first and then write the code to do it. If you can't tell me, step by step, what your code needs to do then you haven't given it enough thought yet. As I said, you can come up with those steps without considering it a programming problem so lack of programming experience is no issue.
 
Back
Top Bottom