[RESOLVED] Beginning C# - Page 3
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 43 of 43

Thread: [RESOLVED] Beginning C#

  1. #31
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Beginning C#

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  2. #32
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    well i have been to that page dglienna but thanks you for the link. i have tried to get the enter key to do what I want and i cant seem to get it to work here is what i have on getting it to work(please note i dumped the KeyPress)
    Code:
            void Calculator_KeyDown(object sender, KeyEventArgs e)
            {            
                switch (e.KeyCode)
                {
                    case Keys.Enter: //Enter Key
                        {
                            btnEqual.PerformClick();
                            break;
                        }
                    case Keys.NumPad0: //NumPad0
                        {
                            button0.PerformClick();
                            break;
                        }
                    case Keys.NumPad1://NumPad1
                        {
                            button1.PerformClick();
                            break;
                        }
                    case Keys.NumPad2://NumPad2
                        {
                            button2.PerformClick();
                            break;
                        }
                    case Keys.NumPad3://NumPad3
                        {
                            button3.PerformClick();
                            break;
                        }
                    case Keys.NumPad4://NumPad4
                        {
                            button4.PerformClick();
                            break;
                        }
                    case Keys.NumPad5://NumPad5
                        {
                            button5.PerformClick();
                            break;
                        }
                    case Keys.NumPad6://NumPad6
                        {
                            button6.PerformClick();
                            break;
                        }
                    case Keys.NumPad7://NumPad7
                        {
                            button7.PerformClick();
                            break;
                        }
                    case Keys.NumPad8://NumPad8
                        {
                            button8.PerformClick();
                            break;
                        }
                    case Keys.NumPad9://NumPad6
                        {
                            button9.PerformClick();
                            break;
                        }
                    case Keys.D5://Modulus
                        {
                            btnModulus.PerformClick();
                            break;
                        }
                    case Keys.Add://Addition
                        {
                            btnAdd.PerformClick();
                            break;
                        }
                    case Keys.Subtract://Subtraction
                        {
                            btnSubtract.PerformClick();
                            break;
                        }
                    case Keys.Multiply://Multiply
                        {
                            btnMultiply.PerformClick();
                            break;
                        }
                    case Keys.Divide://Divide
                        {
                            btnDivide.PerformClick();
                            break;
                        }
                    case Keys.Home://Negative/Positive Toggle
                        {
                            btnAddSub.PerformClick();
                            break;
                        }
                    case Keys.PageUp://Clear Entry
                        {
                            btnClearEntry.PerformClick();
                            break;
                        }
                    case Keys.PageDown://Clear ALL Data
                        {
                            btnClear.PerformClick();
                            break;
                        }
                    case Keys.Decimal://Decimals
                        {
                            break;
                        }
                    case Keys.End://Quit
                        {
                            this.Close();
                            break;
                        }
                    default://Standard displays error
                        {
                            MessageBox.Show("All Keys are on the NumPad", "Incorrect Key", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                break;
                        }
                }
            }
    
            void Calcualtor_KeyUp(object sender, KeyEventArgs e)
            {
                //Beta test this out
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                        {
                            btnEqual.PerformClick();
                            break;
                        }
                }
            }
    
            void Calculator_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
            {
                //This is to check for nonInputKeys
                //Sets any keys needed to being InputKeys
                switch (e.KeyCode)
                {
                    case Keys.Enter://Enter Key
                        {
                            if (e.IsInputKey == false)
                            {
                                e.IsInputKey = true;
                                btnEqual.PerformClick();
                            }
                            break;
                        }
                    case Keys.End://End Key for Quitting
                        {
                            e.IsInputKey = true;
                            break;
                        }
                    case Keys.PageDown://Page Down key for Clearing Memory
                        {
                            e.IsInputKey = true;
                            break;
                        }
                    case Keys.PageUp://PageUp key for Clearing Entry
                        {
                            e.IsInputKey = true;
                            break;
                        }
                    case Keys.Home://Home Key for negative values(still working on it)
                        {
                            e.IsInputKey = true;
                            break;
                        }
                }
            }
    
            protected override bool IsInputKey(Keys keyData)
            {
                if (keyData == Keys.Enter)
                {
                    return true;
                }
                else
                {
                    return base.IsInputKey(keyData);
                }
            }
    
            protected override void OnKeyDown(KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    btnEqual.PerformClick();
                }
                else
                {
                    base.OnKeyDown(e);
                }
            }
    
            public KeyEventHandler Calculator_KeyUp { get; set; }
    as you can see i have tried to do it on KeyUp and KeyDown and neither seems to work for me. can anyone help?

  3. #33
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,295

    Re: Beginning C#

    Quote Originally Posted by rockking View Post
    see i have tried to do it on KeyUp and KeyDown and neither seems to work for me. can anyone help?
    Honestly, it's hard to tell what isn't working.

    Do you know how to set a breakpoint and debug your code?

    If not, put a breakpoint on the switch statement in the Calculator_KeyDown event handler.

    To do this, put your cursor on the switch( e.KeyCode ) line and press F9

    Then press F5 to start debugging. Next, use your keyboard to press a key.

    The program should stop on the switch statement and you can hover the mouse over the e.KeyCode value and see the key you pressed. (use F10 or F11 to single step through your code).

    If this breakpoint isn't hit when you press a key, it means that the event handler hasn't be wired up properly (or it could mean other things as well).

  4. #34
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    Okay I will look at it later on after school. I have dumped the KeyUp event so now I am only using KeyDown and PreviewKeyDown events to determine the enter key I have two protected override bools to set and use the Keys.Enter how I want it to be used but those are failing to work as well. I will look at the breakpoint tonight

  5. #35
    Join Date
    Dec 2009
    Posts
    596

    Re: Beginning C#

    Hello Rockking. I don't have a solution for you but I'd like to share this with you. When I'm trying something new sometimes I'll do a mini project that uses that something new and just focus on that. Then without all the extra ideas I would be able to just focus on the task at hand. Then build on that to more closely resemble the real scenario. Doing it that way I'll see what breaks the program and where it is that I'm doing something wrong. Maybe I'll program it to just display a message box by just capuring one Function key(F6 for example) if capturing function keys was my goal. I know it's not yours. It's a remedial approach that I use often. It works for me.

  6. #36
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,295

    Re: Beginning C#

    Quote Originally Posted by viperbyte View Post
    Hello Rockking. I don't have a solution for you but I'd like to share this with you. When I'm trying something new sometimes I'll do a mini project that uses that something new and just focus on that. Then without all the extra ideas I would be able to just focus on the task at hand. Then build on that to more closely resemble the real scenario. Doing it that way I'll see what breaks the program and where it is that I'm doing something wrong. Maybe I'll program it to just display a message box by just capuring one Function key(F6 for example) if capturing function keys was my goal. I know it's not yours. It's a remedial approach that I use often. It works for me.
    Great advice.

  7. #37
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Beginning C#

    I agree, very good advice.


    @rockking: try overriding the ProcessDialogKey() method, like this:
    Code:
    protected override bool ProcessDialogKey(Keys keyData)
            {
                if (keyData == Keys.Enter)
                {
                    // do what needs to be done...
                    return true;
                }
                else
                    return base.ProcessDialogKey(keyData);
    
            }
    This is mentioned in the article titled "How Keyboard Input Works", I think I posted a link to it earlier.
    I also think this probably isn't the only way to do it.

    BTW, I think you can safely delete the entire PreviewKeyDown handler.

  8. #38
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    thanks TheGreatCthulhu that worked exactly as i intended the Enter Key to work. i did read that article actually i read all those articles as they linked together. but i guess i didnt understand what it was saying in the "How Keyboard Input Works" article. here is the calculator before the decimal stuff gets added.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace CS_Calculator
    {
        public partial class Calculator : Form
        {
    
            int[] gbl_trigger = new int[5]; //global trigger
            float[] gbl_operand = new float[9]; //global operands
            int[] gbl_number = new int[11];//global number to be assigned to operand
            char[] gbl_operation = new char[10]; //global operation character
            float gbl_result; //self explanatory maybe, global integer for final result
            int gbl_operandNum; //global value used to identify current operand
            int gbl_operationNum; //global value used to identify current operation
    
            public Calculator() //default constructor
            {
                InitializeComponent();
                this.PreviewKeyDown += new PreviewKeyDownEventHandler(Calculator_PreviewKeyDown);
                this.KeyDown += new KeyEventHandler(Calculator_KeyDown);
            }
    
            public void initialize() //called on form_load
            {
                if (gbl_trigger[0] == 0)
                {
                    for (int i = 0; i < gbl_operand.Length; i++)
                    {
                        gbl_operand[i] = 0;
                    }
                    for (int i = 0; i < gbl_number.Length; i++)
                    {
                        gbl_number[i] = 0;
                    }
                    gbl_trigger[0] = 1;
                    gbl_result = 0;
                }
    
            }
    
            public void calculate() //called by equal button for making calculations left to right
            {
                //gbl_result = (gbl_operand[0])(gbl_operation[0])(gbl_operand[1])(gbl_operation[1])(gbl_operand[2]);
                gbl_result = gbl_operand[0];
                for (int i = 0; i < gbl_operand.Length; i++) //Loop for switching operations between operands but still flawed
                {
                switch (gbl_operation[i]) //switches operations only works with order operations are issued not in Scientific Order
                {
                    case '+':
                        {
                            //Add
                            gbl_result += gbl_operand[(i+1)];
                            break;
                        }
                    case '-':
                        {
                            //Subtract
                            gbl_result -= gbl_operand[(i+1)];
                            break;
                        }
                    case '*':
                        {
                            //Multiply
                            gbl_result *= gbl_operand[(i+1)];
                            break;
                        }
                    case '/':
                        {
                            //Divide
                            gbl_result /= gbl_operand[(i+1)];
                            break;
                        }
                    case '&#37;':
                        {
                            //Modulus
                            gbl_result %= gbl_operand[(i + 1)];
                            break;
                        }
                }
                }
                /*for (int i = 0; i < gbl_operation.Length; i++)
                {
                    gbl_result -= gbl_operation[i];
                }*/
                txtShow.Text = gbl_result.ToString();
            }
    
            private void Calculator_Load_1(object sender, EventArgs e) //Load
            {
                gbl_trigger[0] = 0;
                gbl_operand[gbl_operandNum] = 0;
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
                gbl_operandNum = 0;
                initialize();
            }
    
            private void btnClear_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < gbl_operand.Length; i++)//reset operands
                {
                    gbl_operand[i] = 0;
                }
                for (int i = 0; i < gbl_number.Length; i++)//reset numbers
                {
                    gbl_number[i] = 0;
                }
                gbl_operandNum = 0;//start from operand 0
                gbl_operationNum = 0;//start from operation 0
                gbl_result = 0;//clear result
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();//show empty values
            }
    
            private void btnEqual_Click(object sender, EventArgs e)
            {
               calculate();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                gbl_number[1] = 1;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[1];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                gbl_number[2] = 2;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[2];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                gbl_number[3] = 3;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[3];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                gbl_number[4] = 4;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[4];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                gbl_number[5] = 5;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[5];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                gbl_number[6] = 6;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[6];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                gbl_number[7] = 7;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[7];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                gbl_number[8] = 8;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[8];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                gbl_number[9] = 9;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[9];
                gbl_operand[gbl_operandNum] = gbl_number[0];
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void button0_Click(object sender, EventArgs e)
            {
               gbl_number[10] = 0;
               gbl_number[0] = (gbl_number[0] * 10) + gbl_number[10];
               gbl_operand[gbl_operandNum] = gbl_number[0];
               txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                gbl_operation[gbl_operationNum] = '+';
                gbl_operandNum++;
                gbl_operationNum++;
                for (int i = 0; i < gbl_number.Length; i++)
                {
                    gbl_number[i] = 0;
                }
    
            }
    
            private void btnSubtract_Click(object sender, EventArgs e)
            {
                gbl_operation[gbl_operationNum] = '-';
                gbl_operandNum++;
                gbl_operationNum++;
                for (int i = 0; i < gbl_number.Length; i++)
                {
                    gbl_number[i] = 0;
                }
            }
    
            private void btnMultiply_Click(object sender, EventArgs e)
            {
                gbl_operation[gbl_operationNum] = '*';
                gbl_operandNum++;
                gbl_operationNum++;
                for (int i = 0; i < gbl_number.Length; i++)
                {
                    gbl_number[i] = 0;
                }
            }
    
            private void btnDivide_Click(object sender, EventArgs e)
            {
                gbl_operation[gbl_operationNum] = '/';
                gbl_operandNum++;
                gbl_operationNum++;
                for (int i = 0; i < gbl_number.Length; i++)
                {
                    gbl_number[i] = 0;
                }
            }
    
            private void btnModulus_Click(object sender, EventArgs e)
            {
                gbl_operation[gbl_operationNum] = '%';
                gbl_operandNum++;
                gbl_operationNum++;
                for (int i = 0; i < gbl_number.Length; i++)
                {
                    gbl_number[i] = 0;
                }
            }
    
            private void btnClearEntry_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < gbl_number.Length; i++)
                {
                    gbl_number[i] = 0;
                }
                gbl_operand[gbl_operandNum] = 0;
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
            }
    
            void Calculator_KeyDown(object sender, KeyEventArgs e)
            {            
                switch (e.KeyCode)
                {
                    case Keys.Enter: //Enter Key
                        {
                            btnEqual.PerformClick();
                            break;
                        }
                    case Keys.NumPad0: //NumPad0
                        {
                            button0.PerformClick();
                            break;
                        }
                    case Keys.NumPad1://NumPad1
                        {
                            button1.PerformClick();
                            break;
                        }
                    case Keys.NumPad2://NumPad2
                        {
                            button2.PerformClick();
                            break;
                        }
                    case Keys.NumPad3://NumPad3
                        {
                            button3.PerformClick();
                            break;
                        }
                    case Keys.NumPad4://NumPad4
                        {
                            button4.PerformClick();
                            break;
                        }
                    case Keys.NumPad5://NumPad5
                        {
                            button5.PerformClick();
                            break;
                        }
                    case Keys.NumPad6://NumPad6
                        {
                            button6.PerformClick();
                            break;
                        }
                    case Keys.NumPad7://NumPad7
                        {
                            button7.PerformClick();
                            break;
                        }
                    case Keys.NumPad8://NumPad8
                        {
                            button8.PerformClick();
                            break;
                        }
                    case Keys.NumPad9://NumPad6
                        {
                            button9.PerformClick();
                            break;
                        }
                    case Keys.D5://Modulus
                        {
                            btnModulus.PerformClick();
                            break;
                        }
                    case Keys.Add://Addition
                        {
                            btnAdd.PerformClick();
                            break;
                        }
                    case Keys.Subtract://Subtraction
                        {
                            btnSubtract.PerformClick();
                            break;
                        }
                    case Keys.Multiply://Multiply
                        {
                            btnMultiply.PerformClick();
                            break;
                        }
                    case Keys.Divide://Divide
                        {
                            btnDivide.PerformClick();
                            break;
                        }
                    case Keys.Home://Negative/Positive Toggle
                        {
                            btnAddSub.PerformClick();
                            break;
                        }
                    case Keys.PageUp://Clear Entry
                        {
                            btnClearEntry.PerformClick();
                            break;
                        }
                    case Keys.PageDown://Clear ALL Data
                        {
                            btnClear.PerformClick();
                            break;
                        }
                    case Keys.Decimal://Decimals
                        {
                            break;
                        }
                    case Keys.End://Quit
                        {
                            this.Close();
                            break;
                        }
                    default://Standard displays error
                        {
                            MessageBox.Show("All Keys are on the NumPad", "Incorrect Key", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                break;
                        }
                }
            }
    
            void Calculator_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
            {
                //This is to check for nonInputKeys
                //Sets any keys needed to being InputKeys
                switch (e.KeyCode)
                {
                    case Keys.Enter://Enter Key
                        {
                            if (e.IsInputKey == false)
                            {
                                e.IsInputKey = true;
                                btnEqual.PerformClick();
                            }
                            break;
                        }
                    case Keys.End://End Key for Quitting
                        {
                            e.IsInputKey = true;
                            break;
                        }
                    case Keys.PageDown://Page Down key for Clearing Memory
                        {
                            e.IsInputKey = true;
                            break;
                        }
                    case Keys.PageUp://PageUp key for Clearing Entry
                        {
                            e.IsInputKey = true;
                            break;
                        }
                    case Keys.Home://Home Key for negative values(still working on it)
                        {
                            e.IsInputKey = true;
                            break;
                        }
                }
            }
    
            protected override bool IsInputKey(Keys keyData)
            {
                if (keyData == Keys.Enter)
                {
                    return true;
                }
                else
                {
                    return base.IsInputKey(keyData);
                }
            }
    
            protected override void OnKeyDown(KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    btnEqual.PerformClick();
                }
                else
                {
                    base.OnKeyDown(e);
                }
            }
    
            protected override bool ProcessDialogKey(Keys keyData)
            {
                if (keyData == Keys.Enter)
                {
                    btnEqual.PerformClick();
                    return true;
                }
                else
                {
                    return base.ProcessDialogKey(keyData);
                }
            }
        }
    }
    decimals are next

  9. #39
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Beginning C#

    Hint:

    With VB6, they gave you a little example. I think you'll enjoy it. Might not be too hard to emulate.

    Code:
    ' ------------------------------------------------------------------------
    '               Copyright (C) 1994 Microsoft Corporation
    '
    ' You have a royalty-free right to use, modify, reproduce and distribute
    ' the Sample Application Files (and/or any modified version) in any way
    ' you find useful, provided that you agree that Microsoft has no warranty,
    ' obligations or liability for any Sample Application Files.
    ' ------------------------------------------------------------------------
    Option Explicit
    Dim Op1, Op2                ' Previously input operand.
    Dim DecimalFlag As Integer  ' Decimal point present yet?
    Dim NumOps As Integer       ' Number of operands.
    Dim LastInput               ' Indicate type of last keypress event.
    Dim OpFlag                  ' Indicate pending operation.
    Dim TempReadout
    
    ' Click event procedure for C (cancel) key.
    ' Reset the display and initializes variables.
    Private Sub Cancel_Click()
        Readout = Format(0, "0.")
        Op1 = 0
        Op2 = 0
        Form_Load
    End Sub
    
    ' Click event procedure for CE (cancel entry) key.
    Private Sub CancelEntry_Click()
        Readout = Format(0, "0.")
        DecimalFlag = False
        LastInput = "CE"
    End Sub
    
    ' Click event procedure for decimal point (.) key.
    ' If last keypress was an operator, initialize
    ' readout to "0." Otherwise, append a decimal
    ' point to the display.
    Private Sub Decimal_Click()
        If LastInput = "NEG" Then
            Readout = Format(0, "-0.")
        ElseIf LastInput <> "NUMS" Then
            Readout = Format(0, "0.")
        End If
        DecimalFlag = True
        LastInput = "NUMS"
    End Sub
    
    ' Initialization routine for the form.
    ' Set all variables to initial values.
    Private Sub Form_Load()
        DecimalFlag = False
        NumOps = 0
        LastInput = "NONE"
        OpFlag = " "
        Readout = Format(0, "0.")
        'Decimal.Caption = Format(0, ".")
    End Sub
    
    ' Click event procedure for number keys (0-9).
    ' Append new number to the number in the display.
    Private Sub Number_Click(Index As Integer)
        If LastInput <> "NUMS" Then
            Readout = Format(0, ".")
            DecimalFlag = False
        End If
        If DecimalFlag Then
            Readout = Readout + Number(Index).Caption
        Else
            Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
        End If
        If LastInput = "NEG" Then Readout = "-" & Readout
        LastInput = "NUMS"
    End Sub
    
    ' Click event procedure for operator keys (+, -, x, /, =).
    ' If the immediately preceeding keypress was part of a
    ' number, increments NumOps. If one operand is present,
    ' set Op1. If two are present, set Op1 equal to the
    ' result of the operation on Op1 and the current
    ' input string, and display the result.
    Private Sub Operator_Click(Index As Integer)
        TempReadout = Readout
        If LastInput = "NUMS" Then
            NumOps = NumOps + 1
        End If
        Select Case NumOps
            Case 0
            If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
                Readout = "-" & Readout
                LastInput = "NEG"
            End If
            Case 1
            Op1 = Readout
            If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
                Readout = "-"
                LastInput = "NEG"
            End If
            Case 2
            Op2 = TempReadout
            Select Case OpFlag
                Case "+"
                    Op1 = CDbl(Op1) + CDbl(Op2)
                Case "-"
                    Op1 = CDbl(Op1) - CDbl(Op2)
                Case "X"
                    Op1 = CDbl(Op1) * CDbl(Op2)
                Case "/"
                    If Op2 = 0 Then
                       MsgBox "Can't divide by zero", 48, "Calculator"
                    Else
                       Op1 = CDbl(Op1) / CDbl(Op2)
                    End If
                Case "="
                    Op1 = CDbl(Op2)
                Case "%"
                    Op1 = CDbl(Op1) * CDbl(Op2)
                End Select
            Readout = Op1
            NumOps = 1
        End Select
        If LastInput <> "NEG" Then
            LastInput = "OPS"
            OpFlag = Operator(Index).Caption
        End If
    End Sub
    
    ' Click event procedure for percent key (%).
    ' Compute and display a percentage of the first operand.
    Private Sub Percent_Click()
        Readout = Readout / 100
        LastInput = "Ops"
        OpFlag = "%"
        NumOps = NumOps + 1
        DecimalFlag = True
    End Sub
    They have a INDEX for the buttons. Kind of different in C#.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #40
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    okay so someone here will be able to answer this for me. i have a decimal counter and a trigger for it. essetially you hit the decimal button and the trigger gets set to run different codes under each number. i have the triggered code but i cannot get any of it to run for some reason. i can hit the decimal key and display say 5.0 but then when i hit 1 i get -6 rather than 5.1 like i need. how can i get that to work properly because i really need it working properly to finish the calculator. also how do you work with powers in C# i know i C++ its 'pow' but what is the notation for it in C#? thanks for all the help you guys.

  11. #41
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,295

    Re: Beginning C#

    Quote Originally Posted by rockking View Post
    okay so someone here will be able to answer this for me. i have a decimal counter and a trigger for it. essetially you hit the decimal button and the trigger gets set to run different codes under each number. i have the triggered code but i cannot get any of it to run for some reason. i can hit the decimal key and display say 5.0 but then when i hit 1 i get -6 rather than 5.1 like i need. how can i get that to work properly because i really need it working properly to finish the calculator. also how do you work with powers in C# i know i C++ its 'pow' but what is the notation for it in C#? thanks for all the help you guys.
    If you need help with code, you're going to have to post the code. Describing the code and what is wrong isn't enough for us to help.

    As far as the power method in C#, I suggest that you search bing or google for it. Search on keywords such as "C# math pow power" and see what type of hits you get back.

    Also, search directly in msdn. If you aren't familar with msdn, it's Microsoft's api documentation resource center.

    http://msdn.microsoft.com/en-us/library/default.aspx

  12. #42
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    alright here is moment you are all waiting for i guess. here is the code behind the decimal button and then the button for 1. as well as the declarations used.
    Code:
            int[] gbl_trigger = new int[5]; //global trigger [0] = initilize, [4] = decimal
            double[] gbl_operand = new double[9]; //global operands
            int[] gbl_number = new int[11];//global number to be assigned to operand
            char[] gbl_operation = new char[10]; //global operation character
            double gbl_result; //self explanatory maybe, global integer for final result
            int gbl_operandNum; //global value used to identify current operand
            int gbl_operationNum; //global value used to identify current operation
            double gbl_decimal; //global value for places after decimals
            int gbl_deciCounter; //counts current decimal
            double gbl_tempDecimal;//Temproary Decimal
    declarations above
    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                if (gbl_trigger[4] == 1)
                {
                    gbl_tempDecimal = 1;
                    gbl_decimal = Math.Pow(gbl_tempDecimal, gbl_deciCounter);
                    gbl_operand[gbl_operandNum] = gbl_number[0] + gbl_decimal;
                    txtShow.Text = gbl_operand[gbl_operandNum].ToString();
                }
                else
                {
                    gbl_number[1] = 1;
                    gbl_number[0] = (gbl_number[0] * 10) + gbl_number[1];
                    gbl_operand[gbl_operandNum] = gbl_number[0];
                    txtShow.Text = gbl_operand[gbl_operandNum].ToString();
                }
            }
    button 1 above
    Code:
            private void btnDecimal_Click(object sender, EventArgs e)
            {
                gbl_deciCounter--;
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
                gbl_trigger[4] = 1;
            }
    and this is the decimal button.
    essentially what happens is there will be two seperate sets of code on each button and depending on the state of "gbl_trigger[4]". if it is '0' it does normal numbers(1,2,3) if it is '1' it is suppose to set decimals(1.1, 1.2, 1.3) i cant seem to get it working. i tried to just assign the value but that wont work because then you have incorrect math values when you just start doing operations with them. i searched bing for C# powers and i found a post on them. i read it and i believe i did it correctly but i am going to search MSDN too. just to make sure its done correctly. thanks guys.

  13. #43
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Beginning C#

    Quote Originally Posted by rockking View Post
    i searched bing for C# powers and i found a post on them. i read it and i believe i did it correctly but i am going to search MSDN too. just to make sure its done correctly. thanks guys.
    As I've said a few posts back:
    Quote Originally Posted by TheGreatCthulhu View Post
    Also, check out the Math class, you will want to use it.
    Of course, If I recall correctly, you basically skipped that post (more or less); though I admit it was rather long...
    But, the good thing is that you found some info on your own.
    My advice is to google for: msdn math class - anything you need will be there.

    Quote Originally Posted by rockking View Post
    [...] the declarations used.
    Code:
            int[] gbl_trigger = new int[5]; //global trigger [0] = initilize, [4] = decimal
            double[] gbl_operand = new double[9]; //global operands
            int[] gbl_number = new int[11];//global number to be assigned to operand
            char[] gbl_operation = new char[10]; //global operation character
            double gbl_result; //self explanatory maybe, global integer for final result
            int gbl_operandNum; //global value used to identify current operand
            int gbl_operationNum; //global value used to identify current operation
            double gbl_decimal; //global value for places after decimals
            int gbl_deciCounter; //counts current decimal
            double gbl_tempDecimal;//Temproary Decimal
    I have just two points: (1) as I said before, gbl_trigger[0], ... , gbl_trigger[4] aren't descriptive names, nor is int the best type to be used for trigger-like data. These are, of course, recommendations, with good reasons behind them - but, how you name your variables is, in the end, up to you; (2) I think you're making things more complicated then they are or need to be. Behind the scenes, you can simply either work strictly with numbers, and convert them to string representation when required, or use a string based approach similar to the one used in the VB code posted by dglienna.

    Quote Originally Posted by rockking View Post
    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                if (gbl_trigger[4] == 1)
                {
                    gbl_tempDecimal = 1;
                    gbl_decimal = Math.Pow(gbl_tempDecimal, gbl_deciCounter);
                    gbl_operand[gbl_operandNum] = gbl_number[0] + gbl_decimal;
                    txtShow.Text = gbl_operand[gbl_operandNum].ToString();
                }
    Ok, what does Math.Pow(gbl_tempDecimal, gbl_deciCounter) do if the value of gbl_tempDecimal is 1?
    I guess you wanted to turn the last entry - number 1 - into 0.1, or 0.01, or 0.001..., and add it to the previous entry; to do that, you need to divide it with a number that is a power of 10. Think about how you need to change the code in order to do it.

    Quote Originally Posted by rockking View Post
    Code:
                else
                {
                    gbl_number[1] = 1;
                    gbl_number[0] = (gbl_number[0] * 10) + gbl_number[1];
                    gbl_operand[gbl_operandNum] = gbl_number[0];
                    txtShow.Text = gbl_operand[gbl_operandNum].ToString();
                }
            }
    To me, this all feels as if you're making your life harder? What are the rules you use to determine what is a number, what is an operand, and when one becomes the other, which of the 9 operands this is, etc.?
    There's really no need for the 9 operands. Consider how a calculator works.
    First you enter some numbers, then you hit an operator-button, like [+]; at that point what you entered is the first operand.
    Than you enter some more numbers, and when you're done, these new numbers become the second operand. Of course, since the calculator doesn't know that you've done entering numbers, it waits until you hit another operator-button.
    If this was [=], it simply adds them up and prints out the result.
    If you entered, say [+] once more, it will again simply add them up, and replace the first operand with the result, and then it'll wait for you to enter the second operand.
    That's it. A simple calculator doesn't care how many operands you have planed - it works with only a few.
    That being said, some operations require only one operand, like SQRT(x), or the sign change.

    Quote Originally Posted by rockking View Post
    Code:
            private void btnDecimal_Click(object sender, EventArgs e)
            {
                gbl_deciCounter--;
                txtShow.Text = gbl_operand[gbl_operandNum].ToString();
                gbl_trigger[4] = 1;
            }
    Again, the point I started with, and designated it with (1): anyone reading your code should be able to tell at once what gbl_trigger[4] exactly triggers, without having to refer to the comment you provided with the declaration; since the name is not that descriptive, that is not the case.
    Also, consider this: If you decided to work with another project for a time, and then came back to this one, even you would have trouble remembering what exactly that line did.
    However, don't change it now, since you might make a mess. Try to get your code to work first.
    Last edited by TheGreatCthulhu; October 19th, 2010 at 02:15 PM.

Page 3 of 3 FirstFirst 123

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center