//Code Written By S Barratt

i would like my button display to - out put all the values of the arrayList.

------------------------------------------------------------------------------------------

i would very much like some information about key pressing so when the use presses the key enter return ( char 13).
i did try - but my key press_ down just does not work.
-----------------------------------------------------------------------------------------------

finally i would like someone to show me how to format a number into dollars or pounds and how to round a decimal to two points

i know it goes string("00.00.00"" but where do i put String(Fomat type)?? - eg. at start of value i am using to store data input at the end when i am doing data output?

------------------------------------------------------------------------------------------------

finally i need some help with dates and the addition of dates
any help or refrencing to other material would be greatly apprechiated.
This is A program storing scores etc.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Exercise13
{
    public partial class FrmCalculateAverageScore : Form
    {

        ArrayList StoreValueOfScores = new ArrayList();
        int Numberofscores;
        int TotalOfScore;
        int i;
        int ValueScore;
        bool DTrueOrFalse;
        public FrmCalculateAverageScore()
        {
            InitializeComponent();
        }

        private void BtnCalculate_Click(object sender, EventArgs e)
        {

            int AverageScore;
            DTrueOrFalse = false;

            try
            {
                ValueScore = int.Parse(TbValueOfScore.Text);

            }
            catch (FormatException)
            {
                MessageBox.Show("Enter a number into Score Below 101");
                TbValueOfScore.Clear();
                TbValueOfScore.Focus();
                DTrueOrFalse = true;
            }
            catch (OverflowException)
            {
                MessageBox.Show("Enter a number into Score Below 101");
                TbValueOfScore.Clear();
                TbValueOfScore.Focus();
                DTrueOrFalse = true;

            }
            if (ValueScore > 101)
            {
                DTrueOrFalse = true;
                MessageBox.Show("Enter a number into Score Below 101");
                TbValueOfScore.Clear();
                TbValueOfScore.Focus();
            }
            if (ValueScore < 0)
            {
                DTrueOrFalse = true;
                MessageBox.Show("Enter a number into score Above 0");
                TbValueOfScore.Clear();
                TbValueOfScore.Focus();
            }


            if (DTrueOrFalse == false)
            {
                Numberofscores = Numberofscores + 1;
                LblNumberOfScores.Text = Numberofscores.ToString();
                StoreValueOfScores.Add(ValueScore);
                TbValueOfScore.Clear();
                TbValueOfScore.Focus();

                for (int i = 0; i < StoreValueOfScores.Count; i++)
                {
                    TotalOfScore = TotalOfScore + ValueScore;
                    LblScoreTotal.Text = TotalOfScore.ToString();
                    AverageScore = TotalOfScore / Numberofscores;
                    LblAverageScore.Text = AverageScore.ToString();
                }
            }
        }

        private void BtnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            for (i = 0; i < StoreValueOfScores.Count; i++)
            {
                StoreValueOfScores.Remove(ValueScore);
                LblNumberOfScores.Text = 0.ToString();
                LblAverageScore.Text = 0.ToString();
                TbValueOfScore.Focus();
                LblScoreTotal.Text = 0.ToString();
                Numberofscores = 0;
                TotalOfScore = 0;
                ValueScore = 0;
            }
        }

        private void BtnDisplay_Click(object sender, EventArgs e)
        {

            for (i = 0; i < StoreValueOfScores.Count; i++)
            {
               

MessageBox.Show(StoreValueOfScores[i].ToString());
            
            }
            
        }
    }
}

//Code Written By S Barratt

Recommended Answers

All 8 Replies

Hi,

Few suggestions for your questions;

i would like my button display to - out put all the values of the arrayList.
Sorry, not much clear to me.

i would very much like some information about key pressing so when the use presses the key enter return ( char 13).
i did try - but my key press_ down just does not work.

Use from KeyDown event, Keys.Enter
here is a piece of code

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
                {
                    case Keys.Enter:
                        SendKeys.Send("{Tab}");
                        break;
                    case Keys.Escape:
                        btnCancel_Click(this,e);
                        break;
}

finally i would like someone to show me how to format a number into dollars or pounds and how to round a decimal to two points
Please have a look at
http://blog.stevex.net/?page_id=33

finally i need some help with dates and the addition of dates
any help or refrencing to other material would be greatly apprechiated.

