some help please.

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 16
Reputation: brightsolar is an unknown quantity at this point 
Solved Threads: 0
brightsolar brightsolar is offline Offline
Newbie Poster

some help please.

 
0
  #1
17 Days Ago
//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.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Collections;
  9.  
  10. namespace Exercise13
  11. {
  12. public partial class FrmCalculateAverageScore : Form
  13. {
  14.  
  15. ArrayList StoreValueOfScores = new ArrayList();
  16. int Numberofscores;
  17. int TotalOfScore;
  18. int i;
  19. int ValueScore;
  20. bool DTrueOrFalse;
  21. public FrmCalculateAverageScore()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. private void BtnCalculate_Click(object sender, EventArgs e)
  27. {
  28.  
  29. int AverageScore;
  30. DTrueOrFalse = false;
  31.  
  32. try
  33. {
  34. ValueScore = int.Parse(TbValueOfScore.Text);
  35.  
  36. }
  37. catch (FormatException)
  38. {
  39. MessageBox.Show("Enter a number into Score Below 101");
  40. TbValueOfScore.Clear();
  41. TbValueOfScore.Focus();
  42. DTrueOrFalse = true;
  43. }
  44. catch (OverflowException)
  45. {
  46. MessageBox.Show("Enter a number into Score Below 101");
  47. TbValueOfScore.Clear();
  48. TbValueOfScore.Focus();
  49. DTrueOrFalse = true;
  50.  
  51. }
  52. if (ValueScore > 101)
  53. {
  54. DTrueOrFalse = true;
  55. MessageBox.Show("Enter a number into Score Below 101");
  56. TbValueOfScore.Clear();
  57. TbValueOfScore.Focus();
  58. }
  59. if (ValueScore < 0)
  60. {
  61. DTrueOrFalse = true;
  62. MessageBox.Show("Enter a number into score Above 0");
  63. TbValueOfScore.Clear();
  64. TbValueOfScore.Focus();
  65. }
  66.  
  67.  
  68. if (DTrueOrFalse == false)
  69. {
  70. Numberofscores = Numberofscores + 1;
  71. LblNumberOfScores.Text = Numberofscores.ToString();
  72. StoreValueOfScores.Add(ValueScore);
  73. TbValueOfScore.Clear();
  74. TbValueOfScore.Focus();
  75.  
  76. for (int i = 0; i < StoreValueOfScores.Count; i++)
  77. {
  78. TotalOfScore = TotalOfScore + ValueScore;
  79. LblScoreTotal.Text = TotalOfScore.ToString();
  80. AverageScore = TotalOfScore / Numberofscores;
  81. LblAverageScore.Text = AverageScore.ToString();
  82. }
  83. }
  84. }
  85.  
  86. private void BtnExit_Click(object sender, EventArgs e)
  87. {
  88. Close();
  89. }
  90.  
  91. private void BtnClear_Click(object sender, EventArgs e)
  92. {
  93. for (i = 0; i < StoreValueOfScores.Count; i++)
  94. {
  95. StoreValueOfScores.Remove(ValueScore);
  96. LblNumberOfScores.Text = 0.ToString();
  97. LblAverageScore.Text = 0.ToString();
  98. TbValueOfScore.Focus();
  99. LblScoreTotal.Text = 0.ToString();
  100. Numberofscores = 0;
  101. TotalOfScore = 0;
  102. ValueScore = 0;
  103. }
  104. }
  105.  
  106. private void BtnDisplay_Click(object sender, EventArgs e)
  107. {
  108.  
  109. for (i = 0; i < StoreValueOfScores.Count; i++)
  110. {
  111.  
  112.  
  113. MessageBox.Show(StoreValueOfScores[i].ToString());
  114.  
  115. }
  116.  
  117. }
  118. }
  119. }
  120.  
  121. //Code Written By S Barratt
Last edited by brightsolar; 17 Days Ago at 6:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: MeSampath is an unknown quantity at this point 
Solved Threads: 6
MeSampath's Avatar
MeSampath MeSampath is offline Offline
Newbie Poster
 
1
  #2
16 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
0
  #3
16 Days Ago
Originally Posted by brightsolar View Post
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:

  1. namespace Exercise13
  2. {
  3. public partial class FrmCalculateAverageScore : Form
  4. {
  5. public FrmCalculateAverageScore()
  6. {
  7. InitializeComponent();
  8. this.button1.KeyPress+=new KeyPressEventHandler(button1_KeyPress);
  9. }

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.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 16
Reputation: brightsolar is an unknown quantity at this point 
Solved Threads: 0
brightsolar brightsolar is offline Offline
Newbie Poster
 
0
  #4
16 Days Ago
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.
-----------------------------------------------------------------------------------
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark
 
1
  #5
16 Days Ago
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.):

  1. ArrayList ary = new ArrayList();
  2. ary.Add("one");
  3. ary.Add("two");
  4. ary.Add("three");
  5. string s = string.Format("{0}\r\n{1}\r\n{2}\r\n", ary.ToArray());
  6. 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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 16
Reputation: brightsolar is an unknown quantity at this point 
Solved Threads: 0
brightsolar brightsolar is offline Offline
Newbie Poster
 
0
  #6
15 Days Ago
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.
-----------------------------------------------------------------
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark
 
1
  #7
15 Days Ago
Originally Posted by brightsolar View Post
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.

  1. string s2 = string.Empty;
  2. foreach (object oString in ary)
  3. s2 += (string)oString + "\r\n";
  4. MessageBox.Show("My ArrayList contains:\r\n\r\n" + s);
Last edited by DdoubleD; 15 Days Ago at 9:41 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
1
  #8
15 Days Ago
Originally Posted by DdoubleD View Post
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
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 16
Reputation: brightsolar is an unknown quantity at this point 
Solved Threads: 0
brightsolar brightsolar is offline Offline
Newbie Poster
 
0
  #9
14 Days Ago
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC