C# Substring Issue

Status
Not open for further replies.

Mikee

Active Member
Jul 8, 2017
162
102
Code:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == e.Key)
            { 
                //Check if its a letter
                if ((e.Key >= Key.A) && (e.Key <= Key.Z)){
                    string KeyPressed = e.Key.ToString(); //convert the keypress to a string (I wanted char but :sadface )

                    bool ShiftPressed = false;
                    string LetterConverted;

                    if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift) || Keyboard.IsKeyToggled(Key.CapsLock))
                        ShiftPressed = true;
                  
                    LetterConverted = UserInput.ReturnOutput(KeyPressed, ShiftPressed);
                    MyTextBox.Text += LetterConverted;

                    //MyTextBox.SelectionStart = MyTextBox.Text.Length + 1;
                  //MyTextBox.Text = MyTextBox.Text.Substring(0, MyTextBox.SelectionStart - 1);
                }

So what happens. I Press the letter "r". That then gets converted via the UserInput.ReturnOutput function into the russian equivalent of r which is "p". Then the program on the textbox prints "rp" Because it'll print what i type "r" + the russian translation which is "p".

//MyTextBox.SelectionStart = MyTextBox.Text.Length + 1; -> this line of code swaps the two. It then turns into "pr".

Now my goal is to cut out the english version -> i.e return only the russian equaivelnt "p".
But for some reason //MyTextBox.Text = MyTextBox.Text.Substring(0, MyTextBox.SelectionStart - 1);
is not working. That line of code completely ignores the russian translation and only returns on the input screen the english letter.

All I need is after MyTextBook.SelectionStart = MyTextbox.Text.Length+1 is to delete the input key (the english letter) and to return only the russian translation, which in theory should be a simple substring function.
 


Issue Solved:

Ok So. To recap the problem was this.

When I typed the letter (for instance) L in the textbox I wanted only the russian translation of that letter to be displayed "л"
But I had an issue where substring(0,MyTextBox.Text.Length - 1) Wouldn't return everything and remove the english letter L, which I didn't need as I only wanted to translation to be shown.

What fixed it was this. e.Handled = true;

This tells the program that the inputkey was handled and doesn't need to do anything. I.e it wont show up in the textbox.

Full code solution:
Code:
 private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == e.Key)
            { 
                //Check if its a letter
                if ((e.Key >= Key.A) && (e.Key <= Key.Z)){
                    e.Handled = true; //This handles the inputkey and doesn't display it in the textbox
                    string KeyPressed = e.Key.ToString(); //convert the keypress to a string (I wanted char but :sadface )

                    bool ShiftPressed = false;
                    string LetterConverted;

                    if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift) || Keyboard.IsKeyToggled(Key.CapsLock))
                        ShiftPressed = true;

                    LetterConverted = UserInput.ReturnOutput(KeyPressed, ShiftPressed);
                    MyTextBox.Text += LetterConverted;
                  
                    MyTextBox.CaretIndex = MyTextBox.Text.Length; //place cursor in the end of everything, preemptive check
                }
            }
        }
 
Last edited:
Status
Not open for further replies.

Users who are viewing this thread

Top