Question

Write a C# Windows Forms App code in Visual studio with a "start", "stop", "up", "down",...

Write a C# Windows Forms App code in Visual studio with a "start", "stop", "up", "down", "left" and "right" buttons and a textbox.

The textbox should start printing numbers from 1 to 100 on the press of the "start" button and stop printing process immediately on the press of "stop" button.

Also create a thread after stop button is pressed to allow manual input from  "up", "down", "left" and "right" buttons and print up, down, left or right in the same textbox.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//form1.cs

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

namespace PrintNumbers
{
public partial class Form1 : Form
{
static bool stop = false;
BackgroundWorker bgw;
String numbers = "";
public Form1()
{
InitializeComponent();
bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.WorkerReportsProgress = true;
bgw.WorkerSupportsCancellation = true;
}
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
stop = false;
PrintNumbers();
}
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
btnDown.Enabled = false;
btnLeft.Enabled = false;
btnRight.Enabled = false;
btnUp.Enabled = false;
NumTextbox.Refresh();
NumTextbox.Text = numbers;   
}


public void PrintNumbers()
{
int i = 1;
while(!stop)
{
numbers = numbers + (i).ToString();
bgw.ReportProgress(0);
Thread.Sleep(500);   
i++;
}
}

private void StopClk(object sender, EventArgs e)
{
stop = true;
btnDown.Enabled = true;
btnLeft.Enabled = true;
btnRight.Enabled = true;
btnUp.Enabled = true;
bgw.CancelAsync();
  
}

private void StartClk(object sender, EventArgs e)
{

bgw.RunWorkerAsync(); //call backgroundworker to handle task on click of start

}

private void btnUp_Click(object sender, EventArgs e)
{
NumTextbox.Text = btnUp.Text;

}

private void btnRight_Click(object sender, EventArgs e)
{
NumTextbox.Text = btnRight.Text;
}

private void btnLeft_Click(object sender, EventArgs e)
{
NumTextbox.Text = btnLeft.Text;
}

private void btnDown_Click(object sender, EventArgs e)
{
NumTextbox.Text = btnDown.Text;
}

}

}

//form1.designer.cs

namespace PrintNumbers
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

  
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.NumTextbox = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnRight = new System.Windows.Forms.Button();
this.btnLeft = new System.Windows.Forms.Button();
this.btnDown = new System.Windows.Forms.Button();
this.btnUp = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// NumTextbox
//
this.NumTextbox.Location = new System.Drawing.Point(12, 12);
this.NumTextbox.Multiline = true;
this.NumTextbox.Name = "NumTextbox";
this.NumTextbox.Size = new System.Drawing.Size(260, 83);
this.NumTextbox.TabIndex = 0;
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(36, 207);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 1;
this.btnStart.Text = "START";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.StartClk);
//
// btnRight
//
this.btnRight.Enabled = false;
this.btnRight.Location = new System.Drawing.Point(186, 138);
this.btnRight.Name = "btnRight";
this.btnRight.Size = new System.Drawing.Size(75, 23);
this.btnRight.TabIndex = 2;
this.btnRight.Text = "RIGHT";
this.btnRight.UseVisualStyleBackColor = true;
this.btnRight.Click += new System.EventHandler(this.btnRight_Click);
//
// btnLeft
//
this.btnLeft.Enabled = false;
this.btnLeft.Location = new System.Drawing.Point(24, 138);
this.btnLeft.Name = "btnLeft";
this.btnLeft.Size = new System.Drawing.Size(75, 23);
this.btnLeft.TabIndex = 3;
this.btnLeft.Text = "LEFT";
this.btnLeft.UseVisualStyleBackColor = true;
this.btnLeft.Click += new System.EventHandler(this.btnLeft_Click);
//
// btnDown
//
this.btnDown.Enabled = false;
this.btnDown.Location = new System.Drawing.Point(105, 167);
this.btnDown.Name = "btnDown";
this.btnDown.Size = new System.Drawing.Size(75, 23);
this.btnDown.TabIndex = 4;
this.btnDown.Text = "DOWN";
this.btnDown.UseVisualStyleBackColor = true;
this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
//
// btnUp
//
this.btnUp.Enabled = false;
this.btnUp.Location = new System.Drawing.Point(105, 120);
this.btnUp.Name = "btnUp";
this.btnUp.Size = new System.Drawing.Size(75, 23);
this.btnUp.TabIndex = 5;
this.btnUp.Text = "UP";
this.btnUp.UseVisualStyleBackColor = true;
this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(175, 207);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(75, 23);
this.btnStop.TabIndex = 6;
this.btnStop.Text = "STOP";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.StopClk);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnUp);
this.Controls.Add(this.btnDown);
this.Controls.Add(this.btnLeft);
this.Controls.Add(this.btnRight);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.NumTextbox);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox NumTextbox;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnRight;
private System.Windows.Forms.Button btnLeft;
private System.Windows.Forms.Button btnDown;
private System.Windows.Forms.Button btnUp;
private System.Windows.Forms.Button btnStop;
}
}

