locked
Text insert in textbox RRS feed

  • Question

  • Hi.

    My name is humam and i am currently facing a problem in my application. Actually the application i am creating is a calculator application in c# WPF (MVVM) which operates is like a casio scientific calculator in which you type the whole expression and get the result below on pressing ANS. The method i am using to add text in upper textbox is:

    private void CMD_MTD(string CMD)

    {

    INPUT_VALUE += CMD;

    }

    Now the thing is when i press button with number lets suppose i press button with no 9 it adds 9 in text box and so on.

    thats all well and good. But what i actually want to achieve is that when i rearrange the cursor in textbox let say 23+6-5 is in textbox and i move the cursor to 23 and then enter another number lat say i enter 6 then it becomes 236 but when i add another number let say 9 it goes at the end like 236+6-59. The another thing is i am unable to rearrange the position of cursor through code.

    I used SendKey method for my first problem but it didnt work either.

    Actually i want to add text in textbox the way we type using keyboard through code.

    Please help me i am been stuck at this point for 3 days.

    If i am missing something in my question or if there is something that i didnt clear in my question so please do tell me.

    Thank You.


    Friday, September 9, 2016 8:13 PM

Answers

  • Hi Humam,

    >>"but when ever i click on the button the textbox became inactive"

    It is because when you click the button, your textbox will lost focus. You need to set the textbox focused by Focus method before setting its SelectionStart and CaretIndex property.

    textbox.Focus();
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by DotNet Wang Monday, October 10, 2016 5:34 AM
    Monday, October 3, 2016 6:41 AM

All replies

  • I think that it is much easier to control the text box directly.

    In order to insert a text to textBox1 at current position and move the caret, try this:

        textBox1.SelectedText = CMD;
        textBox1.SelectionLength = 0;
        textBox1.SelectionStart += CMD.Length;

    In order to append a text and move the caret to the end:

        textBox1.AppendText( CMD );
        textBox1.CaretIndex = int.MaxValue;

    But if INPUT_VALUE is a string and you want to insert CMD to a position that corresponds to the caret, then try this:

        INPUT_VALUE  = INPUT_VALUE.Insert( k, CMD );

    where k is the position, which can be determined using textBox1.SelectionStart or textBox1.CaretIndex.


    • Edited by Viorel_MVP Saturday, September 10, 2016 7:38 PM
    Saturday, September 10, 2016 7:38 PM
  • I tried doing this before too but when ever i click on the button the textbox became inactive and couldn't return the caret index.

    and using selectedtext wouldn't it change the text that is selected in textbox?

    Sunday, September 11, 2016 12:09 PM
  • Hi Humam505,

    Since your problem is more related to WPF. I will move it to WPF forum for support.

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 12, 2016 6:19 AM
  • Hi Humam,

    >>"but when ever i click on the button the textbox became inactive"

    It is because when you click the button, your textbox will lost focus. You need to set the textbox focused by Focus method before setting its SelectionStart and CaretIndex property.

    textbox.Focus();
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by DotNet Wang Monday, October 10, 2016 5:34 AM
    Monday, October 3, 2016 6:41 AM