Create a C# app to do just that! Use a loop to display all positive integers up to 100 in a listbox whenyou enter a “Count by” number (e.g., 2, 3, 5, 10, etc.) in a textbox and click the Calculate button orpress the Enter key. Think carefully about how to initialize and increment your counter variable in yourloop. Remember, you can set a breakpoint (red dot) in your code to help debug it, if needed.Additionally, display the sum of all of the listbox values in a label. For example, if your app counts by10’s, the sum should be 550 (Figure 1). Declare and initialize a sum variable to zero, since it will serveas an accumulator variable. Add this logic to the code for the loop already in the Calculate button eventhandler. Clear the listbox before entering the loop
Question
Create a C# app to do just that! Use a loop to display all positive integers up to 100 in a listbox whenyou enter a “Count by” number (e.g., 2, 3, 5, 10, etc.) in a textbox and click the Calculate button orpress the Enter key. Think carefully about how to initialize and increment your counter variable in yourloop. Remember, you can set a breakpoint (red dot) in your code to help debug it, if needed.Additionally, display the sum of all of the listbox values in a label. For example, if your app counts by10’s, the sum should be 550 (Figure 1). Declare and initialize a sum variable to zero, since it will serveas an accumulator variable. Add this logic to the code for the loop already in the Calculate button eventhandler. Clear the listbox before entering the loop
Solution
Here is a simple C# application that does what you asked for. This application uses Windows Forms.
using System;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
int countBy = int.Parse(txtCountBy.Text);
int sum = 0;
for (int i = countBy; i <= 100; i += countBy)
{
listBox1.Items.Add(i);
sum += i;
}
lblSum.Text = "Sum: " + sum.ToString();
}
private void txtCountBy_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnCalculate_Click(this, new EventArgs());
}
}
}
In this code, btnCalculate_Click is the event handler for the Calculate button click and txtCountBy_KeyDown is the event handler for the Enter key press in the textbox. The loop in btnCalculate_Click goes from the count by number to 100, incrementing by the count by number each time. It adds each number to the listbox and adds it to the sum. After the loop, it displays the sum in a label. The txtCountBy_KeyDown method triggers the Calculate button click event when the Enter key is pressed in the textbox.
Similar Questions
what will be the output of the c program?
What will be the output of the following C program?#include<stdio.h>int main(){char c;c = 'A';printf("%c",c);return 0;}Select one:65Aac
Structure of a C Program:
What will be the output of the following C code?
Which of the following is an invalid identifier in C?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.