DateTime dt1 = new DateTime();
dt1.AddDays(1);

The above code will add a day to the date. similarly it provide multiple options to add time, month,...etc. Did this solve your problem?

Good luck

i would very much like some information about key pressing so when the use presses the key enter return ( char 13).
i did try - but my key press_ down just does not work.
-----------------------------------------------------------------------------------------------

MeSampath pretty much covered it, only thing i'd mention is to check that you have created the event handler that calls your key press method:

namespace Exercise13
{
    public partial class FrmCalculateAverageScore : Form
    {
         public FrmCalculateAverageScore()
        {
            InitializeComponent();
            this.button1.KeyPress+=new KeyPressEventHandler(button1_KeyPress);
        }

If you create the event handler through the Visual Studio designer this code is created in the Form1.Designer.cs partial file. This is the code that calls your method when the assigned event is raised.

Thanks everyone :)

my first question rephased- to better english construction

But if i input say 22, 33 and 44 into an arraylist (dynamic array)

how would i display all 3 values in one message box instead of 3 indivual ones. As done by using the for loop.

and how would i sort the array to appear in order?.
More help would Strongely improve my understanding and it would be apprechiated.
-----------------------------------------------------------------------------------

Here is an example for your messagebox question in which the ArrayList is cast to an Array, which the string.Format method can then use to format a string with individual placeholders (eg. {0}, {1}, etc.):

ArrayList ary = new ArrayList();
                ary.Add("one");
                ary.Add("two");
                ary.Add("three");
                string s = string.Format("{0}\r\n{1}\r\n{2}\r\n", ary.ToArray());
                MessageBox.Show("My ArrayList contains:\r\n\r\n" + s);

In order to sort the array: ArrayList.Sort . If you need to sort complex items, you need to provide an IComparer .

You may wish to consider using List<> (eg. List<int>) instead of an ArrayList in the future.

thanks everyone.

            string s = string.Format("{0}\r\n{1}\r\n{2}\r\n", ary.ToArray());
            MessageBox.Show("My ArrayList contains:\r\n\r\n" + s);

these lines seem intresting but how would you do it for an index - which can constantly get bigger by the users input? . for example 3 values one time , 4 values another time or X values another time.

it has to be in an array or arraylist which is kinda of a shame as list<> is easier no dought.

thanks everyone.
string s = string.Format("{0}\r\n{1}\r\n{2}\r\n", ary.ToArray());
MessageBox.Show("My ArrayList contains:\r\n\r\n" + s);

these lines seem intresting but how would you do it for an index - which can constantly get bigger by the users input? . for example 3 values one time , 4 values another time or X values another time.

it has to be in an array or arraylist which is kinda of a shame as list<> is easier no dought.
-----------------------------------------------------------------

You could use a loop to build the string. In the following example, notice that the loop resolves each item to an object type before casting it to a string . This is because ArrayList is not a "strongly typed" container and treats its items simply as object types. Unfortunately, this also means you could store mixed types in the array, which would cause an error if you try to cast it to the wrong type.

string s2 = string.Empty;
                foreach (object oString in ary)
                    s2 += (string)oString + "\r\n";
                MessageBox.Show("My ArrayList contains:\r\n\r\n" + s);
commented: Wow, Practically a Posting Shark = True lol. +1

This is because ArrayList is not a "strongly typed" container and treats its items simply as object types. Unfortunately, this also means you could store mixed types in the array, which would cause an error if you try to cast it to the wrong type.

I think DdoubleD mentioned this in passing, but its a point worth highlighting. When storing a single piece of data you (hopefully) use a strongly typed variable such as string/int/bool/etc. The same applies to collections; if you are storing a collection of strings then use a List<string>. You wouldnt use an object for a single variable object myString = new object(); :p

By using strongly typed collections you can avoid a lot of InvalidCastExceptions. You can check that the data is of the correct type when entering it into the collection then you know what you are working with any time you retrieve an item from the collection.

Also, when you create a variable/collection an area of memory is set aside to store it. Because an object can contain almost ANY type of data the memory set aside may be much larger than is needed. By using a strongly typed collection you only use what memory you ened :)

commented: Very helpful- +1

thank you - DdoubleD

replaced string with int32 after exception handle mentioned by ryshad in more detail and now most of my problems are solved. i may need more help so look for Can you help me with C# again section 2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.