(Part 2) Visual Studio WinForm Windows Calculator Tutorial Example (C#)

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ต.ค. 2024

ความคิดเห็น • 97

  • @MudHoleCreation
    @MudHoleCreation 10 ปีที่แล้ว +13

    To add Enter for equals. Just put the Tabstop property to false on all the buttons exept equals. Even the textbox.

    • @Andre2hits
      @Andre2hits 7 ปีที่แล้ว

      Thank you very much man! That helped out wonders. What MudHoleCreation said fixes the Enter!

    • @kaladin_4
      @kaladin_4 4 ปีที่แล้ว

      Thank you so much. :)

    • @bernardopower5267
      @bernardopower5267 ปีที่แล้ว

      Hi, Do u know how to display the equation textbox to fill equation instead of 2 oprehend and 1 operator ?

    • @bernardopower5267
      @bernardopower5267 ปีที่แล้ว

      @MudholeCreation do uk how to keypress backspace button and display the full equation in the equation textbox eg 1+2+3+4 ?

    • @MudHoleCreation
      @MudHoleCreation ปีที่แล้ว

      @@bernardopower5267 Sorry, but no, I probably did at the time. But this was 8 years ago.

  • @AlexApho
    @AlexApho 7 ปีที่แล้ว +1

    Hello, Chris. Your lessons are very good. Thanks for the new knowledge.

  • @ivanderianto7310
    @ivanderianto7310 8 ปีที่แล้ว +2

    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!

  • @kop3sh
    @kop3sh 8 ปีที่แล้ว +1

    @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...

  • @chrismerritt7291
    @chrismerritt7291  10 ปีที่แล้ว +12

    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.

    • @john20x
      @john20x 10 ปีที่แล้ว

      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.

    • @chrismerritt7291
      @chrismerritt7291  10 ปีที่แล้ว +1

      Check out this video I just posted. Think it covers what you're looking for: th-cam.com/video/hrWbqJKRMS4/w-d-xo.html

    • @john20x
      @john20x 10 ปีที่แล้ว

      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

    • @chrismerritt7291
      @chrismerritt7291  10 ปีที่แล้ว

      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.

    • @arnoid11
      @arnoid11 10 ปีที่แล้ว +1

      my calculator is showing 1 2tmes and same for all other numerical buttons

  • @JohnBrown-hp7sq
    @JohnBrown-hp7sq 8 ปีที่แล้ว

    Good tutorial this gave me the basis to build a neat little scientific calculator

  • @ronaldjohnson4470
    @ronaldjohnson4470 10 ปีที่แล้ว

    Thank you, part 2 answered my question about coding the decimal point. Thanks.

  • @kenpcmusic5575
    @kenpcmusic5575 7 ปีที่แล้ว +8

    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.

    • @潘驄杰
      @潘驄杰 3 ปีที่แล้ว

      [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

  • @bernardopower5267
    @bernardopower5267 ปีที่แล้ว

    Hi, Do u know how to display the equation textbox to fill equation instead of 2 oprehend and 1 operator ?

  • @bigbangsangco7973
    @bigbangsangco7973 9 ปีที่แล้ว

    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?

  • @andrelrohenkohl
    @andrelrohenkohl 8 ปีที่แล้ว +6

    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.

  • @14mebloscianka88
    @14mebloscianka88 10 ปีที่แล้ว

    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.

  • @rakibulhasan8592
    @rakibulhasan8592 5 ปีที่แล้ว

    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

  • @MatthewBendyna
    @MatthewBendyna 2 ปีที่แล้ว

    So how do I handle keypresses like ENTER, BACKSPACE, and DELETE?

  • @robertoadame2768
    @robertoadame2768 3 ปีที่แล้ว

    Great job! Thank you for sharing!

  • @brodybishop7647
    @brodybishop7647 9 ปีที่แล้ว +1

    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?)"

    • @saltyasian4462
      @saltyasian4462 8 ปีที่แล้ว

      Maybe your spelling "Preform" wrong? It's supposed to be "Perform".

  • @MrSanfrinsisco
    @MrSanfrinsisco 5 ปีที่แล้ว

    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.

    • @Siilur
      @Siilur 4 ปีที่แล้ว

      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.

  • @wibudesu205
    @wibudesu205 8 ปีที่แล้ว +9

    if you have problem with decimal number, i think you must change "." with "," in the text button '.'

  • @lyarielly
    @lyarielly 10 ปีที่แล้ว

    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!!!!!

  • @vasilis500
    @vasilis500 8 ปีที่แล้ว

    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.

    • @sergejkeser7270
      @sergejkeser7270 7 ปีที่แล้ว

      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...

  • @MrSteveman22222
    @MrSteveman22222 9 ปีที่แล้ว

    Why does a line such as this appear when the text edit line appear when entering numbers/ operators ?

  • @mckartep
    @mckartep 10 ปีที่แล้ว +1

    When I press "+" button between double values, I get error in this line:
    value = Double.Parse(result.Text);
    please help!

    • @mckartep
      @mckartep 10 ปีที่แล้ว

      Oh, i understood. No problem. CultureInfo helped.

    • @kurekureci
      @kurekureci 8 ปีที่แล้ว

      +Евгений Ананченко help me!!! i have the same problem but i cant solve it!

    • @claramashwama8883
      @claramashwama8883 4 ปีที่แล้ว

      i have the same problem

  • @rwxstudio7173
    @rwxstudio7173 10 ปีที่แล้ว

    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?

    • @rjc131
      @rjc131 10 ปีที่แล้ว

      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.

    • @rwxstudio7173
      @rwxstudio7173 10 ปีที่แล้ว

      Okay, you're going to have to show me on Sunday over hangouts.

  • @Mojomatrix
    @Mojomatrix 9 ปีที่แล้ว

    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 ?

    • @chrismerritt7291
      @chrismerritt7291  9 ปีที่แล้ว

      Mojomatrix just add result.Text = ""; in your event handler when you want the text box to be cleared.

  • @Aadarsh155
    @Aadarsh155 9 ปีที่แล้ว +1

    Use this to make the return key work in the calculator:
    case "
    ":
    equal.PerformClick();
    break;

  • @sirisharani8743
    @sirisharani8743 9 ปีที่แล้ว

    Thank you for this video!

  • @Sndubuisi950
    @Sndubuisi950 5 ปีที่แล้ว

    How can you make the result add comas to separate numbers in thousands and millions ?

  • @bernardopower5267
    @bernardopower5267 ปีที่แล้ว

    Hi, how do u key press backspace ?

  • @raoskidoo15
    @raoskidoo15 10 ปีที่แล้ว +1

    For some reasons , I can't do a Double.Parse(TEXTBOX.TEXT); I do get an error ... I have no idea why

    • @jobijose8354
      @jobijose8354 6 ปีที่แล้ว

      maybe the textbox name will be wrong

  • @mroalireza8070
    @mroalireza8070 2 ปีที่แล้ว

    Perfect

  • @LukeJF89
    @LukeJF89 8 ปีที่แล้ว

    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.

  • @namanmalhotra4169
    @namanmalhotra4169 4 ปีที่แล้ว

    13:36 thank you so much!!!

  • @mubeenmaheen1353
    @mubeenmaheen1353 7 ปีที่แล้ว

    Thanks a lot dude!

  • @bratal2
    @bratal2 10 ปีที่แล้ว

    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

    • @jankuhrau1181
      @jankuhrau1181 5 ปีที่แล้ว

      works for me. Maybe you have a Mistake somewhere

  • @AsadKhan-vm8cz
    @AsadKhan-vm8cz 4 ปีที่แล้ว

    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

  • @emanuelbaldissera6510
    @emanuelbaldissera6510 10 ปีที่แล้ว

    Nice video!! :D
    Thanks!

  • @DominikSascha
    @DominikSascha 8 ปีที่แล้ว

    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;
    }

    • @chrismerritt7291
      @chrismerritt7291  8 ปีที่แล้ว

      +Dominik Jonczyk So if you click the following buttons: [1][.][5], in the text box, you get 15 rather than 1.5?

    • @DominikSascha
      @DominikSascha 8 ปีที่แล้ว +1

      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;
      }

    • @chrismerritt7291
      @chrismerritt7291  8 ปีที่แล้ว

      Ahhhh. Glad you figured it out!

  • @kurekureci
    @kurekureci 8 ปีที่แล้ว +4

    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!

  • @danilovictor1
    @danilovictor1 7 ปีที่แล้ว

    Simplesmente, Perfeito

  • @johanBe75
    @johanBe75 8 ปีที่แล้ว

    Nice one!! only mine dot behavior crazy. put to as first line all time..

  • @creadordanbont9410
    @creadordanbont9410 9 ปีที่แล้ว

    which is more accurate please? Double or Float?

    • @alecr474
      @alecr474 9 ปีที่แล้ว

      Creador Danbont the double is more accurate.
      it was named double because it is a double float

    • @alecr474
      @alecr474 9 ปีที่แล้ว

      Alexander Ross well, a double precise float is the term

  • @abdulrahmanrefaei1023
    @abdulrahmanrefaei1023 8 ปีที่แล้ว

    do you have a original file to download, pls?

  • @irf5aan
    @irf5aan 9 ปีที่แล้ว

    how do you program the calculator to not divide by 0

    • @janekback
      @janekback 7 ปีที่แล้ว +1

      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;

  • @LukeJF89
    @LukeJF89 8 ปีที่แล้ว

    The decimal point code; when I use it; it draws two decimal points? WTF is happening? lol

  • @wanrerkkhamsamer5634
    @wanrerkkhamsamer5634 8 ปีที่แล้ว

    why insert funcion mod(%)

  • @МаринаМоргунова-ж6к
    @МаринаМоргунова-ж6к 9 ปีที่แล้ว +1

    зря только мучился у меня в PerforaClick выдает ошибку.

  • @lindgren2097
    @lindgren2097 3 ปีที่แล้ว

    1,5 + 1 = 16 Please help

  • @stelxr
    @stelxr 10 ปีที่แล้ว

    I like it, but the video quality could be better :'(

    • @chrismerritt7291
      @chrismerritt7291  10 ปีที่แล้ว

      Hey! Make sure you switch the video to 720P (I believe default is 240P for TH-cam).

    • @stelxr
      @stelxr 10 ปีที่แล้ว +1

      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.

  • @miloscarcina
    @miloscarcina 8 ปีที่แล้ว

    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.

    • @chrismerritt7291
      @chrismerritt7291  8 ปีที่แล้ว +1

      +miloscarcina if the number is less than 1 and larger than 0, there will be a decimal point present.

  • @MuhammadHamza-fl7rn
    @MuhammadHamza-fl7rn 5 ปีที่แล้ว

    Dude you dont program good it has so many bugs

    • @chrismerritt7291
      @chrismerritt7291  5 ปีที่แล้ว +1

      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#.

    • @Infinity540
      @Infinity540 3 ปีที่แล้ว

      @@chrismerritt7291 dw about him/her, you helped me and millions of other people. Thank you Chris, 7 years later.

  • @chrismerritt8642
    @chrismerritt8642 10 ปีที่แล้ว

    Raphael, what error do you get?

  • @suneesahod5610
    @suneesahod5610 8 ปีที่แล้ว

    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

    • @donryan5076
      @donryan5076 6 ปีที่แล้ว +1

      why would you like 20 to come out when you subtract 20 from 35?