Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Any C# PRO can help me 3 simple coding? (http://www.go4expert.com/forums/c-sharp-pro-help-3-simple-coding-t893/)

Zlebronjames 14Jun2006 18:01

Any C# PRO can help me 3 simple coding?
 
^_^The code is very short one that I remember I teacher show me before but I cannot remember. This may be long to read but the code is really simple plz here me Im new to C#. There are 3 steps needing to change the program.

1)*Never disabling the add grade button* :cool:
remove the code from the btnAdd_click event handler so that the Add Grade Button is not disabled after entering 10 grades.

2)*Summing the grades in the ListBox.* :eek:
modify code in the btnAverage_Click event handler so that inGradeCounter is incremented until it is equal to the number of grades entered. Use lstGrades.Items.Count to determine the number of items in the Listbox. The number returned by the Count property will be zero if there are no grades entered. Use an if selection statement to avoid division by zero and display a message dialog to the user if there are no grades entered when the user clicks the Average button.

3)*Calcualting the class average* :confused:
Modify the code in the btnAverage_Click event handler so that dblAverage is computed by using intGradeCounter rather than the Value 10.


I had the statment in short for some of the main code
Code: C#

static void Main()
     {
        Application.Run( new FrmClassAverage() );
     }

     // handles Add Grade Button's Click event
     private void btnAdd_Click(
        object sender, System.EventArgs e )
     {
        // clear previous grades and calculation result
        if ( lblOutput.Text != "" )
        {
           lblOutput.Text = "";
           lstGrades.Items.Clear();
        }

        // display grade in ListBox
        lstGrades.Items.Add( Int32.Parse( txtInput.Text ) );
        txtInput.Clear(); // clear grade from TextBox
        txtInput.Focus(); // transfer focus to TextBox

        // prohibit users from entering more than 10 grades
        if ( lstGrades.Items.Count == 10 )
        {
           btnAdd.Enabled = false; // disable Add Grade Button
           btnAverage.Focus(); // transfer focus to Average Button
        }

     } // end method btnAdd_Click

     // handles Average Button's Click event
     private void btnAverage_Click(
        object sender, System.EventArgs e )
     {
        // initialization phase
        int intTotal = 0;
        int intGradeCounter = 0;
        int intGrade = 0;
        double dblAverage = 0;

        // sum grades in ListBox
        do
        {
           // read grade from ListBox
           intGrade = ( int )
              lstGrades.Items[ intGradeCounter ];
           intTotal += intGrade; // add grade to total
           intGradeCounter++; // increment counter
        } while ( intGradeCounter < 10 );

        dblAverage = intTotal / 10.0; // calculate average
        lblOutput.Text = String.Format( "{0:F}", dblAverage );
        btnAdd.Enabled = true; // enable Add Grade Button
        txtInput.Focus(); // reset focus to Enter grade: TextBox

     } // end method btnAverage_Click

  } // end class FrmClassAverage
}


shabbir 15Jun2006 13:26

Re: Any C# PRO can help me 3 simple coding?
 
Quote:

Originally Posted by Zlebronjames
1)*Never disabling the add grade button*

Comment out the if below the lines // prohibit users from entering more than 10 grades

Quote:

Originally Posted by Zlebronjames
2)*Summing the grades in the ListBox.*

Use lstGrades.Items.Count instead of 10 in while ( intGradeCounter < 10 ); and after that add the if condition.

Quote:

Originally Posted by Zlebronjames
3)*Calcualting the class average*

Use the variable intGradeCounter intTotal / 10.0; instead of 10

You cannot ask for the code solution as thats against the TOS of G4EF.

Thanks
Shabbir

Zlebronjames 15Jun2006 14:59

Re: Any C# PRO can help me 3 simple coding?
 
Quote:

Originally Posted by shabbir
Comment out the if below the lines // prohibit users from entering more than 10 grades


Use lstGrades.Items.Count instead of 10 in while ( intGradeCounter < 10 ); and after that add the if condition.


Use the variable intGradeCounter intTotal / 10.0; instead of 10

May I know how to use lstGrades.Items.Count command? Can show me example?

I also need to know how the intGradeCounter works to be able to count the variable number?
Im new to this software so I may need to know more.

Thanks for your understanding.

shabbir 15Jun2006 15:27

Re: Any C# PRO can help me 3 simple coding?
 
Quote:

May I know how to use lstGrades.Items.Count command? Can show me example?
Just replace 10 with lstGrades.Items.Count. Just giving the example would mean solving of the problem and thats against G4EF terms.
Quote:

I also need to know how the intGradeCounter works to be able to count the variable number?
By incrementing it.


All times are GMT +5.5. The time now is 07:09.