Repalce dot with comma

J

Joza

Hi!

How to replace dot with comma when user is typing somewhat in textBox?
I have wrote this part of code to detect typed dot, but how to
replace it with coma:

private void txtNum_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (int)(char)'.')
{
e.Handled=true;

// TO DO

}
}

Thnx!
 
Z

zacks

Hi!

How to replace dot with comma when user is typing somewhat in textBox?
I have wrote this part of code to detect typed dot, but how to
replace it with coma:

private void txtNum_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
        if (e.KeyChar == (int)(char)'.')
        {
        e.Handled=true;

        // TO DO

        }

}

Thnx!

if (e.KeyChar == Convert.ToChar("."))
e.KeyChar = Convert.ToChar(",");
 
D

DeveloperX

Hi!

How to replace dot with comma when user is typing somewhat in textBox?
I have wrote this part of code to detect typed dot, but how to
replace it with coma:

private void txtNum_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
        if (e.KeyChar == (int)(char)'.')
        {
        e.Handled=true;

        // TO DO

        }

}

Thnx!

if(e.KeyChar == '.')
{
TextBox box = (TextBox)sender;
int pos = box.SelectionStart;
box.Text = box.Text.Insert(pos,",");
box.SelectionStart = pos + 1;
e.Handled = true;
}

You don't really need to use the box variable, it just makes it more
general purpose.
 
D

DeveloperX

            if (e.KeyChar == Convert.ToChar("."))
                e.KeyChar = Convert.ToChar(",");- Hide quoted text -

- Show quoted text -

KeyChar is read only, well on 1.1 anyway, just getting 2008 installed
on this machine at the moment. If it's changed I'll post back.
 
J

Joza

DeveloperX said:
if(e.KeyChar == '.')
{
TextBox box = (TextBox)sender;
int pos = box.SelectionStart;
box.Text = box.Text.Insert(pos,",");
box.SelectionStart = pos + 1;
e.Handled = true;
}

You don't really need to use the box variable, it just makes it more
general purpose.

Thank you very much! That's it! :)
 
B

Ben Voigt [C++ MVP]

DeveloperX said:
if(e.KeyChar == '.')
{
TextBox box = (TextBox)sender;
int pos = box.SelectionStart;
box.Text = box.Text.Insert(pos,",");
box.SelectionStart = pos + 1;
e.Handled = true;
}

This doesn't properly handle the case when there is text selected already.

Try constructing a new KeyPressEventArgs with a comma, and pass that to
base.OnKeyPress.... oh wait, you're using an event, not an override. Call
SendMessage(box.Handle, WM_CHAR, ...) to get the proper default handling of
the key.
 
Z

zacks

KeyChar is read only, well on 1.1 anyway, just getting 2008 installed
on this machine at the moment. If it's changed I'll post back.

It must be. I tested that code in VS2005 and it worked fine.
 
C

christery

Nope...

if u paste code in (to the textbox) handeling it like that it wont
work I think... u know ctrl-v...

not while I tested it in 2005 anyways... but that was for 0-9 only
accepted... or was it VB6?

hmmm... dont remember.. well, tried the same solution and it worked
on kanual input but not on copy/paste

check what u got when u leave... and if wrong input warn user to
carefully RTFM, and exit program/bluescreen ;)

//CY
 
F

Frank Adam

This doesn't properly handle the case when there is text selected already.

Try constructing a new KeyPressEventArgs with a comma, and pass that to
base.OnKeyPress.... oh wait, you're using an event, not an override. Call
SendMessage(box.Handle, WM_CHAR, ...) to get the proper default handling of
the key.
In '08, a simple "if (e.KeyChar == '.') e.KeyChar = ',';" certainly
works, though obviously won't handle pastes.

To add to the confusion and as long as complete novels are not
entered, one could just use TextChanged. For the average text fields
used in my apps, i can't outtype this thing below. Maybe i'm too slow.
;)

private void tb_Changed(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
int tbStart = tb.SelectionStart;
tb.Text = tb.Text.Replace('.', ',');
tb.SelectionStart = tbStart;
}

Then again, why not just do that on Validate.. the only time i found
swapping keys on a user is for prank purposes. :)
 
B

Ben Voigt [C++ MVP]

Frank said:
In '08, a simple "if (e.KeyChar == '.') e.KeyChar = ',';" certainly
works, though obviously won't handle pastes.

To add to the confusion and as long as complete novels are not
entered, one could just use TextChanged. For the average text fields
used in my apps, i can't outtype this thing below. Maybe i'm too slow.
;)

private void tb_Changed(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
int tbStart = tb.SelectionStart;
tb.Text = tb.Text.Replace('.', ',');
tb.SelectionStart = tbStart;
}

Then again, why not just do that on Validate.. the only time i found
swapping keys on a user is for prank purposes. :)

The specific case of dot/comma suggests international entry of numeric
values.
 
C

christery

The specific case of dot/comma suggests international entry of numeric
values.- Dölj citerad text -

Hmm, 123,312.12 uses both... wich is wich.. is there a INTERnational
standard for the case? and if not why? yes u can put it in win doze
that it will read (or anyway write it that way) but is that
implemented... Im running circles around swedish chars... really nice
when going dipped in 7 bit somewhere... it sort of get like "Wdlcome"
instead of "Welcome"...

//CY
 
F

Frank Adam

Hmm, 123,312.12 uses both... wich is wich.. is there a INTERnational
Indeed. Hence my notion that this should rather be done on Validate or
perhaps on the enter key and not drive the user nuts with replacing
his or her keystrokes. I know that if i typed a dot and it turned into
a comma, i'd be staggering up the keyboard for the backspace and
blaming the bottle for mishitting the keys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top