none
How to implement these in my VB calculator?

    Question

  • I have made a calculator in Visual Basic 2010 (VS 2010 Ultimate). Can anyone help me in the following areas? I would greatly appreciate it.

    1. Implementing type functionality without letting the user type unwanted characters
    2. Making the cursor not visible in the text box (which is read-only)
    3. Negative and decimal factorials
    4. The "slide" functionality like in Windows 7 or XP calculators (when you press Scientific, Programmer, etc. modes, the window slides to accomodate them)
    5. Creating a basic help file (I am very new to web programming)

    Thanks,

    Rahul Yedida

    • Moved by Annabella Luo Wednesday, May 02, 2012 7:57 AM (From:Windows Presentation Foundation (WPF))
    Monday, April 30, 2012 3:50 PM

Answers

  • Hi Rahul. Sorry, my code is in C#, but it will not be too difficult to change to VB.NET. For point number 1, I want to make the following suggestion. This is code I used to validate if the value is numeric on a Windows Form: 

    private void ValidateNumeric(object sender, KeyPressEventArgs e)
    {            
        if (!(IsNumeric(e.KeyChar)))
            e.Handled = true;
    } 

    If the value typed in by the user is not numeric, the key will not register a value in the textbox. In other words, nothing will happen if the user types a non numeric value. There are however exceptions such as a period and a backspace for instance. These are catered for in the IsNumeric function. The IsNumeric function code is as follows:

    public static bool IsNumeric(object KeyPressed)
    {
        // Variable to collect the Return value of the TryParse method.
        bool isNum;
    
        // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
        double retNum;
    
        // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
        // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
        isNum = Double.TryParse(Convert.ToString(KeyPressed), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
    
        //Allow the user to type a backspace and a decimal point
        if (Convert.ToString(KeyPressed) == "\b" | Convert.ToString(KeyPressed) == ".")
            isNum = true;
    
        return isNum;
    }

    Then on your Windows Form, select the Textbox control you want to validate as the user types:

    Then in the properties pane, for the events select the KeyPress event and set it to your ValidateNumeric method:  

    You can now do the same for all textboxes you want to validate because you are sharing the KeyPress event handler (ValidateNumeric). You can do a lot more to improve the code like using String.IsNullOrEmpty or String.IsNullOrWhiteSpace to check for empty text boxes. I hope that this code helps you a bit. Good luck.


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. This helps us build a healthy and positive community. Happy programming!



    • Edited by DirkStrauss Wednesday, May 02, 2012 11:26 AM
    • Marked as answer by rahuly1039 Thursday, May 03, 2012 5:36 AM
    Wednesday, May 02, 2012 11:18 AM

All replies

  • Hi rahuly1039,

    Do this calculator is WPF apps? We will give you accordingly solution base on what technique you use.

    Best regards.

    Wednesday, May 02, 2012 7:38 AM
  • Hi Rosa,

    My calculator is a Windows Forms Application.

    Regards,

    Rahul Yedida

    Wednesday, May 02, 2012 7:47 AM
  • Hi rahuly1039,

    According to your descirption, I understand your issue is about Windows Forms, so I'll moving your thread to Windows Form forum, so that you can get a better help.

    Thank you for your understanding and support.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 02, 2012 7:54 AM
  • Hi Rahul. Sorry, my code is in C#, but it will not be too difficult to change to VB.NET. For point number 1, I want to make the following suggestion. This is code I used to validate if the value is numeric on a Windows Form: 

    private void ValidateNumeric(object sender, KeyPressEventArgs e)
    {            
        if (!(IsNumeric(e.KeyChar)))
            e.Handled = true;
    } 

    If the value typed in by the user is not numeric, the key will not register a value in the textbox. In other words, nothing will happen if the user types a non numeric value. There are however exceptions such as a period and a backspace for instance. These are catered for in the IsNumeric function. The IsNumeric function code is as follows:

    public static bool IsNumeric(object KeyPressed)
    {
        // Variable to collect the Return value of the TryParse method.
        bool isNum;
    
        // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
        double retNum;
    
        // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
        // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
        isNum = Double.TryParse(Convert.ToString(KeyPressed), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
    
        //Allow the user to type a backspace and a decimal point
        if (Convert.ToString(KeyPressed) == "\b" | Convert.ToString(KeyPressed) == ".")
            isNum = true;
    
        return isNum;
    }

    Then on your Windows Form, select the Textbox control you want to validate as the user types:

    Then in the properties pane, for the events select the KeyPress event and set it to your ValidateNumeric method:  

    You can now do the same for all textboxes you want to validate because you are sharing the KeyPress event handler (ValidateNumeric). You can do a lot more to improve the code like using String.IsNullOrEmpty or String.IsNullOrWhiteSpace to check for empty text boxes. I hope that this code helps you a bit. Good luck.


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. This helps us build a healthy and positive community. Happy programming!



    • Edited by DirkStrauss Wednesday, May 02, 2012 11:26 AM
    • Marked as answer by rahuly1039 Thursday, May 03, 2012 5:36 AM
    Wednesday, May 02, 2012 11:18 AM
  • Is your application really a web application?  You apparently posted in the WPF forum, and your thread was moved to the WinForms forum, neither of which is specific for web applications.

    You are asking several unrelated questions at once.  I suggest that you ask the questions in separate threads, one at a time.  I understand that all of your issues are related to one project, but your separate issues may be best answered in separate forums.

    Meanwhile, here's some food for thought on one approach to writing your calculator.

    http://msdn.microsoft.com/en-us/library/dd460648.aspx#calculator_logic  

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    • Edited by Rudedog2MVP Wednesday, May 02, 2012 7:09 PM
    Wednesday, May 02, 2012 7:09 PM