I want a Speedometer code using c# in which when user 1). hit the top arrow butt
ID: 3624004 • Letter: I
Question
I want a Speedometer code using c# in which when user1). hit the top arrow button the indicator should go high.
2). if user don't hit any arrow button the indicator should comes down slowly as normally we see in the games speedometer.
3). if user hit the down arrow button the indicator should comes down fast.
You should use c# windows forms to implement it and your code should run on visual studio 2008 express edition or 2010.
you could send me the project files at my email id: yola3467@hotmail.com
Thanks in advance.
Explanation / Answer
Hi yola, I just created a simple project with two buttons (gas/brake) and a label to display the current speed. First, I linked the label to the CurrentSpeed property. Then, the constants (maximum speed and rate of acceleration) needed to be specified. After that, I created a timer that would tick every 100 ms. Every time it ticked, it will then check to see if the gas is pressed, the brake is pressed, or no button is pressed at all. I used two control variables, GasEnabled and BrakeEnabled. Then I linked the MouseDown event and the MouseUp event to set the control variables to either true or false. Whenever the timer ticked, it will then check to see which control variable is active and apply some logic to increase or decrease the speed as well as some checks to make sure that it doesn't go below zero or above the maximum speed. The code is shown below. Please let me know if you have any questions. You can contact me through email at JayCee2358@gmail.com. using System; using System.Windows.Forms; namespace Speedometer { public class Form1 : Form { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.btnGas = new System.Windows.Forms.Button(); this.btnBrake = new System.Windows.Forms.Button(); this.lblSpeed = new System.Windows.Forms.Label(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); // // btnGas // this.btnGas.Location = new System.Drawing.Point(37, 73); this.btnGas.Name = "btnGas"; this.btnGas.Size = new System.Drawing.Size(75, 23); this.btnGas.TabIndex = 0; this.btnGas.Text = "Gas"; this.btnGas.UseVisualStyleBackColor = true; this.btnGas.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ControlButtonPressed); this.btnGas.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ControlButtonReleased); // // btnBrake // this.btnBrake.Location = new System.Drawing.Point(37, 129); this.btnBrake.Name = "btnBrake"; this.btnBrake.Size = new System.Drawing.Size(75, 23); this.btnBrake.TabIndex = 0; this.btnBrake.Text = "Brake"; this.btnBrake.UseVisualStyleBackColor = true; this.btnBrake.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ControlButtonPressed); this.btnBrake.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ControlButtonReleased); // // lblSpeed // this.lblSpeed.AutoSize = true; this.lblSpeed.Location = new System.Drawing.Point(34, 31); this.lblSpeed.Name = "lblSpeed"; this.lblSpeed.Size = new System.Drawing.Size(13, 13); this.lblSpeed.TabIndex = 1; this.lblSpeed.Text = "0"; // // timer1 // this.timer1.Enabled = true; this.timer1.Tick += new System.EventHandler(this.TimerTicked); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(201, 196); this.Controls.Add(this.lblSpeed); this.Controls.Add(this.btnBrake); this.Controls.Add(this.btnGas); this.Name = "Form1"; this.Text = "Speedometer"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnGas; private System.Windows.Forms.Button btnBrake; private System.Windows.Forms.Label lblSpeed; private System.Windows.Forms.Timer timer1; /// /// Integer value that the speedometer changes by. /// private const int ACCEL = 10; /// /// The maximum speed that the speedometer can display. /// private const int MAX_SPEED = 300; /// /// Flag that is true whenever the gas button is pressed down. /// private bool GasEnabled = false; /// /// Flag that is true whenever the brake button is pressed down. /// private bool BrakeEnabled = false; /// /// Property representing the current speed. /// private int CurrentSpeed { get { return Convert.ToInt32(lblSpeed.Text); } set { lblSpeed.Text = value.ToString(); } } /// /// Initializes the GUI. /// public Form1() { InitializeComponent(); } /// /// Event that occurs whenver the timer ticks. /// /// The timer object that triggered the event. /// Arguments associated with the event. private void TimerTicked(object sender, EventArgs e) { if (GasEnabled) { if (CurrentSpeed > MAX_SPEED) return; if (CurrentSpeed + ACCEL > MAX_SPEED) { CurrentSpeed = MAX_SPEED; return; } CurrentSpeed += ACCEL; } else if (BrakeEnabled) { if (CurrentSpeed < 0) return; if (CurrentSpeed - ACCEL < 0) { CurrentSpeed = 0; return; } CurrentSpeed -= ACCEL; } else if (CurrentSpeedRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.