Question

In: Computer Science

how to code in c# to get the total number of 1's as the numbers are...

how to code in c# to get the total number of 1's as the numbers are added in a Listbox in windows forms

Solutions

Expert Solution

Answer.

  1. namespace test   
  2. {   
  3. using System;   
  4. using System.Drawing;   
  5. using System.Collections;   
  6. using System.ComponentModel;   
  7. using System.Windows.Forms;   
  8. using System.Data;   
  9. public class ListBox : System.Windows.Forms.Form   
  10. {   
  11. private System.ComponentModel.Container container;   
  12. private System.Windows.Forms.Button buttonAdd;   
  13. private System.Windows.Forms.Button buttonClose;   
  14. private System.Windows.Forms.Button buttonModify;   
  15. private System.Windows.Forms.Button buttonDelete;   
  16. private System.Windows.Forms.Button buttonMoveUp;   
  17. private System.Windows.Forms.Button buttonMoveDown;   
  18. private System.Windows.Forms.ListBox listbox;   
  19. private System.Windows.Forms.TextBox textbox;   
  20. private System.Windows.Forms.Label label;   
  21. private int nSelectedIndex;   
  22. //*********SIZE & LOCATION******************//   
  23. // COMPONENT - BUTTON(s) aligned along X-axis.   
  24. const int BUTTON_LENGTH = 50;   
  25. const int BUTTON_HEIGHT = 20;   
  26. const int FIRSTBUTTON_XPOS = 20;   
  27. const int FIRSTBUTTON_YPOS =220;   
  28. const int XSPACING = 70; // (Note: XSPACING >= BUTTON_LENGTH)   
  29. const int YSPACING = 0;   
  30. //COMPONENT - MOVE BUTTONS   
  31. const int MBUTTON_LENGTH = 20;   
  32. const int MBUTTON_HEIGHT = 20;   
  33. const int FIRSTMBUTTON_XPOS = 220;   
  34. const int FIRSTMBUTTON_YPOS =70;   
  35. const int SECONDMBUTTON_XPOS = 220;   
  36. const int SECONDMBUTTON_YPOS =100;   
  37. // COMPONENT - LISTBOX   
  38. const int LISTBOX_LENGTH = 3*BUTTON_LENGTH;   
  39. const int LISTBOX_HEIGHT = 6*BUTTON_HEIGHT;   
  40. const int LISTBOX_XPOS = 50;   
  41. const int LISTBOX_YPOS = 50;   
  42. // COMPONENT - LABEL   
  43. const int LABEL_LENGTH = 50;   
  44. const int LABEL_HEIGHT = 50;   
  45. const int LABEL_XPOS = 20; // align it with first button   
  46. const int LABEL_YPOS = 173;   
  47. // COMPONENT - TEXTBOX   
  48. const int TEXTBOX_LENGTH = 120;   
  49. const int TEXTBOX_HEIGHT = 50;   
  50. const int TEXTBOX_XPOS = 70;   
  51. const int TEXTBOX_YPOS = 170;   
  52. public ListBox() : base()   
  53. {   
  54. InitializeComponent();   
  55. }   
  56. public override void Dispose()   
  57. {   
  58. base.Dispose();   
  59. container.Dispose();   
  60. }   
  61. private void InitializeComponent()   
  62. {
  63. // this   
  64. this.container = new System.ComponentModel.Container();   
  65. this.Text="List Box";   
  66. // buttonAdd   
  67. this.buttonAdd = new System.Windows.Forms.Button();   
  68. buttonAdd.Location = new System.Drawing.Point(FIRSTBUTTON_XPOS,FIRSTBUTTON_YPOS);   
  69. buttonAdd.Text = "&Add";   
  70. buttonAdd.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);   
  71. buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); buttonAdd.Enabled = false;   
  72. this.Controls.Add(this.buttonAdd);   
  73. //buttonModify   
  74. this.buttonModify = new System.Windows.Forms.Button();   
  75. buttonModify.Location = new System.Drawing.Point(FIRSTBUTTON_XPOS+XSPACING,FIRSTBUTTON_YPOS+YSPACING);   
  76. buttonModify.Text = "&Modify";   
  77. buttonModify.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);   
  78. buttonModify.Click += new System.EventHandler(this.buttonModify_Click); buttonModify.Enabled = false;   
  79. this.Controls.Add(this.buttonModify);   
  80. //buttonDelete   
  81. this.buttonDelete = new System.Windows.Forms.Button();   
  82. buttonDelete.Location = new System.Drawing.Point(FIRSTBUTTON_XPOS+2*XSPACING,FIRSTBUTTON_YPOS+2*YSPACING); buttonDelete.Text = "&Delete";   
  83. buttonDelete.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);   
  84. buttonDelete.Enabled = false;   
  85. buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);   
  86. this.Controls.Add(this.buttonDelete);   
  87. // buttonClose   
  88. this.buttonClose = new System.Windows.Forms.Button();   
  89. buttonClose.Location = new System.Drawing.Point(FIRSTBUTTON_XPOS+3*XSPACING,FIRSTBUTTON_YPOS+3*YSPACING); buttonClose.Text = "&Close";   
  90. buttonClose.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);   
  91. buttonClose.Click += new System.EventHandler(this.buttonClose_Click);   
  92. this.Controls.Add(this.buttonClose);   
  93. // listbox   
  94. this.listbox = new System.Windows.Forms.ListBox();   
  95. listbox.Location = new System.Drawing.Point(LISTBOX_XPOS,LISTBOX_YPOS);   
  96. listbox.Size = new System.Drawing.Size(LISTBOX_LENGTH,LISTBOX_HEIGHT);   
  97. listbox.Click += new System.EventHandler(this.listbox_SelectedIndexChanged);   
  98. listbox.BackColor = (Color)System.Drawing.SystemColors.Desktop;   
  99. this.Controls.Add(this.listbox);   
  100. // label   
  101. this.label = new System.Windows.Forms.Label();   
  102. label.Location = new System.Drawing.Point(LABEL_XPOS,LABEL_YPOS);   
  103. label.Size = new System.Drawing.Size(LABEL_LENGTH,LABEL_HEIGHT);   
  104. label.Text = "Enter:";   
  105. this.Controls.Add(this.label);   
  106. // textbox   
  107. this.textbox = new System.Windows.Forms.TextBox();   
  108. textbox.Location = new System.Drawing.Point(TEXTBOX_XPOS,TEXTBOX_YPOS); textbox.Click += new System.EventHandler(this.textbox_Click);   
  109. textbox.Size = new System.Drawing.Size(TEXTBOX_LENGTH,TEXTBOX_HEIGHT);   
  110. this.Controls.Add(this.textbox);   
  111. // buttonMoveUp   
  112. this.buttonMoveUp = new System.Windows.Forms.Button();   
  113. buttonMoveUp.Location = new System.Drawing.Point(FIRSTMBUTTON_XPOS,FIRSTMBUTTON_YPOS);   
  114. buttonMoveUp.Text = "<";   
  115. buttonMoveUp.Size = new System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);   
  116. buttonMoveUp.Click += new System.EventHandler(this.buttonMoveUp_Click);   
  117. buttonMoveUp.Enabled = false;   
  118. this.Controls.Add(this.buttonMoveUp);   
  119. // buttonMoveDown   
  120. this.buttonMoveDown = new System.Windows.Forms.Button();   
  121. buttonMoveDown.Location = new System.Drawing.Point(SECONDMBUTTON_XPOS,SECONDMBUTTON_YPOS);   
  122. buttonMoveDown.Text = ">";   
  123. buttonMoveDown.Size = new System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);   
  124. buttonMoveDown.Click += new System.EventHandler(this.buttonMoveDown_Click);   
  125. buttonMoveDown.Enabled = false;
  126. this.Controls.Add(this.buttonMoveDown);   
  127. }
  128. protected void textbox_Click(Object sender, System.EventArgs e)   
  129. {   
  130. this.buttonAdd.Enabled = true;   
  131. if (this.listbox.Items.Count>0)   
  132. EnableAllListBoxButtons();   
  133. }   
  134. protected void listbox_SelectedIndexChanged(object sender, System.EventArgs e)
  135. {
  136. nSelectedIndex = this.listbox.SelectedIndex;   
  137. string szSelected = (string)this.listbox.SelectedItem;   
  138. this.textbox.Text = szSelected;   
  139. }   
  140. protected void buttonAdd_Click(Object sender, System.EventArgs e)   
  141. {   
  142. if (this.textbox.Text !="")   
  143. {
  144. this.listbox.Items.Add(this.textbox.Text);   
  145. this.textbox.Text = "";   
  146. EnableAllListBoxButtons();   
  147. }   
  148. }   
  149. protected void buttonModify_Click(Object sender, System.EventArgs e)   
  150. {   
  151. this.listbox.Items[nSelectedIndex] = this.textbox.Text;   
  152. }   
  153. protected void buttonDelete_Click(Object sender, System.EventArgs e)   
  154. {   
  155. nSelectedIndex = this.listbox.SelectedIndex;   
  156. this.listbox.Items.Remove(nSelectedIndex);   
  157. System.Console.WriteLine("Remove fn does not work...");   
  158. }   
  159. protected void buttonClose_Click(Object sender, System.EventArgs e)   
  160. {   
  161. this.Close();   
  162. }   
  163. protected void buttonMoveUp_Click(Object sender, System.EventArgs e)   
  164. {   
  165. if (this.listbox.SelectedIndex >0)   
  166. this.listbox.SelectedIndex--;   
  167. }   
  168. protected void buttonMoveDown_Click(Object sender, System.EventArgs e)   
  169. {   
  170. if (this.listbox.SelectedIndex < this.listbox.Items.Count-1)   
  171. this.listbox.SelectedIndex++;   
  172. }   
  173. private void EnableAllListBoxButtons()   
  174. {
  175. this.buttonAdd.Enabled = true;   
  176. this.buttonModify.Enabled = true;   
  177. this.buttonDelete.Enabled = true;   
  178. this.buttonMoveUp.Enabled = true;   
  179. this.buttonMoveDown.Enabled = true;   
  180. }   
  181. [STAThread]   
  182. public static void Main(string[] args)   
  183. {   
  184. Application.Run(new ListBox());   
  185. }   
  186. } // class   
  187. } // namespace

