Make ctrl-a work for readonly textbox - c#

I have an issue with my textbox, i want to select everything from a textbox that is readonly. The text that appears in the textbox is a ouput of another function that checks if our databases has no corruptions. The ouput will appear in the textbox.
So what im trying to do is to select everything from a readonly textbox. So we can save to ouput of the database check.
i've tried this so far:
private void ContentTextBox_TextChanged(object sender, KeyEventArgs e)
{
if (e.Control)
{
MessageBox.Show("Control works");
}
}
But now i have to make the "a" key work and i have to make the combination select the textbox.text
Can someone help me with this?
Thanks in advance

You can add a KeyDown() method to your TextBox, which recognises a user hitting Ctrl + A and then selects all of the text, like:
private void ContentTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control && e.KeyCode == Keys.A)
{
ContentTextBox.SelectAll();
}
}

You have to code the KeyDown method of your textbox. Something like:
private void ContentTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control && e.KeyCode == Keys.A)
{
MessageBox.Show("Ctrl + a detected");
}
}

You can use this code:
if (e.Control && e.KeyCode == Keys.A)
{
textBox.Focus();
textBox.SelectionStart = 1; //start
textBox.SelectionLength = 2; //length
textBox.ScrollToCaret();
}

Related

Enter key in C#

I have a code like below
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
if (!textBox1.AcceptsReturn)
{
button1.PerformClick();
}
}
}
After I hit Enter, it'll send a message to another textbox and begin a new line. Can anyone help me to bring the cursor back to its first line?
I tried textBox1.SelectionStart, SelectionLength and Focus but it doesn't work, is there any another way?
You can prevent that the keypress is passed on to the control by setting the KeyPressEventArgs.Handled property to true:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
if (!textBox1.AcceptsReturn)
{
button1.PerformClick();
e.Handled = true;
}
}
}
As you mentioned in a comment that you are implementing a chat app, you also might want to implement the typical behavior of Shift+Return inserting a new line:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13 && !e.Shift)
{
if (!textBox1.AcceptsReturn && !string.IsNullOrEmpty(textBox1.Text))
{
button1.PerformClick();
textBox1.Text = "";
e.Handled = true;
}
}
}
To set cursor position to the beginning of a textbox, use the following...
I will hazard a guess that you didn't use these in combination with each other...
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;

How to overwrite ctrl + a select all from list box?

I was hoping that this code below would overwrite it, since I am assigning new stuff. But instead it executes both, selecting all and my message box
private void EventSetter_OnHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
System.Windows.MessageBox.Show("ctrl a");
}
}
please help thanks
If you handle the PreviewKeyDown event for the ListBox, you should be able to mark the event as handled, and the Ctrl+A should be ignored:
private void OnListBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
e.Handled = true;
}
}

Save a text string to textbox with keyboard shortcut

Im a beginner in C# and im trying to create a windows form application that save a copied text to a textbox when you execute a command with the keyboard. I know there is mouch more to do but where do i start? I suceed to make someting happening with the code below a start at least..
And another question.. is it possible to create more than 2 commands. It doesn't work if i add for example : " && KeyCode.ToString() == "B") "
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "C")
{
MessageBox.Show("CTRL+C");
}
}
Cheers
You would manage this by calling keyDown/Up events. Keep track of each event and which key went down. Then using the Clipboard.GetText() function to copy/paste the text from clipboard into your textbox once both keys are down.
Example,
bool keyup = false;
bool keyleft = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = true;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = true;
}
if (keyleft && keyup)
{
textboxOne.Text = Clipboard.GetText(TextDataFormat.Html);
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = false;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = false;
}
}
Used both these as my resources.
Resource One: Detect when two keys are pressed at the same time
Resource Two: http://msdn.microsoft.com/en-us/library/c2thcsx4%28v=vs.110%29.aspx

Trying to detect keypress

I made a method that detects when a key is pressed, but its not working! Heres my code
void KeyDetect(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W && firstload == true)
{
MessageBox.Show("Good, now move to that box over to your left");
firstload = false;
}
}
I also tried to make a keyeventhandler but, it sais "cannot assign to key detect because it is a method group"
public Gwindow()
{
this.KeyDetect += new KeyEventHandler(KeyDetect);
InitializeComponent();
}
Use keypress event like this:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.F1 && e.Alt)
{
//do something
}
}
1) Go to your form's Properties
2) Look for the "Misc" section and make sure "KeyPreview" is set to "True"
3) Go to your form's Events
4) Look for the "Key" section and double click "KeyDown" to generate a function to handle key down events
Here is some example code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("You pressed " + e.KeyCode);
if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
{
//Do Something if the 0 key is pressed (includes Num Pad 0)
}
}
You are looking for this.KeyPress. See How to Handle Keypress Events on MSDN.
Try to use the KeyDown event.
Just see KeyDown in MSDN
Just do
if (Input.GetKeyDown("/* KEYCODE HERE */"))
{
/* CODE HERE */
}

How can you display textbox1 value in textbox2 using keystokes

I have 2 textbox (textbox 1 & textbox 2 ); the textbox1 has numeric value such as $1,000. Now when i am in textbox2 and if i hit keystrokes like (= or + or pageup or pagedown etc...) the textbox1 value should appear in textbox2.
The reason behind this is to enable customers improve speed in processsing/ filling data.
I don't know if I understand what you need, anyway try this:
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.PageDown ||
e.KeyCode == Keys.PageUp ||
e.KeyCode == Keys.Oemplus ||
e.KeyCode == Keys.Add ||
(e.KeyCode == Keys.D0 && e.Shift))
{
textBox2.Text = textBox1.Text;
e.Handled = true;
e.SuppressKeyPress = true;
}
}
How about adding a keyPress event on textbox2 with a function like this:
private void textbox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '+')
{
textbox2.Text = textbox1.Text;
}
}
You can try this:
protected void textbox2_TextChanged(object sender, EventArgs e)
{
if(textbox2.Text=="+"||textbox2.Text=="=")
textbox2.Text=textbox1.Text;
}

Categories