C # Make a group of characters in a text box behave like one character

Basically, I have keywords like sin( and cos( in the text box that I want to keep as one character.

When I mention the entire line below, it refers to a group of characters (for example, " sin( ")

Using sin( as an example:

If the carriage was in this position (beyond s ):

enter image description here

If you press del , it will delete the whole line. If the right arrow key is pressed, the carriage will jump after ( .

If the carriage was in this position (after ( ):

enter image description here

If you click backspace , the entire line will be deleted. If the left arrow key was pressed, the carriage would move beyond s .

EDIT: Thanks to John Skeet who pointed this out.

Selecting any substring of a character group (for example, si from sin( ) should select the entire string.

Sorry if this is hard to understand, then what I mean is a little hard to put into words.

EDIT 2: Thanks to Darksheao for giving me an answer to the backspace and deleting the keys. I moved the delete segment to the PreviewKeyDown event since it does not work with the KeyPress event.

EDIT 3: Using the charCombinations way of doing things, here is how I made the implementation for the left and right keys:

 #region Right case Keys.Right: { s = txt.Substring(caretLocation); foreach (string combo in charCombinations) { if (s.StartsWith(combo)) { textBox1.SelectionStart = caretLocation + combo.Length - 1; break; } } break; } #endregion #region Left case Keys.Left: { s = txt.Substring(0, caretLocation); foreach (string combo in charCombinations) { if (s.EndsWith(combo)) { textBox1.SelectionStart = caretLocation - combo.Length + 1; break; } } break; } #endregion 

All that remains is the implementation of the mouse. Anyone? I also realized that the user can use the mouse to place the carriage in the middle of one of these lines, so when this happens, the mouse needs to be moved to the beginning of the line.

+8
string c # textbox
source share
1 answer

Here is a section of code that sets up an array of character combinations that you want to treat as "single characters" and sets up a handler that looks for them.

 private void textBox1_KeyDown(object sender, KeyEventArgs e) { string[] charCombinations = new string[2]; charCombinations[0] = "sin("; charCombinations[1] = "cos("; string txt = this.textBox1.Text; int caretLocation = this.textBox1.SelectionStart; if (e.KeyCode == Keys.Delete) { //get text in front string s = txt.Substring(caretLocation); string notChecking = txt.Substring(0, caretLocation); foreach (string combo in charCombinations) { if (s.StartsWith(combo)) { txt = notChecking + s.Substring(combo.Length - 1); break; } } } if (e.KeyCode == Keys.Back) { //get text in behind string s = txt.Substring(0, caretLocation); string notChecking = txt.Substring(caretLocation); foreach (string combo in charCombinations) { if (s.EndsWith(combo)) { txt = s.Substring(0, s.Length - combo.Length + 1) + notChecking; caretLocation -= combo.Length - 1; break; } } } this.textBox1.Text = txt; //changing the txt feild will reset caret location this.textBox1.SelectionStart = caretLocation; } 

Making some changes to this, so that you are looking around SelectionStart, will handle what John mentioned in the comment.

Links: TextBox Event Reference and How to find the cursor position in a text box? FROM#

+2
source share

All Articles