Kindly upvote please,

Thank you.


Related Solutions

For a 3 digit code with distinct numbers. (0-9) How many combinations to get the code...
For a 3 digit code with distinct numbers. (0-9) How many combinations to get the code (max)? How many combinations if you remember the middle number is 1?
In C++ The existing code provides number of students who get A grade ( >=91 ),...
In C++ The existing code provides number of students who get A grade ( >=91 ), their average points. You may need to provide code for other students who got B, C, D and F grades with grade >= 81, 71, 61 and below 61 respectively. You are asked to write a print function to display / print name of group ( such as Grade A group) , number, and average grade. in the main function, you just make the...
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
please answer to Part 1 c please show your work how to get that number and...
please answer to Part 1 c please show your work how to get that number and no hand writing plz thanks Waterways Corporation is considering various business opportunities. It wants to make the best use of its production facilities to maximize income. This problem asks you to help Waterways do incremental analysis on these various opportunities. Part 1 Waterways mass-produces a special connector unit that it normally sells for $3.90. It sells approximately 35,000 of these units each year. The...
Fill in the blanks of this code to print out the numbers 1 through 7. number...
Fill in the blanks of this code to print out the numbers 1 through 7. number = 1 while number ___ 7:     print(number, end=" ")     ___
this is a python code: QUESTION 1: Input floating point numbers for a, b, c, d...
this is a python code: QUESTION 1: Input floating point numbers for a, b, c, d and e. calculate the following and display the result.  Mathematical expression ab means a * b in programming context. QUESTION 2: You are sleep expert for a baby that is having struggle sleeping every night. You are to ask the mother of the child "How many oz of milk the child drank ?" Based on the amount of milk, you will have to determine...
Revise this code to handle exceptions and provide correct total number of valid objects. (1) /**...
Revise this code to handle exceptions and provide correct total number of valid objects. (1) /** * class: CircleWithException * description: This program creates CircleWithException specification by using an instance variable/data/attribute named * radius. * Then, also we will create a static variable named numberOfObjects to count number of object when you create in a tester program. * In this program, you will learn how you declare an exception and define the exception. * This is a part of checked...
Revise this code to handle exceptions and provide correct total number of valid objects. (1) /**...
Revise this code to handle exceptions and provide correct total number of valid objects. (1) /** * class: CircleWithException * description: This program creates CircleWithException specification by using an instance variable/data/attribute named * radius. * Then, also we will create a static variable named numberOfObjects to count number of object when you create in a tester program. * In this program, you will learn how you declare an exception and define the exception. * This is a part of checked...
Revise this code to handle exceptions and provide correct total number of valid objects. (1) /**...
Revise this code to handle exceptions and provide correct total number of valid objects. (1) /** * class: CircleWithException * description: This program creates CircleWithException specification by using an instance variable/data/attribute named * radius. * Then, also we will create a static variable named numberOfObjects to count number of object when you create in a tester program. * In this program, you will learn how you declare an exception and define the exception. * This is a part of checked...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT