Replacing chars when typing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Trooper

    Replacing chars when typing

    Hi!

    I want when user type comma in textbox to
    automaticly replace in dot.

    In keypress event i have something like this:

    if (e.KeyChar == 44)
    {
    textBox1.Text = textBox1.Text.R eplace(",",".") ;
    }

    but it doesn't work in way that i want. Is there any
    way to do that?

    Thnx.
  • Peter Duniho

    #2
    Re: Replacing chars when typing

    On Tue, 18 Nov 2008 15:45:36 -0800, Trooper <trp@nema.email wrote:
    Hi!
    >
    I want when user type comma in textbox to
    automaticly replace in dot.
    >
    In keypress event i have something like this:
    >
    if (e.KeyChar == 44)
    {
    textBox1.Text = textBox1.Text.R eplace(",",".") ;
    }
    >
    but it doesn't work in way that i want. Is there any
    way to do that?
    Just change the KeyChar property in your event handler. When it's equal
    to ',', set it to '.' (and use the character literals, not ASCII codes).

    Pete

    Comment

    • DH

      #3
      Re: Replacing chars when typing

      Trooper wrote:
      Hi!
      >
      I want when user type comma in textbox to
      automaticly replace in dot.
      >
      In keypress event i have something like this:
      >
      if (e.KeyChar == 44)
      {
      textBox1.Text = textBox1.Text.R eplace(",",".") ;
      }
      >
      but it doesn't work in way that i want. Is there any
      way to do that?
      >
      Thnx.
      I would do it in the TextChanged event handler and just
      textBox1.Text=t extBox1.Text.Re place(',','.');

      that is assuming it is ok for it to replace all the ',' with '.' once
      the user has finished entering what ever they are entering.

      what exactly do you mean by it doesn't work the way you want it to?
      What do you expect and what does it do?

      Comment

      • Trooper

        #4
        Re: Replacing chars when typing

        Peter Duniho wrote:
        >
        Just change the KeyChar property in your event handler. When it's equal
        to ',', set it to '.' (and use the character literals, not ASCII codes).
        >
        Pete

        Did you mean to put e.KeyChar = Keys.Decimal; ??
        KeyChar property is read-only and i can't assign anything.

        Comment

        • Peter Duniho

          #5
          Re: Replacing chars when typing

          On Tue, 18 Nov 2008 16:02:48 -0800, Trooper <trp@nema.email wrote:
          Peter Duniho wrote:
          > Just change the KeyChar property in your event handler. When it's
          >equal to ',', set it to '.' (and use the character literals, not ASCII
          >codes).
          > Pete
          >
          >
          Did you mean to put e.KeyChar = Keys.Decimal; ??
          KeyChar property is read-only and i can't assign anything.
          If KeyChar is read-only, then you are not actually handling the KeyPress
          event as your original post claimed.


          Comment

          • Trooper

            #6
            Re: Replacing chars when typing

            DH wrote:
            I would do it in the TextChanged event handler and just
            textBox1.Text=t extBox1.Text.Re place(',','.');
            >
            This works fine, but it moves cursor at begining of textbox.
            that is assuming it is ok for it to replace all the ',' with '.' once
            the user has finished entering what ever they are entering.
            >
            what exactly do you mean by it doesn't work the way you want it to?
            What do you expect and what does it do?
            All I want to do is when user type ',' it automaticly change to '.'
            I want to intercept ',' and replace it with '.'

            Comment

            • =?ISO-8859-1?Q?Bj=F8rn_Brox?=

              #7
              Re: Replacing chars when typing

              Trooper skrev:
              DH wrote:
              >I would do it in the TextChanged event handler and just
              >textBox1.Text= textBox1.Text.R eplace(',','.') ;
              >>
              >
              This works fine, but it moves cursor at begining of textbox.
              >
              That's because you replace textBox1.Text with a new value (the only
              thing you are allowed to)

              Store the cursor position before modifying the text, and then restore it.

              --
              Bjørn Brox

              Comment

              Working...