Activate a textbox automatically when user starts typing - c#

I want to activate a textbox when users starts typing in my Windows 8.1 Store app.
I tried handling KeyDown event of Page, something like this code:
private void pageRoot_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (SearchBox.FocusState == Windows.UI.Xaml.FocusState.Unfocused)
{
string pressedKey = e.Key.ToString();
SearchBox.Text = pressedKey;
SearchBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
}
}
But the problem is e.Key.ToString() always returns capital english character of the pressed key, while user might be typing in another language. For example, the Key D types ی in Persian keyboard, and user might want to type in Persian, but e.Key.ToString() will still return D instead of ی.
Also I tried making that textbox always focused (my page contains some gridviews and so on, and a textbox) and while this solution works on PCs, it makes the on-screen keyboard to always appear on tablets.
So, what should I do? Is there any way to get the exact typed character in KeyDown event?

As Mark Hall suggested, It seemed that CoreWindow.CharacterReceived event can help solving this issue.
So, I found the final answer here.
This is the code from that link:
public Foo()
{
this.InitializeComponent();
Window.Current.CoreWindow.CharacterReceived += KeyPress;
}
void KeyPress(CoreWindow sender, CharacterReceivedEventArgs args)
{
args.Handled = true;
Debug.WriteLine("KeyPress " + Convert.ToChar(args.KeyCode));
return;
}
But this event will fire anywhere independent of current active page. So I must remove that event when user navigates to another page, and add it again when user comes back.
Update: I also had to move the cursor of the textbox to the end of the text, so user can write naturally. Here's my final code:
private void KeyPress(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.CharacterReceivedEventArgs args)
{
if (SearchBox.FocusState == Windows.UI.Xaml.FocusState.Unfocused)
{
SearchBox.Text = Convert.ToChar(args.KeyCode).ToString();
SearchBox.SelectionStart = SearchBox.Text.Length;
SearchBox.SelectionLength = 0;
SearchBox.Focus(FocusState.Programmatic);
}
}
private void pageRoot_GotFocus(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.CharacterReceived += KeyPress;
}
private void pageRoot_LostFocus(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.CharacterReceived -= KeyPress;
}

Related

How do you keyboard focus on a specific item?

I am making a game where I need a constant keyboard listener (to navigate through the game). I tried getting the keyboard focus to one place and let it stay there using a seperate thread in a while true loop. This seems to crash my program.
Question:
Is there a method to get my keyboard focused on one element so I can grab my key input from there?
What can I use?:
something that works without throwing exceptions
something I can use in combination with other text input
something that doesn't take hours to compile
something that is easy to build another program (im not super good at c#)
What have I tried?
public MainWindow()
{
InitializeComponent();
Thread keyboardfocus = new Thread(GetFocus);
keyboardfocus.Start();
}
private void GetFocus()
{
while (true)
{
Keyboard.Focus(KeyboardButton);
}
}
private void KeyboardButton_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Z)
{
map.PosUp -= 1;
MainCanvas.Background = Brushes.Aqua;
}
else if (e.Key == Key.S)
{
map.PosUp += 1;
MainCanvas.Background = Brushes.Black;
}
}
Thanks
Add event handler for Window.Loaded and set there a focus to the desired control:
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Keyboard.Focus(KeyboardButton);
}
Add event handler for the UIElement.LostKeyboardFocus in your case KeyboardButton and just set the keybord focus again to the KeyboardButton:
private void KeyboardButton_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
Keyboard.Focus(KeyboardButton);
}

Program for learning foreign words C#

