How can I change selected textBox in windowsForms app - c#

I want users to be able to travel between textboxes and to a button by clicking Keys.Enter, Keys.Up, Keys.Down. Is there a way like this.SelectedItem = TxtBoxName or something?
private void TxtBoxName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if(TextIsEmpty(TxtBoxDebt.Text))
{
//switch to TxtBoxDebt
}
else
{
AssignNewPerson();
}
}
if (e.KeyCode == Keys.Down)
{
//switch to TxtBoxDebt
}
}
private void TxtBoxDebt_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
AssignNewPerson();
}
else if (e.KeyCode == Keys.Up)
{
//switch to TxtBoxName
}
else if(e.KeyCode == Keys.Down)
{
//switch to TxtBoxNote
}
}
private void TxtBoxNote_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
AssignNewPerson();
}
else if (e.KeyCode == Keys.Up)
{
//switch to TxtBoxDebt
}
else if (e.KeyCode == Keys.Down)
{
//switch to BtnAssign
}
}

I think you are looking for the Focus() method, for example:
TxtBoxDebt.Focus();

Related

UserControl not raising a KeyPress event when a key is pressed in C#

I would like to create a textbox that allows user input only positive double numbers. To do so, I have created a class that inherits from System.Windows.Forms.Textbox and added a KeyPress event as follows:
public partial class PositiveDoubleOnlyTB : TextBox
{
private void InitializeComponent()
{
this.SuspendLayout();
//
// PositiveDoubleOnlyTB
//
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.PositiveDoubleOnlyTB_KeyPress);
this.ResumeLayout(false);
}
private void PositiveDoubleOnlyTB_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
SystemSounds.Beep.Play();
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
SystemSounds.Beep.Play();
}
}
}
The problem is that when I input data in this custom TextBox a KeyPress event is not being raised. Could somebody help me showing what is wrong?
public class PositiveDoubleOnlyTB : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!(char.IsDigit(e.KeyChar) || e.KeyChar == '.' && base.Text.IndexOf('.') == -1))
{
e.Handled = true;
}
base.OnKeyPress(e);
}
}

C# Windows Forms KeyDown event fires continiously [duplicate]

