Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Repalce dot with comma

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joza - 07 Feb 2008 14:43 GMT
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!
zacks@construction-imaging.com - 07 Feb 2008 14:58 GMT
> Hi!
>
[quoted text clipped - 15 lines]
>
> Thnx!

           if (e.KeyChar == Convert.ToChar("."))
               e.KeyChar = Convert.ToChar(",");
DeveloperX - 07 Feb 2008 15:06 GMT
On 7 Feb, 14:58, za...@construction-imaging.com wrote:

> > Hi!
>
[quoted text clipped - 20 lines]
>
> - 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.
zacks@construction-imaging.com - 07 Feb 2008 16:48 GMT
> On 7 Feb, 14:58, za...@construction-imaging.com wrote:
>
[quoted text clipped - 25 lines]
> 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.
christery@gmail.com - 07 Feb 2008 19:19 GMT
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
DeveloperX - 07 Feb 2008 15:05 GMT
> Hi!
>
[quoted text clipped - 15 lines]
>
> 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.
Joza - 07 Feb 2008 15:27 GMT
> if(e.KeyChar == '.')
> {
[quoted text clipped - 7 lines]
> You don't really need to use the box variable, it just makes it more
> general purpose.

Thank you very much! That's it! :)
Ben Voigt [C++ MVP] - 07 Feb 2008 15:45 GMT
>> Hi!
>>
[quoted text clipped - 25 lines]
> 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.

> You don't really need to use the box variable, it just makes it more
> general purpose.
Frank Adam - 08 Feb 2008 06:05 GMT
>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. :)

Signature

Regards, Frank

Ben Voigt [C++ MVP] - 11 Feb 2008 14:51 GMT
>> This doesn't properly handle the case when there is text selected
>> already.
[quoted text clipped - 22 lines]
> 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.
christery@gmail.com - 12 Feb 2008 01:06 GMT
> 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
Frank Adam - 12 Feb 2008 02:04 GMT
>> 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

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.

Signature

Regards, Frank


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.