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

Поделиться
HTML-код
  • Опубликовано: 2 фев 2025

Комментарии • 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 2 года назад

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

    • @bernardopower5267
      @bernardopower5267 2 года назад

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

    • @MudHoleCreation
      @MudHoleCreation 2 года назад

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

  • @chrismerritt7291
    @chrismerritt7291  11 лет назад +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 11 лет назад

      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  11 лет назад +1

      Check out this video I just posted. Think it covers what you're looking for: ruclips.net/video/hrWbqJKRMS4/видео.html

    • @john20x
      @john20x 11 лет назад

      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  11 лет назад

      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

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

  • @vnd123-d6x
    @vnd123-d6x 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!

  • @kenpcmusic5575
    @kenpcmusic5575 8 лет назад +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.

    • @潘驄杰
      @潘驄杰 4 года назад

      [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

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

  • @bernardopower5267
    @bernardopower5267 2 года назад

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

  • @robertoadame2768
    @robertoadame2768 3 года назад

    Great job! Thank you for sharing!

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

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

  • @MatthewBendyna
    @MatthewBendyna 3 года назад

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

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

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

    • @refract8491
      @refract8491 8 лет назад

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

  • @Rii-Americano
    @Rii-Americano 8 лет назад +9

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

  • @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?

  • @bernardopower5267
    @bernardopower5267 2 года назад

    Hi, how do u key press backspace ?

  • @mckartep
    @mckartep 11 лет назад +1

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

    • @mckartep
      @mckartep 11 лет назад

      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

  • @Aadarsh155
    @Aadarsh155 10 лет назад +1

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

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

  • @MrSteveman22222
    @MrSteveman22222 10 лет назад

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

  • @rwxstudio7173
    @rwxstudio7173 11 лет назад

    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 11 лет назад

      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 11 лет назад

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

  • @raoskidoo15
    @raoskidoo15 11 лет назад +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

  • @Sndubuisi950
    @Sndubuisi950 6 лет назад

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

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

  • @sirisharani8743
    @sirisharani8743 9 лет назад

    Thank you for this video!

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

  • @namanmalhotra4169
    @namanmalhotra4169 4 года назад

    13:36 thank you so much!!!

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

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

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

  • @abdulrahmanrefaei1023
    @abdulrahmanrefaei1023 8 лет назад

    do you have a original file to download, pls?

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

  • @johanBe75
    @johanBe75 9 лет назад

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

  • @mubeenmaheen1353
    @mubeenmaheen1353 7 лет назад

    Thanks a lot dude!

  • @creadordanbont9410
    @creadordanbont9410 10 лет назад

    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

  • @irf5aan
    @irf5aan 10 лет назад

    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;

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

  • @mroalireza8070
    @mroalireza8070 2 года назад

    Perfect

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

  • @wanrerkkhamsamer5634
    @wanrerkkhamsamer5634 8 лет назад

    why insert funcion mod(%)

  • @LukeJF89
    @LukeJF89 8 лет назад

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

  • @danilovictor1
    @danilovictor1 7 лет назад

    Simplesmente, Perfeito

  • @МаринаМоргунова-ж6к
    @МаринаМоргунова-ж6к 10 лет назад +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 RUclips).

    • @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 9 лет назад

    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  9 лет назад +1

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

  • @MuhammadHamza-fl7rn
    @MuhammadHamza-fl7rn 6 лет назад

    Dude you dont program good it has so many bugs

    • @chrismerritt7291
      @chrismerritt7291  6 лет назад +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 11 лет назад

    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 7 лет назад +1

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