Click here to Skip to main content
15,909,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made c# windowsform program where if the user enters data in a textbox in any other language other than English a label appears saying”this field has to be in english” that works perfectly, but I have tried to get the .focus()and .select() methods to work in the TextChangedEvent event to work but they dont
Is there any other solution?
Here is my code:

What I have tried:

Char eng=new char {‘a’,’b’,’c’,.......,’z’};
Foreach(char c in textbox.text.ToLower())
If (!eng.Contains(c))
{
Label22.Visible=true;
Textbox.focus();
}
Else{
Label22.Visible=false;
Posted
Updated 8-Jun-19 10:45am
v3
Comments
[no name] 7-Jun-19 12:22pm    
It's usually more helpful if you explain "what" you want to accomplish / happen; versus posting code that makes little sense but "doesn't work" .... because otherwise, this turns into a long "question and answer period".
AIDEN IV 7-Jun-19 13:25pm    
Well what im trying to do is if the user enters a in a language other than english, a label appears that says “this field ha to be in english” but that works perfectly, what doesnt work is the .focus() method. It works in the validating event, but i want to use the TextChangedEvent event
[no name] 8-Jun-19 18:15pm    
Still doesn't explain why you would try to hijack the keyboard while a user is typing. There are other ways to get a user's attention without meddling with their mental model. If you want them to "stop typing", then disable the field or throw away key strokes; beep; red border; etc.

WinForm app .NET 4.0 on a Win10 machine with Thai language installed. Three TextBoxes for Thai entry, three for English.

In this example, using English in a Thai TextBox (and, the reverse) is not possible ... unless the user switches the global language setting during text entry.

I strongly encourage you not to block the user from leaving a TextBox if they have not entered anything, as is demonstrated in this code: that goes against Win UI guidelines, and will probably confuse some users.
using System;
using System.Windows.Forms;
using System.Globalization;

namespace MixedLanguageTbx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        InputLanguage Thai;
        InputLanguage English;

        private void Form1_Load(object sender, EventArgs e)
        {
            CultureInfo thaiLanguage = new CultureInfo("th");
            Thai = InputLanguage.FromCulture(thaiLanguage);

            CultureInfo englishLanguage = new CultureInfo("en");
            English = InputLanguage.FromCulture(englishLanguage);
        }

        private TextBox currentTextBox;

        private char currentChar;
        private char backKey = (char)Keys.Back;

        // all TextBoxes use this
        private void tbxDataEntry_KeyPress(object sender, KeyPressEventArgs e)
        {
            currentChar = e.KeyChar;

            bool validChar =
                //char.IsLower(currentChar)
                //||
                char.IsLetter(currentChar)
                ||
                char.IsDigit(currentChar)
                ||
                currentChar == backKey;

            if (!validChar)
            {
                if (!lblWarning.Visible)
                {
                    lblWarning.Visible = true;
                }
                e.Handled = true;
            }
            else
            {
                if (lblWarning.Visible)
                {
                    lblWarning.Visible = false;
                }
            }
        }

        // all Thai TextBoxes use this
        private void ThaiTbxs_Enter(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = Thai;
        }

        // all English TextBoxes use this
        private void EnglishTbxs_Enter(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = English;
        }


        // what you should not do !
        // all TextBoxes use this
        private void AllTextBoxes_Leave(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = English;

            currentTextBox = sender as TextBox;

            if(currentTextBox.Text == "")
            {
                currentTextBox.Focus();
            }
        }
    }
}
 
Share this answer
 
Comments
AIDEN IV 8-Jun-19 17:36pm    
This helped, thank you for your replies
I suggest you filter the user's key entry as it occurs; that wy you caan avoid the issue of maintaining focus. The example here demonstrates the fine-grained control you can have:
private char currentChar;

private char backKey = (char) Keys.Back;

private void tbxDataEntry_KeyPress(object sender, KeyPressEventArgs e)
{
    currentChar = e.KeyChar;

    bool validChar =
        char.IsLower(currentChar)
        ||
        char.IsLetter(currentChar)
        ||
        char.IsDigit(currentChar)
        ||
        currentChar == backKey;
    
    if(! validChar)
    {
        if (! lblWarning.Visible)
        {
            lblWarning.Visible = true;
        }
        e.Handled = true;
    }
    else
    {
        if (lblWarning.Visible)
        {
            lblWarning.Visible = false;
        }
    }
}
A potential problem is: how do you know the user is typing with the keyboard mapped to Roman characters ? What about digits ?

Question: what do you want to do if the user changes focus to another control, bur has not entered any text ?
 
Share this answer
 
v2
Comments
AIDEN IV 8-Jun-19 5:15am    
Well i have implemented a code in Validating event where if the field is empty,it doesnt let you edit another field, in other word the .focus() method works in the Validating event. But now i want to use the .focus() method in TextChangedEvent, where if the users enters a number or enters in any other language other than english, it shouldn’t let me move on to the next field. But the .focus() and .select () methods doesnt work in the TextChangedEvent
BillWoodruff 8-Jun-19 6:46am    
Why do you think you need to control focus in the TextChanged Event: focus does not change except, possibly, by the entry of a tab character ?

My advice is not to use the TextChanged Event. You can see how to test for a number (digit) in the code above.
AIDEN IV 8-Jun-19 6:52am    
Well if it doesnt work in the TextChangedEvent then i have to use it in the Validating event. Thanks for your help
BillWoodruff 8-Jun-19 7:18am    
Hi, I'm pretty sure I can assist you, but I need a clear picture of what is happening now, and what you want to happen. What is happening that makes you need to deal with focus in a TextChanged event ? cheers Bill
AIDEN IV 8-Jun-19 12:24pm    
I have created a form that comprises 20 textboxes, there are 3 textboxes that i want to accept only arabic language, i have no problem with that i have a code for that that works perfectly, what i want to do now is if the user enters in english in a textbox that should be filled in using arabic,a label appears saying”this textbox has to be in english”, what i want to do now is if the user enters in english in an arabic based textbox, it should focus on that textbox and shouldnt let me move to the next textbox, i want to use the TextChangedEvent because if i write a word that is not arabic,the label appears without clicking on the next textbox, i dont want to use the Validating event since i have to click on the next textbox in order for the label to appear

My only problem here is the .focus() and .select() method do not work in the TextChangedEvent. If there is any solution to that it would be great. Thank you for your time and assistance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900