In button16_Click meethod. Where I add value = Int32.Parse(result.Text); that should actually be: value = Double.Parse(result.Text); If we use Int32, we'll truncate our decimal.
Hello Chris, I'm very new to C# so I want to start with simple things, and I would like to know, I have my form set up as so " s9.postimg.org/42gdsnor3/Untitled.png " if I add a timer how can I make it so I enter the numerator and the denominator into the numericUpDown control, then the "Ratio" label automatically shows the output of the two numbers divided? Thanks.
Chris Merritt Yes that answers it perfectly. Thank you. I also noticed you said in the video, that the number could be shrunk. So instead of 10.55555 or 2.555555 just 2.55'5 and 10.55'5 is this just an easy change? How would I go about that, but I'm not too bothered on that. Here is my code as follows: pastebin.com/gW1i2NFf
john20x You can either truncate or round. If you want to truncate, check this out: msdn.microsoft.com/en-us/library/7d101hyf%28v=vs.110%29.aspx. If you want to round, check this out: stackoverflow.com/questions/257005/how-do-you-round-a-number-to-two-decimal-places-in-c.
@Chris Merritt There is another way to fix the size of the windows: under appearance in the proprieties menu there is an option called FormBorderStyle you can just set it to fixed...
Hey it's really a great video, i've finished my calculator like you did with some extra function. But, is there any possibility to perform a calculation with precedence (order of operation) ? If yes, would u tell me how to do that, or what code should i change or do i need to figure out new method and start all over again? thanks!
I have a solution in enter keypress private void Form1_KeyPress(object sender, KeyPressEventArgs e) { equal.Focus(); switch (e.KeyChar.ToString()) { so when you press any key it will focus on equal button automatically.
[Issue]Tks for your support! But now , I have "two" special keys to add in the switch like enter and backspace ,it'll focus on the backspace keypress ,instead of the enter keypress. But fortunately, the keypress of numbers and operation are without any problems. Here is my code: user-images.githubusercontent.com/62552984/106383303-1df05780-6400-11eb-96dc-c10eefc37370.png and how to fix?tks
Hello Cris! I've a problem with the decimal operations. For example, i need do 3.3 + 1.7, and the correct result is 5.0, but in my case, the result is 50. i've problemas with the ".". So, i don't find the error in the code. Do you know someting about this? Thanks Man.
Is there a method that checks for local decimal separator and allows storing it in string variable? This calculator for example won't work on systems on which decimal separator is comma.
While trying to add key press functionality I keep getting this error "Error CS1061 'Button' does not contain a definition for 'PreformClick' and no extension method 'PreformClick' accepting a first argument of type 'Button' could be found (are you missing a using directive or an assembly reference?)"
Hi Chris.. ..can I ask about how would you determine the initial value if you just only press one number and then you press any operator sign for the first time?
after creating a new project , there found that only code window not show designer form, again pressing shift + F7 it show only code not show the form where we add button level or textbox etc, where is wrong I don"t catch it, plz reply my comment
I figured out the code to use enter key for equals but I'm not sure how to implement it. Thanks again for your help on my other error I was having btw. [CODE] if (e.KeyChar == Convert.ToChar(Keys.Return)) { equals.PerformClick(); } [/CODE] I have added it to the code but all it does is add a zero right now. (Keys.Return) is also known as ((char) 13), ascii key 13 from DOS days. Would I put this code before the switch event as an if-else?
Hi chris Good job with the app ,but the are some things missing ,that i found like what happens when the user input a decimal with zero like 0,1 or 0.5 etc, zero vanishes buy you can se the other half like ( ,5 ) and the result is right though.But zero vanishes always,how do you fix that? And what happens if a number is divided by zero? thanks.
I found the Same problem. The easiest way o fix it is to put diferent event for decimal point button. the problem is in the code of Button_Click event where it deletes 0 every time when you start typeing. Ad a new, diferente mevent to the decimal point button and write this: if (!Result.Text.Contains(",")) { Result.Text = Result.Text + "," ; } after this you can delete the IF part in the Button_Click event that adreses the decimal point since the decimal point button is not trigered with the Buton_Click event P.S. Sorry for bad English, not my native language...
I'm stuck. How do I clear the text box after i click the equal button ? Let's say I get my result. Instead of C or CE, how can I press a number button to both clear the textbox and assign the value ?
Okay so I'm a little confused on how this works, the lines of code: if (b.Text == ".") { if (!result.Text.Contains(".")) { result.Text = result.Text = result.Text + b.Text; } }else { result.Text = result.Text = result.Text + b.Text; } Shouldn't it still add a decimal? I mean you're basically saying if result.Text doesn't contain a decimal, add a decimal, otherwise, add a decimal.
Yeah I know, its maybe a bit too late for me to answer your question, but in case you still wondering why, i try to explain it. The "else" is an else to the (outer) "if (b.Text == ".")". So what it does is, at first it checks if the transmitted parameter b contains a "." - if not, then just add the value of the transmitted parameter (which is a number from 0 to 9) to the result-String. But if the transmitted parameter is a ".", then check, if there is NOT already a "." within the result-String. If that is the case then just add the "." to the string. If it DOES contain an "." then (because there is no else for the inner if) just go on.
Have an error here . The point or comma operator does not work . When I enter , for example, 1.5 , the number appears correctly in the text box , however, to push out an operation appears not 1.5 in label but instead desses 15. I just do not find the error , despite which I probably have the same code . I ask for help. Regards Dominik 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 Calculator { public partial class Form1 : Form { Double value = 0; String operation = ""; bool operation_pressed = false; public Form1() { InitializeComponent(); } private void btn_Click(object sender, EventArgs e) { if ((result.Text == "0") || (operation_pressed)) result.Clear(); operation_pressed = false; Button b = (Button)sender; if (b.Text == ".") { if (!result.Text.Contains(".")) result.Text = result.Text + b.Text; } else result.Text = result.Text + b.Text; } private void btnCE_Click(object sender, EventArgs e) { result.Text = "0"; } private void operator_click(object sender, EventArgs e) { Button b = (Button)sender; operation = b.Text; value = Double.Parse(result.Text); operation_pressed = true; equation.Text = value + " " + operation; } private void btnGleich_Click(object sender, EventArgs e) { equation.Text = ""; switch (operation) { case "+": result.Text = (value + Double.Parse(result.Text)).ToString(); break; case "-": result.Text = (value - Double.Parse(result.Text)).ToString(); break; case "*": result.Text = (value * Double.Parse(result.Text)).ToString(); break; case "/": result.Text = (value / Double.Parse(result.Text)).ToString(); break; default: break; }//end switch } private void btnC_Click(object sender, EventArgs e) { result.Text = "0"; value = 0; }
Have found the error . The fault is in this case that I develop in Germany . With German Windows ! Therefore one has to rely on point the dot operator ( number separator ) . As the text is read property from the button , you have to set these also point to point . Thus, it works with the German development environment . Otherwise, as in the video ! Here the corrections in the code: private void btn_Click(object sender, EventArgs e) { if ((result.Text == "0") || (operation_pressed)) result.Clear(); operation_pressed = false; Button b = (Button)sender; if (b.Text == ",") { if (!result.Text.Contains(",")) result.Text = result.Text + b.Text; } else result.Text = result.Text + b.Text; }
FormBorderStyle = Fixed3D Is an easier way to fix the size of the window to the size you create it at. This is in WVS 2015 on windows 10 so it may be different.
case "/": if (result.Text == "0") //Division by ZERO = Message error = MessageBox.Show("Pamietaj cholero nie dziel przez ZERO !"); else result.Text = (value / Double.Parse(result.Text)).ToString(); break;
I enjoyed to make calculator but there are some error. for example if we add two values 3 + 4 = 7 the result 7 and you type any number it will continue with 7. i.e. 723232 like this
I think that there are 2 problems: 1. In the windows calculator for example if you do 7+2 and then press= it 9 AND then if press again = it 11 and so on 13,15,17. In this calc it doesn't do it. 2. also in the windows calculator for example if you do 3+2 and then press= it 5 and you can do anther calculation but in this calculator after you press = you can't click on the numbers because the numbers will be added to the number that is already displayed. Can Please fix it? all in all you do great videos and please keep it like this!!!!!
Chris Merritt It's strange, but i see only 360p quality for this video funkyimg.com/i/HDEa.jpg Part 1 and Part 3 are good (720p). Anyway awesome videos and best calculator tutorial.
To add Enter for equals. Just put the Tabstop property to false on all the buttons exept equals. Even the textbox.
Thank you very much man! That helped out wonders. What MudHoleCreation said fixes the Enter!
Thank you so much. :)
Hi, Do u know how to display the equation textbox to fill equation instead of 2 oprehend and 1 operator ?
@MudholeCreation do uk how to keypress backspace button and display the full equation in the equation textbox eg 1+2+3+4 ?
@@bernardopower5267 Sorry, but no, I probably did at the time. But this was 8 years ago.
Hello, Chris. Your lessons are very good. Thanks for the new knowledge.
In button16_Click meethod. Where I add
value = Int32.Parse(result.Text);
that should actually be:
value = Double.Parse(result.Text);
If we use Int32, we'll truncate our decimal.
Hello Chris,
I'm very new to C# so I want to start with simple things, and I would like to know, I have my form set up as so " s9.postimg.org/42gdsnor3/Untitled.png " if I add a timer how can I make it so I enter the numerator and the denominator into the numericUpDown control, then the "Ratio" label automatically shows the output of the two numbers divided? Thanks.
Check out this video I just posted. Think it covers what you're looking for: ruclips.net/video/hrWbqJKRMS4/видео.html
Chris Merritt Yes that answers it perfectly. Thank you. I also noticed you said in the video, that the number could be shrunk. So instead of 10.55555 or 2.555555 just 2.55'5 and 10.55'5 is this just an easy change? How would I go about that, but I'm not too bothered on that. Here is my code as follows: pastebin.com/gW1i2NFf
john20x
You can either truncate or round. If you want to truncate, check this out: msdn.microsoft.com/en-us/library/7d101hyf%28v=vs.110%29.aspx. If you want to round, check this out: stackoverflow.com/questions/257005/how-do-you-round-a-number-to-two-decimal-places-in-c.
my calculator is showing 1 2tmes and same for all other numerical buttons
@Chris Merritt
There is another way to fix the size of the windows:
under appearance in the proprieties menu there is an option called FormBorderStyle you can just set it to fixed...
Hey it's really a great video, i've finished my calculator like you did with some extra function. But, is there any possibility to perform a calculation with precedence (order of operation) ? If yes, would u tell me how to do that, or what code should i change or do i need to figure out new method and start all over again? thanks!
I have a solution in enter keypress
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
equal.Focus();
switch (e.KeyChar.ToString())
{
so when you press any key it will focus on equal button automatically.
[Issue]Tks for your support! But now , I have "two" special keys to add in the switch like enter and backspace ,it'll focus on the backspace keypress ,instead of the enter keypress. But fortunately, the keypress of numbers and operation are without any problems. Here is my code:
user-images.githubusercontent.com/62552984/106383303-1df05780-6400-11eb-96dc-c10eefc37370.png
and how to fix?tks
Good tutorial this gave me the basis to build a neat little scientific calculator
Thank you, part 2 answered my question about coding the decimal point. Thanks.
Hi, Do u know how to display the equation textbox to fill equation instead of 2 oprehend and 1 operator ?
Great job! Thank you for sharing!
Hello Cris!
I've a problem with the decimal operations.
For example, i need do 3.3 + 1.7, and the correct result is 5.0, but in my case, the result is 50. i've problemas with the ".".
So, i don't find the error in the code. Do you know someting about this?
Thanks Man.
try to use , instead of . as your Button
i came to 4:25 and i cant do anythin. It crashes when i type something like "3.3" i add "+" and it goes like BOOOOM! XD
help me!
you need to add a tag name
So how do I handle keypresses like ENTER, BACKSPACE, and DELETE?
Is there a method that checks for local decimal separator and allows storing it in string variable? This calculator for example won't work on systems on which decimal separator is comma.
While trying to add key press functionality I keep getting this error
"Error CS1061 'Button' does not contain a definition for 'PreformClick' and no extension method 'PreformClick' accepting a first argument of type 'Button' could be found (are you missing a using directive or an assembly reference?)"
Maybe your spelling "Preform" wrong? It's supposed to be "Perform".
if you have problem with decimal number, i think you must change "." with "," in the text button '.'
You are wrong, it isn't working for me...
THANK YOU SO MUCH MAN
yeah tried that too and it worked
That fixed it. Thank you.
Hi Chris.. ..can I ask about how would you determine the initial value if you just only press one number and then you press any operator sign for the first time?
Hi, how do u key press backspace ?
When I press "+" button between double values, I get error in this line:
value = Double.Parse(result.Text);
please help!
Oh, i understood. No problem. CultureInfo helped.
+Евгений Ананченко help me!!! i have the same problem but i cant solve it!
i have the same problem
Use this to make the return key work in the calculator:
case "
":
equal.PerformClick();
break;
after creating a new project , there found that only code window not show designer form, again pressing shift + F7 it show only code not show the form where we add button level or textbox etc, where is wrong I don"t catch it, plz reply my comment
Why does a line such as this appear when the text edit line appear when entering numbers/ operators ?
I figured out the code to use enter key for equals but I'm not sure how to implement it. Thanks again for your help on my other error I was having btw.
[CODE]
if (e.KeyChar == Convert.ToChar(Keys.Return))
{
equals.PerformClick();
}
[/CODE]
I have added it to the code but all it does is add a zero right now.
(Keys.Return) is also known as ((char) 13), ascii key 13 from DOS days.
Would I put this code before the switch event as an if-else?
Use the form properties window, in there is a property that says, "Accept Button" use the drop down to find the button you want to use.
Okay, you're going to have to show me on Sunday over hangouts.
For some reasons , I can't do a Double.Parse(TEXTBOX.TEXT); I do get an error ... I have no idea why
maybe the textbox name will be wrong
How can you make the result add comas to separate numbers in thousands and millions ?
Hi chris
Good job with the app ,but the are some things missing ,that i found like what happens when the user input a decimal with zero like 0,1 or 0.5 etc, zero vanishes buy you can se the other half like ( ,5 ) and the result is right though.But zero vanishes always,how do you fix that?
And what happens if a number is divided by zero?
thanks.
I found the Same problem. The easiest way o fix it is to put diferent event for decimal point button. the problem is in the code of Button_Click event where it deletes 0 every time when you start typeing. Ad a new, diferente mevent to the decimal point button and write this:
if (!Result.Text.Contains(","))
{
Result.Text = Result.Text + "," ;
}
after this you can delete the IF part in the Button_Click event that adreses the decimal point since the decimal point button is not trigered with the Buton_Click event
P.S. Sorry for bad English, not my native language...
Thank you for this video!
I'm stuck. How do I clear the text box after i click the equal button ? Let's say I get my result. Instead of C or CE, how can I press a number button to both clear the textbox and assign the value ?
Mojomatrix just add result.Text = ""; in your event handler when you want the text box to be cleared.
13:36 thank you so much!!!
Okay so I'm a little confused on how this works, the lines of code:
if (b.Text == ".") {
if (!result.Text.Contains(".")) {
result.Text = result.Text = result.Text + b.Text;
}
}else {
result.Text = result.Text = result.Text + b.Text;
}
Shouldn't it still add a decimal? I mean you're basically saying if result.Text doesn't contain a decimal, add a decimal, otherwise, add a decimal.
Yeah I know, its maybe a bit too late for me to answer your question, but in case you still wondering why, i try to explain it.
The "else" is an else to the (outer) "if (b.Text == ".")".
So what it does is, at first it checks if the transmitted parameter b contains a "." - if not, then just add the value of the transmitted parameter (which is a number from 0 to 9) to the result-String.
But if the transmitted parameter is a ".", then check, if there is NOT already a "." within the result-String. If that is the case then just add the "." to the string.
If it DOES contain an "." then (because there is no else for the inner if) just go on.
there is a problem when I try to div ("/") lower digit with bigger one. Example 5/10 and it appears a dialog window with errors
works for me. Maybe you have a Mistake somewhere
Nice video!! :D
Thanks!
Have an error here .
The point or comma operator does not work .
When I enter , for example, 1.5 , the number appears correctly in the text box ,
however, to push out an operation appears not 1.5 in label but instead desses 15.
I just do not find the error , despite which I probably have the same code .
I ask for help. Regards Dominik
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 Calculator
{
public partial class Form1 : Form
{
Double value = 0;
String operation = "";
bool operation_pressed = false;
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if ((result.Text == "0") || (operation_pressed))
result.Clear();
operation_pressed = false;
Button b = (Button)sender;
if (b.Text == ".")
{
if (!result.Text.Contains("."))
result.Text = result.Text + b.Text;
}
else
result.Text = result.Text + b.Text;
}
private void btnCE_Click(object sender, EventArgs e)
{
result.Text = "0";
}
private void operator_click(object sender, EventArgs e)
{
Button b = (Button)sender;
operation = b.Text;
value = Double.Parse(result.Text);
operation_pressed = true;
equation.Text = value + " " + operation;
}
private void btnGleich_Click(object sender, EventArgs e)
{
equation.Text = "";
switch (operation)
{
case "+":
result.Text = (value + Double.Parse(result.Text)).ToString();
break;
case "-":
result.Text = (value - Double.Parse(result.Text)).ToString();
break;
case "*":
result.Text = (value * Double.Parse(result.Text)).ToString();
break;
case "/":
result.Text = (value / Double.Parse(result.Text)).ToString();
break;
default:
break;
}//end switch
}
private void btnC_Click(object sender, EventArgs e)
{
result.Text = "0";
value = 0;
}
+Dominik Jonczyk So if you click the following buttons: [1][.][5], in the text box, you get 15 rather than 1.5?
Have found the error .
The fault is in this case that I develop in Germany .
With German Windows ! Therefore one has to rely on point the dot operator ( number separator ) .
As the text is read property from the button , you have to set these also point to point .
Thus, it works with the German development environment . Otherwise, as in the video !
Here the corrections in the code:
private void btn_Click(object sender, EventArgs e)
{
if ((result.Text == "0") || (operation_pressed))
result.Clear();
operation_pressed = false;
Button b = (Button)sender;
if (b.Text == ",")
{
if (!result.Text.Contains(","))
result.Text = result.Text + b.Text;
}
else
result.Text = result.Text + b.Text;
}
Ahhhh. Glad you figured it out!
do you have a original file to download, pls?
FormBorderStyle = Fixed3D Is an easier way to fix the size of the window to the size you create it at. This is in WVS 2015 on windows 10 so it may be different.
Nice one!! only mine dot behavior crazy. put to as first line all time..
Thanks a lot dude!
which is more accurate please? Double or Float?
Creador Danbont the double is more accurate.
it was named double because it is a double float
Alexander Ross well, a double precise float is the term
how do you program the calculator to not divide by 0
case "/":
if (result.Text == "0") //Division by ZERO = Message
error = MessageBox.Show("Pamietaj cholero nie dziel przez ZERO !");
else
result.Text = (value / Double.Parse(result.Text)).ToString();
break;
I enjoyed to make calculator but there are some error. for example if we add two values 3 + 4 = 7 the result 7 and you type any number it will continue with 7. i.e. 723232 like this
Perfect
I think that there are 2 problems:
1. In the windows calculator for example if you do 7+2 and then press= it 9 AND then if press again = it 11 and so on 13,15,17. In this calc it doesn't do it.
2. also in the windows calculator for example if you do 3+2 and then press= it 5 and you can do anther calculation but in this calculator after you press = you can't click on the numbers because the numbers will be added to the number that is already displayed.
Can Please fix it?
all in all you do great videos and please keep it like this!!!!!
why insert funcion mod(%)
The decimal point code; when I use it; it draws two decimal points? WTF is happening? lol
Simplesmente, Perfeito
зря только мучился у меня в PerforaClick выдает ошибку.
1,5 + 1 = 16 Please help
I like it, but the video quality could be better :'(
Hey! Make sure you switch the video to 720P (I believe default is 240P for RUclips).
Chris Merritt It's strange, but i see only 360p quality for this video funkyimg.com/i/HDEa.jpg
Part 1 and Part 3 are good (720p). Anyway awesome videos and best calculator tutorial.
You can't compare result.Text with 0, becuse what happens if you want to type a number that is less than 1 and larger than 0? Your logic is bad.
+miloscarcina if the number is less than 1 and larger than 0, there will be a decimal point present.
Dude you dont program good it has so many bugs
Dude this is not meant to be a fully functional piece of software; it's meant to help people get started with winforms and c#.
@@chrismerritt7291 dw about him/her, you helped me and millions of other people. Thank you Chris, 7 years later.
Raphael, what error do you get?
I found something wrong : when I press 7 * 5 is equal 35 and then I try to subtract with 20 but in the textbox it's change to 0 not 20 as i want
why would you like 20 to come out when you subtract 20 from 35?