• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1051
  • Last Modified:

translate VB6 code to C#

I have a function in VB6 which checks on keypress events. I would like to translate that to C#. I am pasting the VB6 code over here. Please if somebody can translate this into C# for me. Thanks
 If KeyAscii = 13 Or KeyAscii = 9 Then
        KeyAscii = 0
        cmdUpdate_Click
    End If
   
    Select Case KeyAscii
        Case 46 'Period check
            If (InStr(1, txtAmount.Text, ".") <> 0) And txtAmount.SelLength = 0 Then 'Only one "." allowed
                Beep
                KeyAscii = 0
            End If
        Case 48 To 57   'Make sure only 2 sigs to right of decimal
            If txtAmount.SelLength = 0 And InStr(1, txtAmount.Text, ".") <> 0 Then
                If Len(Trim$(txtAmount.Text)) - InStr(1, txtAmount.Text, ".") = 2 Then
                    Beep
                    KeyAscii = 0
                End If
            End If
            '
        Case 8  ' backspace key
            'let some keys be cool...
       
        Case 45 ' negative sign
            'let some keys be cool...
        Case Else
            KeyAscii = 0
    End Select
0
pratikshahse
Asked:
pratikshahse
2 Solutions
 
surajgupthaCommented:
What are you trying to do with this code? If we know exactly we can help you better
0
 
answer_meCommented:
if (KeyAscii == 13 || KeyAscii == 9)
            {
                KeyAscii = 0;
                cmdUpdate_Click();
            }
   
            if(KeyAscii==46)
            {
                if ( txtAmount.Text.IndexOf('.',1)>0 && txtAmount.SelectionLength == 0 ) // Only one "." allowed
                {
                    // For Beep refer http://www.thescripts.com/forum/thread213716.html
                    KeyAscii = 0;
                }
            }
            else if(KeyAscii >=48 && KeyAscii<=57)//Make sure only 2 sigs to right of decimal
                {
                     if ( txtAmount.Text.IndexOf('.',1)>0 && txtAmount.SelectionLength == 0 ) // Only one "." allowed
                     {
                          if((txtAmount.Text.Trim().Length - txtAmount.Text.IndexOf('.',1))==2 )
                          {
                              // For Beep refer http://www.thescripts.com/forum/thread213716.html
                              KeyAscii = 0;
                          }
                     }
                }
                else if( KeyAscii ==8)// backspace key
                {
                    //let some keys be cool...
                }
                else if(KeyAscii==45 )' negative sign
            {
                //  let some keys be cool...
            }
            else
            {
                KeyAscii = 0;
            }
0
 
Jaime OlivaresCommented:
Assuming you are implementing the right keypress event, should be something like:

        private void SomeControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 || e.KeyChar == 9)
            {
                   e.KeyChar = (char)0;
                   cmdUpdate_Click();
            }
            if (e.KeyChar == 46)
            {
                  if (txtAmount.Text.IndexOf(".") > 0 &&  And txtAmount.SelectionLength = 0)
                  {
                       Console.Beep();
                       e.KeyChar = (char)0;
                  }
             }
             else if (e.KeyChar >= 48 and e.KeyChar<=57)
             {
                  if (txtAmount.Text.IndexOf(".") > 0 &&  And txtAmount.SelectionLength = 0)
                  {
                        if (txtAmount.Text.Length - txtAmount.Text.IndexOf(".) == 2)
                        {
                              Console.Beep();
                              e.KeyChar = (char)0;
                        }
                   }
        }
        else if (e.KeyChar == 8)
        {
            // 'let some keys be cool...
        }
        else if (e.KeyChar == 45)
        {
            // 'let some keys be cool...
        }
        else
        {
              e.KeyChar = (char)0;
        }
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now