Click here to Skip to main content
15,922,015 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Textbox Pin
Eddy Vluggen9-Dec-16 8:01
professionalEddy Vluggen9-Dec-16 8:01 
GeneralRe: C# Textbox Pin
Michael_Davies9-Dec-16 8:23
Michael_Davies9-Dec-16 8:23 
AnswerRe: C# Textbox Pin
Gerry Schmitz9-Dec-16 8:26
mveGerry Schmitz9-Dec-16 8:26 
AnswerRe: C# Textbox Pin
OriginalGriff9-Dec-16 8:27
mveOriginalGriff9-Dec-16 8:27 
GeneralRe: C# Textbox Pin
Pavlex49-Dec-16 22:06
Pavlex49-Dec-16 22:06 
GeneralRe: C# Textbox Pin
OriginalGriff9-Dec-16 22:21
mveOriginalGriff9-Dec-16 22:21 
GeneralRe: C# Textbox Pin
Pavlex49-Dec-16 23:07
Pavlex49-Dec-16 23:07 
GeneralRe: C# Textbox Pin
OriginalGriff9-Dec-16 23:38
mveOriginalGriff9-Dec-16 23:38 
So the letters in the labels are already characters, and you have them originally as a string, yes?
So don't faf about with converting them at all. Instead, use the string version. Then, when they type a character you can tell if any are permitted immediately by just using string.Contains:
C#
private string theWord = "extemporized".ToUpper();
...
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    char keyPressedByUser = char.ToUpper(e.KeyChar);
    if (!theWord.Contains(keyPressedByUser))
        {
        e.Handled = true;
        return;
        }
    ...
    }

Multiples are harder. They take a bit more work to ensure that the user can only enter three "E"'s to the example I gave.
To do that, add another private variable:
C#
private Dictionary<char, int> letters;

And when you read the word from your database, set it:
C#
letters = theWord.GroupBy(c => c).ToDictionary(g => g.Key, g => g.Count());

Now you can use the Dictionary to decide if he has typed enough letters yet:
C#
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (char.IsLetter(e.KeyChar))
        {
        char keyPressedByUser = char.ToUpper(e.KeyChar);
        if (!theWord.Contains(keyPressedByUser))
            {
            e.Handled = true;
            return;
            }
        int inputCount = myTextBox.Text.ToUpper().Where(c => c == keyPressedByUser).Count() + 1;
        if (inputCount > letters[keyPressedByUser])
            {
            e.Handled = true;
            return;
            }
        e.Handled = false;
        }
    }
Note that you need the IsLetter check or backspace will stop working! Laugh | :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

GeneralRe: C# Textbox Pin
Pavlex410-Dec-16 0:09
Pavlex410-Dec-16 0:09 
GeneralRe: C# Textbox Pin
OriginalGriff10-Dec-16 0:24
mveOriginalGriff10-Dec-16 0:24 
GeneralRe: C# Textbox Pin
Pavlex410-Dec-16 0:26
Pavlex410-Dec-16 0:26 
GeneralRe: C# Textbox Pin
OriginalGriff10-Dec-16 0:41
mveOriginalGriff10-Dec-16 0:41 
GeneralRe: C# Textbox Pin
Pavlex410-Dec-16 0:46
Pavlex410-Dec-16 0:46 
GeneralRe: C# Textbox Pin
OriginalGriff10-Dec-16 0:59
mveOriginalGriff10-Dec-16 0:59 
GeneralRe: C# Textbox Pin
Pavlex410-Dec-16 1:08
Pavlex410-Dec-16 1:08 
GeneralRe: C# Textbox Pin
OriginalGriff10-Dec-16 1:12
mveOriginalGriff10-Dec-16 1:12 
GeneralRe: C# Textbox Pin
Pavlex410-Dec-16 1:22
Pavlex410-Dec-16 1:22 
GeneralRe: C# Textbox Pin
OriginalGriff10-Dec-16 1:44
mveOriginalGriff10-Dec-16 1:44 
QuestionNew to c# Pin
Member 19561089-Dec-16 4:14
Member 19561089-Dec-16 4:14 
AnswerRe: New to c# Pin
ZurdoDev9-Dec-16 4:54
professionalZurdoDev9-Dec-16 4:54 
AnswerRe: New to c# Pin
ZurdoDev9-Dec-16 4:55
professionalZurdoDev9-Dec-16 4:55 
AnswerRe: New to c# Pin
Eddy Vluggen9-Dec-16 7:28
professionalEddy Vluggen9-Dec-16 7:28 
AnswerRe: New to c# Pin
Gerry Schmitz9-Dec-16 8:06
mveGerry Schmitz9-Dec-16 8:06 
GeneralRe: New to c# Pin
Member 19561089-Dec-16 8:25
Member 19561089-Dec-16 8:25 
GeneralRe: New to c# Pin
Gerry Schmitz9-Dec-16 8:49
mveGerry Schmitz9-Dec-16 8:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.