Add a comment
Know the answer?
Add Answer to:
Write a C# Windows Forms App code in Visual studio with a "start", "stop", "up", "down",...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please code the following Java App using Swing No additional classes are required. With that in...

    Please code the following Java App using Swing No additional classes are required. With that in mind ... Write a program that emulates a calculator. Create a Label with a title of "Super Calculator", a textbox, where the numbers are displayed, a series of buttons labeled as shown, and a “Clear” button. As the user enters the numbers, pressing the number buttons, display the numbers right justified to the textbox. When the user press any one of the operators, +,...

  • Programming question. Using Visual Studio 2019 C#. Windows Forms Application. Write the code that assigns the...

    Programming question. Using Visual Studio 2019 C#. Windows Forms Application. Write the code that assigns the number 15 to the reward variable when the state variable contains the string “IL” (entered using any case) and the sales variable contains a number that is greater than 2000; otherwise, assign the number 5.

  • PLEASE USE VISUAL BASIC* BY VISUAL STUDIO. Visual Basic INTERMEDIATE Create a Windows Forms application. Use...

    PLEASE USE VISUAL BASIC* BY VISUAL STUDIO. Visual Basic INTERMEDIATE Create a Windows Forms application. Use the following names for the project and solution, respectively: Chopkins Project and Chopkins Solution. Save the application in the VB2017\Chap03 folder. Change the form file's name to Main Form.vb. Change the form's name to frmMain. Create the interface shown in Figure 3-37. The interface contains six labels, three text boxes, and two buttons. The application calculates and displays the total number of packs ordered...

  • C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to...

    C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to create a Windows Form Application that will mimics a calculator. This is the screenshot how it shouldl look like. Calculator It should have a textbox and the following buttons: + Addition - Subtraction * Multiplication / Division = Equals (Will perform the final calculation) C Clear (Will clear the text box) There will be no maximize or minimize buttons. The rules are these: 2...

  • VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE &...

    VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE & ANSWER/FOLLOW ALL THE QUESTIONS IN THE ASSIGNMENT! . . Your program assignment Write a program name DeskGUI that computes the price of a desk and whose button Click Event calls the following methods: • A method to accept the number of drawers in the desk as input from the key board. This method returns the number of drawers to the SelectDesk_Click event. A method...

  • VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE &...

    VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE & ANSWER/FOLLOW ALL THE QUESTIONS IN THE ASSIGNMENT! . . Your program assignment Write a program name DeskGUI that computes the price of a desk and whose button Click Event calls the following methods: • A method to accept the number of drawers in the desk as input from the key board. This method returns the number of drawers to the SelectDesk_Click event. A method...

  • please do for C# in visual studio. For this exercise you are asked to write a...

    please do for C# in visual studio. For this exercise you are asked to write a Windows Forms application meeting the following specification. You may follow the worksheet for Practical 9 for step-by-step instructions on how to complete it The Form class must be named Form1. This is the default when creating a new Windows Forms app, but make sure that it doesn't change The form must be captioned Click Counter The form must contain a button with the text...

  • VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE &...

    VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE & ANSWER/FOLLOW ALL THE QUESTIONS IN THE ASSIGNMENT! Planet Info Help When planning this project, review the concept of accessing object properties on another form. Form2.IblMessage.Text = "1122.25" or you can put a string or object instead of numbers You can then create one form for the Planets information and set all the properties before you display the Planet Form For the pictures of the...

  • Visual Studio Code C# Well Document everyline saying what the code does. Include designer code and...

    Visual Studio Code C# Well Document everyline saying what the code does. Include designer code and .cscode Extra 6-1 Create a simple calculator In this exercise, you'l1 create a form that accepts two operands and an operator from the user and then performs the requested operation. Simple Calculator Operand 1: 86 Operator Operand 2 11.11 Resut 7.7408 1. Start a new project named SimpleCalculator in the Extra Exercises Chapter 06SimpleCalculator directory 2. Add labels, text boxes, and buttons to the...

  • Programming question. Using Visual Studio 2019 C#. Windows Forms Application. Write the code to display the message “Entry error” in the msgLabel when the value in the Integer units variable is less t...

    Programming question. Using Visual Studio 2019 C#. Windows Forms Application. Write the code to display the message “Entry error” in the msgLabel when the value in the Integer units variable is less than or equal to 0. Otherwise, calculate the total owed as follows: If the value stored in the units variable is less than 20, multiply the value by $10; otherwise, multiply it by $5. Store the total owed in the total variable.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT