C#: How to make an integer rise its value on button click? - c#

I am doing a simple clicker game, i know it is stupid to ask ,but i am still learning ,so my question is how to make the "cost2" integer rise his value +10 every button2 click.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Diamond_Clicker
{
public partial class Form1 : Form
{
private int clicks = 0;
private int counter = 1;
const double factor = 0.95;
double interval = 1000;
int cost = 50;
int cost2 = 500;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void UpdateButton()
{
if (clicks >= cost)
button1.Enabled = true;
else button1.Enabled = false;
}
private void UpdateButton2()
{
if (clicks >= cost2)
button2.Enabled = true;
else button2.Enabled = false;
}
private void myDiamond_MouseUp(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamond.png");
}
private void myDiamond_MouseDown(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamondMouseUp.png");
clicks++;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
UpdateButton();
UpdateButton2();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
counter++;
clicks = clicks + 1;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
UpdateButton();
}
private void button1_Click(object sender, EventArgs e)
{
clicks = clicks - cost;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
timer1.Enabled = true;
UpdateButton();
button1.Enabled = false;
interval *= factor;
timer1.Interval = (int)interval;
cost++;
label2.Text = "Cost: " + cost.ToString() + "$";
}
private void button2_Click(object sender, EventArgs e)
{
clicks = clicks - cost2;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
timer2.Enabled = true;
UpdateButton2();
button2.Enabled = false;
interval *= factor;
timer2.Interval = (int)interval;
cost2++;
label4.Text = "Cost: " + cost2.ToString() + "$";
}
private void timer2_Tick(object sender, EventArgs e)
{
counter++;
clicks = clicks + 10;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
UpdateButton2();
}
}
}

Add the following line in your button2_Click method
cost2 += 10;

You will simply change cost2++; to cost2 += 10;

Related

C# Windows form application InvalidCastException problem

This is a simple calculator program I am trying to make using Windows Forms Application in VS. The UnhandledException appears when I click anywhere except on the calculator buttons. I am fairly new to C# and it seems that a sender button in my function "common_operators" is causing the exception. Also, I want this calculator to have similar memory functionalities as windows 10's built-in calculator. I've searched everywhere but couldn't find a c# calculator implementation that is similar to Win10's built-in calculator has, I have already started but I think there's a better way to implement it. If u need more info, I've uploaded the "designer.cs" file that relates to the form application.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp_Calculator;
namespace WindowsFormsApp_Calculator
{
public partial class CalculatorBase : Form
{
public double[] arrMemory = new double[5];
public int indexer = 0;
double result = 0;
string asmd_operator = ""; // ASMD - Addition, Subtraction, Multiplication, Division
bool insert_value = false;
public CalculatorBase()
{
InitializeComponent();
btn_mc.Enabled = false;
btn_mr.Enabled = false;
mem_textbox.Text = "There's nothing saved in memory";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void numbers_zerotonine(object sender, EventArgs e)
{
Button b = (Button)sender;
if((box_display.Text == "0") || insert_value)
box_display.Text = "";
insert_value = false;
box_display.Text = box_display.Text + b.Text;
}
private void common_operators(object sender, EventArgs e)
{
Button b = (Button)sender;
if (result != 0)
{
btn_eql.PerformClick();
insert_value = true;
asmd_operator = b.Text;
subbox_display.Text = result + " " + asmd_operator;
}
else
{
asmd_operator = b.Text;
result = double.Parse(box_display.Text);
box_display.Text = "";
subbox_display.Text = System.Convert.ToString(result) + " " + asmd_operator;
}
}
private void btn_ce_Click(object sender, EventArgs e)
{
box_display.Text = "0";
}
private void btn_c_Click(object sender, EventArgs e)
{
box_display.Text = "0";
subbox_display.Text = "";
result = 0;
}
private void btn_eql_Click(object sender, EventArgs e)
{
subbox_display.Text = "";
switch(asmd_operator)
{
case "-":
box_display.Text = (result - double.Parse(box_display.Text)).ToString();
break;
case "+":
box_display.Text = (result + double.Parse(box_display.Text)).ToString();
break;
case "X":
box_display.Text = (result * double.Parse(box_display.Text)).ToString();
break;
case "/":
box_display.Text = (result / double.Parse(box_display.Text)).ToString();
break;
default:
break;
}
result = double.Parse(box_display.Text);
asmd_operator = "";
}
private void btn_bs_Click(object sender, EventArgs e)
{
if(box_display.Text.Length > 0)
{
box_display.Text = box_display.Text.Remove(box_display.Text.Length - 1, 1);
}
if(box_display.Text == "")
{
box_display.Text = "0";
}
}
private void btn_ms_Click(object sender, EventArgs e)
{
if (indexer == 5)
{
mem_textbox.Text = "Memory is full. Max limit = 5";
}
else
{
int hgt = 75;
mem_textbox.Text = "";
arrMemory[indexer] = double.Parse(box_display.Text);
indexer++;
btn_mc.Enabled = true;
btn_mr.Enabled = true;
TextBox mem = new TextBox();
mem.Multiline = true;
mem.TextAlign = HorizontalAlignment.Right;
mem.Width = 275;
mem.Height = 70;
mem.Font = new Font(mem.Font.FontFamily, 20);
mem.Text = box_display.Text;
mem.Location = new Point(387, hgt);
this.Controls.Add(mem);
}
}
private void btn_mc_Click(object sender, EventArgs e)
{
foreach (int i in arrMemory)
{
arrMemory[i] = 0;
}
indexer = 0;
btn_mr.Enabled = false;
btn_mc.Enabled = false;
mem_textbox.Text = "There's nothing saved in memory";
}
private void btn_mr_Click(object sender, EventArgs e)
{
box_display.Text = arrMemory[indexer].ToString();
}
private void btn_mp_Click(object sender, EventArgs e)
{
arrMemory[indexer] += double.Parse(box_display.Text);
}
private void btn_mm_Click(object sender, EventArgs e)
{
arrMemory[indexer] -= double.Parse(box_display.Text);
}
}
}
From your designer.cs you've got a Click event handler on the form itself that invokes common_operators, so if that gets fired, it will be an invalid cast since sender will be your CalculatorBase form type and not Button

facing problem in c# calculator operator pressed it shows calculates result while pressing another operator

I am new to c# programming.I am building a simple calculator but when i add two numbers and press operator(+) button it adds two numbers but when i again press another operator it just adds the previous value like 2+2= 4 and if i press (-) operator after that it just shows result = 8 , please help me to get out of this.
thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
double value = 0;
string operation = "";
bool opt_pressed = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if ((txtMain.Text == "0") || (opt_pressed))
txtMain.Clear();
Button a = (Button)sender;
opt_pressed = false;
if (a.Text == ".")
{
if (!txtMain.Text.Contains("."))
txtMain.Text = txtMain.Text + a.Text;
}
else
{
txtMain.Text = txtMain.Text + a.Text;
}
}
private void txtMain_TextChanged(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
txtMain.Clear();
value = 0;
label1.Text = "";
}
private void button11_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
if (value != 0)
{
equal.PerformClick();
operation = b.Text;
label1.Text = value + " " + operation;
opt_pressed = true;
}
else
{ operation = b.Text;
value = double.Parse(txtMain.Text);
label1.Text = value + " " + operation;
opt_pressed = true;
}
}
private void button16_Click(object sender, EventArgs e)
{
switch (operation)
{
case "+":
txtMain.Text = (value + double.Parse(txtMain.Text)).ToString();
break;
case "-":
txtMain.Text = (value - double.Parse(txtMain.Text)).ToString();
break;
case "/":
txtMain.Text = (value / double.Parse(txtMain.Text)).ToString();
break;
case "*":
txtMain.Text = (value * double.Parse(txtMain.Text)).ToString();
break;
}
value = double.Parse(txtMain.Text);
label1.Text = "";
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, KeyPressEventArgs e)
{
Button a = (Button)sender;
button1.PerformClick();
operation = a.Text;
}
}
}

C# Aforge MJPEG stream motiondetection

I am still new at programming, I code as a hobby :)
I wanted to make a MJPEG streaming aplication with an option to record a video and save it on computer when it detects a moving. So far i've managed to get stream to videosourceplayer in WPF, but it seems that i can't manage to get the information whether moving is detected or not. What am i doing wrong? i've tried many things ( commented ). but it seems that i can't compare float MotionDetected to limited (in my case 0.02). my label5 simply doesnt change.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Vision.Motion;
using AForge.Video;
using AForge.Video.DirectShow;
using Accord.Video.FFMPEG;
using System.Globalization;
using AForge.Controls;
namespace AnotherAForge
{
public partial class Form1 : Form
{
MotionDetector Detector;
float f;
float MotionDetected;
int frames;
private VideoFileWriter _writer;
private MJPEGStream stream = new MJPEGStream("link to the cam i can't share");
int uporabimotiondetection = 0;
private IVideoSource _videoSource;
public Form1()
{
InitializeComponent();
}
Form form1 = new Form();
public void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
if (uporabimotiondetection == 1)
{
MotionDetected = Detector.ProcessFrame(image);
//if (label5.InvokeRequired) {
// label5.Text = NivelDeDeteccion.ToString();
//}
//if (Detector.ProcessFrame(image) > 0.02)
//{
// label5.Text = "1";
//}
}
frames++;
}
private void alarm() {
label2.Text = MotionDetected.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
//VideoStream.NewFrame += VideoStream_NewFrame;
//VideoStream.NewFrame += new NewFrameEventHandler(ProcessNewFrame);
stream.Start();
//stream.NewFrame += Stream_NewFrame;
//videoSourcePlayer1.NewFrame += videoSourcePlayer1_NewFrame;
videoSourcePlayer1.VideoSource = stream;
timer1.Enabled = true;
timer1.Start();
timer2.Start();
//pictureBox1.Image = image;
GC.Collect();
}
private void Form1_Load(object sender, EventArgs e)
{
Detector = new MotionDetector(new TwoFramesDifferenceDetector(),new MotionAreaHighlighting());
MotionDetected = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
// textBox1.Text = NivelDeDeteccion.ToString();
label3.Text = frames.ToString() + " frames";
label2.Text = MotionDetected.ToString();
if (MotionDetected <= 0.02 && MotionDetected >= 0)
{
label5.Text = "1";
}
else {
label5.Text = "0";
}
}
private void button2_Click(object sender, EventArgs e)
{
Console.Beep();
}
int time;
private void timer2_Tick(object sender, EventArgs e)
{
time++;
label4.Text = time.ToString();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
stream.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
stream.Stop();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
uporabimotiondetection = 1;
}
else {
uporabimotiondetection = 0;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//textBox1.Text = string.Format("{0:0.00000}", textBox1.Text);
//float motionlevel = float.Parse(MotionDetected);
//float a = 1;
//textBox2.Text = a.ToString();
//float b = MotionDetected / 100;
//textBox1.Text = b.ToString();
//double limiter = Convert.ToDouble("0,02");
//textBox2.Text = limiter.ToString();
//if (MotionDetected < (0.02f / 1000))
//{
// label5.Text = "gibanje";
//}
//else
//{
// label5.Text = "ni gibanja!";
//}
}
}
}

C# Windows Forms Application | Cookie Clicker

Im trying to make a cookie maker!
it works great and all but every time i click on the cookie, it takes it about 2 clicks to add just one to the cookie count. I want to make the counting system go faster, since if i click 2 times a second, it will only count as one atm...
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace FormalCookie
{
public partial class Form1 : Form
{
int cnum = 0;
int add = 1;
public Form1()
{
InitializeComponent();
label1.Text = ("Price For Mouse:" + " 50");
textBox1.Text = ("Cookies:" + " " + cnum);
mouse.Enabled = false;
for (int i = 0; i< add; i++)
{
if (add > 1 && add % 2 == 0)
{
cnum += 1 * add;
}
Thread.Sleep(1000);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Mouse
if (cnum >= add * add * 50)
{
cnum -= (add * 100);
add+=2;
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (cnum >= (add * add * 50))
{
mouse.Enabled = true;
}
else
{
mouse.Enabled = false;
}
cnum += (add * add);
textBox1.Text = ("Cookies:" + " " + cnum);
label1.Text = ("Price For Mouse:" + " " + (add * add * 50));
}
private void label1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, MouseEventArgs e)
{
}
}
}

Windows.Forms.Timer not working properly

I am posting a sample code to decode a problem with a larger code.
I have a 2 seconds timer - System.Windows.Forms.Timer
Every 2 seconds i increment a global int by one, and show its value using MessageBox
If the int reaches 4, i turn off the main flag.
Here is the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
bool Play_On = false;
int i = 0;
public Form1()
{
InitializeComponent();
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Play")
{
button1.Text = "Puase";
Play_On = true;
}
else
{
button1.Text = "Play";
Play_On = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
Timer MinResTimer = new Timer();
{
MinResTimer.Tick += new EventHandler(MinResTimer_Elapsed);
MinResTimer.Interval = 2000;
MinResTimer.Enabled = true;
MinResTimer.Start();
}
}
public void MinResTimer_Elapsed(object sender, EventArgs e)
{
if (Play_On == true)
{
MessageBox.Show("timed"+ i.ToString());
i++;
}
if (i == 4)
{
Play_On = false;
button1.Text = "Play";
}
}
}
}
The problem is that the flag isnt being turned off at all.
The output I am getting is Timed0 - In a messagebox, many times.
I think I have some problem with the messagebox - not sure
Could someone help me find out whats going on here?
Move your increment of i++ to before you call MessageBox
Then you will see that you really want to protect the event from still firing while the message box is displayed.
so your new code would move the I++ to before the MessageBox and then you would disable and enable the timer before and after the message box is called.
When the messagebox show,then you didn't click the 'OK' button to close the messagebox, i++ can not do.So i always equals 0 before you click the 'OK' button on messagebox.
void MinResTimer_Elapsed(object sender, EventArgs e)
{
if (Play_On == true)
{
i++;
MessageBox.Show("timed" + i.ToString());
}
if (i == 4)
{
Play_On = false;
MinResTimer.Enabled = false;
button1.Text = "Pause";
}
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Play")
{
button1.Text = "Puase";
MinResTimer.Enabled = true;
i = 0;
Play_On = true;
}
else
{
button1.Text = "Play";
MinResTimer.Enabled = false;
Play_On = false;
}
}
EDIT #1:
bool Play_On = false;
int i = 0;
Timer MinResTimer;
private void Form1_Load(object sender, EventArgs e)
{
MinResTimer = new Timer();
{
MinResTimer.Tick += new EventHandler(MinResTimer_Elapsed);
MinResTimer.Interval = 2000;
MinResTimer.Enabled = true;
MinResTimer.Start();
}
}
void MinResTimer_Elapsed(object sender, EventArgs e)
{
if (Play_On == true && i <= 4)
{
i++;
MessageBox.Show("timed" + i.ToString());
}
if (i == 4)
{
Play_On = false;
button1.Text = "Pause";
}
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Play")
{
button1.Text = "Puase";
Play_On = true;
}
else
{
button1.Text = "Play";
Play_On = false;
}
}

Categories