This question already has answers here:
how not to allow multiple keystokes received at one key press?
(3 answers)
Closed 6 years ago.
In a Windows forms application I'm trying to execute some function when I press a hotkey using Form's KeyDown event. The problem is that when I keep the hotkey pressed the event fires continuously.
I just want to execute some function at the first time I press the button and another function when I release it.
Here is the code I used to accomplish this operation:
bool isPressed_Num7 = false;
bool isPressed_Num9 = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.NumPad7 && !isPressed_Num7)
{
isPressed_Num7 = true;
Console.WriteLine("Keydown 7");
}
if (e.KeyData == Keys.NumPad9 && isPressed_Num9)
{
isPressed_Num9 = true;
Console.WriteLine("Keydown 9");
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.NumPad7)
{
isPressed_Num7 = false;
Console.WriteLine("Keyup 7");
}
if (e.KeyData == Keys.NumPad9)
{
isPressed_Num9 = false;
Console.WriteLine("Keyup 9");
}
}
I'm going to use a bunch of hotkeys. So what I'm asking is there a better way to accomplish what I'm trying to do?
You could just a dictionary instead of one variable per key.
Dictionary<Keys, bool> keysPressed = new Dictionary<Keys, bool>();
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
keysPressed[e.KeyData] = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (!keysPressed.ContainsKey(e.KeyData) || !keysPressed[e.KeyData]) {
Console.WriteLine(e.KeyData);
keysPressed[e.KeyData] = true;
}
}
Try this:
bool isPressed_Num7 = false;
bool isPressed_Num9 = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.NumPad7) && (!isPressed_Num7))
{
isPressed_Num7 = true;
Console.WriteLine("Keydown 7");
}
if ((e.KeyData == Keys.NumPad9) && (!isPressed_Num9))
{
isPressed_Num9 = true;
Console.WriteLine("Keydown 9");
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.NumPad7)&&(isPressed_Num7==true))
{
isPressed_Num7 = false;
Console.WriteLine("Keyup 7");
}
if ((e.KeyData == Keys.NumPad9)&&(isPressed_Num9==true))
{
isPressed_Num9 = false;
Console.WriteLine("Keyup 9");
}
}
This solution uses the recommended way to handle keyboard shortcuts according to Microsoft. And allows you to assign action to invoke when pressing a key combination.
public partial class Form1 : Form
{
private readonly IDictionary<Keys, Action> _operationsMap = new Dictionary<Keys, Action>();
public Form1()
{
InitializeComponent();
RegisterKeyShortcuts();
}
private void RegisterKeyShortcuts()
{
_operationsMap.Add(Keys.Control | Keys.F, WhenPressingF);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (_operationsMap.ContainsKey(keyData))
{
_operationsMap[keyData].Invoke();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected void WhenPressingF()
{
MessageBox.Show("What the Ctrl+F?");
}
}

How do I make it so I don't have to reclick an application for a keydown function to work?

Something has been really bothering me and its that the fact I have to click out and click back in the application for a key down function to work. Is there any way to fix it?
Code just in case if you need it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dog_simulator
{
public partial class Form1 : Form
{
public bool KeyPreview { get; set; }
bool right;
bool left;
bool up;
bool down;
public int left_var = 5;
public int right_var = 5;
public Form1()
{
this.KeyPreview = true;
InitializeComponent();
Pee.Left += 220;
}
private void label1_Click(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
public void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
public void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
left = true;
Console.WriteLine("Left has been pressed");
}
if(e.KeyCode == Keys.Right)
{
right = true;
Console.WriteLine("Right has been pressed");
}
if(e.KeyCode == Keys.Up)
{
up = true;
Console.WriteLine("Up has been pressed");
}
if(e.KeyCode == Keys.Down)
{
down = true;
Console.WriteLine("Down has been pressed");
}
}
public void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
left = false;
Console.WriteLine("Left has been let go of");
}
if (e.KeyCode == Keys.Right)
{
right = false;
Console.WriteLine("Right has been let go of");
}
if (e.KeyCode == Keys.Up)
{
up = false;
Console.WriteLine("Up has been let go of");
}
if (e.KeyCode == Keys.Down)
{
down = false;
Console.WriteLine("Down has been let go of");
}
}
public void timer1_Tick(object sender, EventArgs e)
{
if (right == true)
{
player.Left += left_var;
}
if (left == true)
{
player.Left -= left_var;
}
Invalidate();
if (up == true)
{
player.Top -= left_var;
}
if (down == true)
{
player.Top += left_var;
}
}
private void dog_Click(object sender, EventArgs e)
{
}
private void player_Click(object sender, EventArgs e)
{
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void PeeTime_Tick(object sender, EventArgs e)
{
if(panel1.Visible == false)
{
Pee.Left -= 5;
if (Pee.Bounds.IntersectsWith(pee2.Bounds))
{
Pee.Left += 220;
}
}
}
}
}

How do i set the label and display an integer value from a TextBox to the label when the enter key is pressed in wpf c#?

Here's the code. I have modified it bit?
public partial class MainWindow : Window
{
int coursenumber;
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
bool res = int.TryParse(textBox.Text, out coursenumber);
if (res == true)
{
//success
}
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
//logic goes here
}
}
Register to the Key Down event and check
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// your logic here
}
}
What you're looking for is the KeyDown event. Subscribe to this event on your TextBox like this:
<TextBox KeyDown="textBox_KeyDown" ... />
And your event handler will look something like this:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
bool res = int.TryParse ...
//The rest of your logic here.
}
}
Thanks everyone, i was able to get it working from your responses. here's the modification
public partial class MainWindow : Window
{
int coursenumber;
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
bool res = int.TryParse(textBox.Text, out coursenumber);
if (res == true)
{
//success
}
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
label2.Content = coursenumber;
}
}

Esc handler ignored

Following code, doesn't get triggered when Esc is pressed. Can somebody give me insight?
I like to change the cursor (let's say from drawing mode turn it into pointer mode)
public override void OnKeyDown(MyCanvas dc, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
_line = null;
e.Handled = true;
}
}
Thanks.
#amit's solution:
public override void OnKeyDown(MyCanvas dc, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
_line = null;
dc.CurrentTool = ToolType.Pointer;
dc.Cursor = Cursors.Arrow;
dc.ReleaseMouseCapture();
}
}

Categories