I am in the process of writing a vocabulary program. C # Windows Form.
Description of the program operation:
Use the buttons to select the location of text files with the words "PL" and "ENG". (two separate files)
Click the start button to start the program
the first word from the board appears in the label
I'm translating the word into the textbox and the Messagebox "OK" or "WRONG" pops up
And here a problem arises. The program instead of every time I wait until I introduce a new word to the textbox, it loops, the questions in the label are changed and MessageBox displays.
How best to do this to make the program work correctly? `` `[
private void sprawdzButton_Click(object sender, EventArgs e)
{
BazaSlow.bazaPolskichSlowek = _fileReader.Read(adresPlikuPL);
BazaSlow.bazaAngielskichSlowek = _fileReader.Read(adresPlikuANG);
string odpowiedz = odpTextBox.Text;
int i = 0;
while (i < BazaSlow.bazaPolskichSlowek.Length)
{
trescSlowkaLabel.Text = BazaSlow.bazaPolskichSlowek[i];
if (odpowiedz.Equals(BazaSlow.bazaAngielskichSlowek[i].ToLower()))
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("ŹLE");
}
i++;
}
}
This approach will not quite work.
If you use WinForms then you can do it via events. I'll quickly use english variable names since I don't speak your language.
This could be one approach to do it: I used the "TextChanged" event from the textBox.
string[] wordsLanguage1;
string[] wordsLanguage2;
int currentIndex = 0;
private void Form1_Load(object sender, EventArgs e)
{
wordsLanguage1 = System.IO.File.ReadAllLines("somePath1");
wordsLanguage2 = System.IO.File.ReadAllLines("somePath2");
}
private void ReportAndCheckInput(string input)
{
if (input.ToLower().Equals(wordsLanguage2[currentIndex].ToLower())) {
//right translation
currentIndex++;
label1.Text = wordsLanguage1[currentIndex];
textBox1.Text = "";
}
else
{
//wrong translation
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ReportAndCheckInput(textBox1.Text);
}
Now this approach uses the TextChanged event. So the ReportAndCheckInput method will be called on every text-change. That means that your Feedback would pop up on every keystroke which would not be nice. You could use any other event instead of TextChanged. For example a button click. Another solution would be to use a label for your feedback and not a message box. Then the user would never have to click anything but woudl instantly see whether or not he was correct.

Setting Textbox values which has focus in C# Winforms

I have numeric buttons which when pressed display the number in different text boxes. Now my problem is that i want check which textbox has focus so that the number pressed will be entered in that textbox.
My Code:
private void btn_one_Click(object sender, EventArgs e)
{
if (txt_one.Focused==true)
{
txt_one.Text += btn_one.Text;
}
else if (txt_two.Focused==true)
{
txt_two.Text += btn_one.Text;
}
}
Now my problem is that the above code is not working what is wrong and what will be the solution? I even used something like this
private void btn_one_Click(object sender, EventArgs e)
{
if (txt_one.Focus()==true)
{
txt_one.Text += btn_one.Text;
}
else if (txt_two.Focus()=true)
{
txt_two.Text += btn_one.Text;
}
}
In both the above cases the text is entered in both the text boxes. Any solutions.
This problem is a little tricky (with my experience dealing with Enter, Focus, LostFocus, Leave events, all these things sometimes make your head ache a lot and you should avoid dealing with them if possible), at the time you click your Button, the current Focused control you can know is exactly the Button (ActiveControl is one short way to access it). So the solution is we have to record the track of focused TextBox, hold it in a reference and use it when needed. In fact if the control other than one of your TextBoxes is focused, we have to reset the variable lastFocused to null:
TextBox lastFocused;
//Enter event handler for all your TextBoxes
private void TextBoxes_Enter(object sender, EventArgs e){
lastFocused = sender as TextBox;
}
//Click event handler for your button
private void btn_one_Click(object sender, EventArgs e){
if(lastFocused != null) lastFocused.Text += btn_one.Text;
}

How can I know which object is clicked in C#?

To make sure that the user name input is valid, I added such callback method to do the verification:
Regex UserNameRE = new Regex(#"^[a-zA-Z]\w*$");
//being called when input box is not focused any more
private void UserNameInput_Leave(object sender, EventArgs e)
{
//pop up a warning when user name input is invalid
if (!UserNameRE.IsMatch(UserNameInput.Text))
{
MessageBox.Show("Invalid User Name!");
this.UserNameInput.Text = "";
this.UserNameInput.Focus();
}
}
The method will be called when user finished their inputting(the method is bounded with the event-"leaving the input box"). It works when user left a invalid User_Name and begin to enter a password.
But it also works when user click another tab, e.g. the Register tab. I don't want this happen. Because the user obviously don't wanna login anymore if he clicks "Register" tab, and my C# app shouldnot pop up a warning box and force them inputting a valid user name again.
How can the C# tell the difference of such 2 situations? It should be easy if I know which object is being clicked.
You will have source of event in object sender in UserNameInput_Leave event.
private void UserNameInput_Leave(object sender, EventArgs e)
{
//sender is source of event here
}
Here's an option:
private void UserNameInput_Leave(object sender, EventArgs e)
{
if (sender.GetType() != typeof(TextBox))
{
return;
}
TextBox tBox = (TextBox)sender;
//pop up a warning when user name input is invalid
if (!UserNameRE.IsMatch(UserNameInput.Text) && tBox.Name == UserNameInput.Name)
{
MessageBox.Show("Invalid User Name!");
this.UserNameInput.Text = "";
this.UserNameInput.Focus();
}
}
I am not sure if there's a right solution for this particular scenario here.
When you add a handler to validate your control on mouse leave, definitely it will be executed first regardless you clicked on another control within the tab or another tab itself.
This normal flow can't be ignored easily. It must be possible by hanlding the message loop yourself but the event based flow, first leave focus, and selected index change (selecting) event will be fired. I would suggest you not to disturb the flow as the validation is client side and pretty fast. Instead of messagebox, I would recommend you to use ErrorProvider and attach to the control whenever required. Also messagebox is quite disturbing and as per your code, you're forcefully making it focus to the textbox again.
How about the following code?
public partial class Form1 : Form
{
ErrorProvider errorProvider = new ErrorProvider();
public Form1()
{
InitializeComponent();
textBox1.Validating += new CancelEventHandler(textBox1_Validating);
}
private void textBox1_Leave(object sender, EventArgs e)
{
textBox1.CausesValidation = true;
}
void textBox1_Validating(object sender, CancelEventArgs e)
{
Regex UserNameRE = new Regex(#"^[a-zA-Z]\w*$");
if (!UserNameRE.IsMatch(textBox1.Text))
{
errorProvider.SetError(this.textBox1, "Invalid username");
}
}
}

Adding a button or link breaks form

I am using a windows form and within the form i have a user control with two labels, one that has a message ENTER AMOUNT and the other where I am putting the values typed by the user (like when you go to an ATM) it starts showing the number .. it works fine if i dont have any other controls on the user control.. but the moment i add a button it does not work, it wont start showing the numbers as I use my numeric key pad.. but if i remove whatever button i added it works again... Here is my user control code.
public partial class OperationAmount : UserControl
{
public OperationAmount()
{
InitializeComponent();
}
private int _inputNumber = 0;
private void OperationAmount_Load(object sender, EventArgs e)
{
}
private void Form_KeyAmountPressed(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar))
{
return;
}
else if (lblOperationAmount.Text.Length > 9)
{
return;
}
else
{
_inputNumber = 10 * _inputNumber + Int32.Parse(e.KeyChar.ToString());
ReformatOutput();
}
}
private void ReformatOutput()
{
lblOperationAmount.Text = String.Format("{0:0.00}", (double)_inputNumber / 100.0);
}
}
Probably the new control steals the keypresses from your Form_KeyAmountPressed method because now it has the focus and receive the event KeyPress.
A simple workaround would be to add the method Form_KeyAmountPressed also at the KeyPress event of the button. Try also to set the TabStop property of the button to false. (not sure if this has any effect when the button is the only control that can get focus on your user control).

Categories