C# :: Aufgabe #274 :: Lösung #3

3 Lösungen Lösungen öffentlich
#274

Windows Forms Taschenrechner

Anfänger - C# von paddlboot - 04.12.2019 um 17:09 Uhr
Erstelle mit Hilfe von Windows Forms einen Taschenrechner!

- Der Taschenrechner soll zwei Zahlen speichern können.
- Wie bei einem Taschenrechner üblich, sollen alle Zahleneingaben gesammelt werden bis der Operator gewählt wurde.
- Mit jedem Drücken einer Zahlentaste wird der Variable also eine Dezimalstelle hinzugefügt.
- Wenn ein Operator ausgewählt wurde, soll die erste eingegebene Zahl gespeichert werden, ebenso der Operator (in einer separaten Variable).
- Mit Drücken der Ergebnis Taste (=) soll das Ergebnis in der selben Textbox ausgegeben werden, wie die, in der die Zahlen eingegeben wurden.

Im Anhang ein Beispiel wie ich das gelöst habe.
#3
vote_ok
von Exception (7090 Punkte) - 22.12.2019 um 09:21 Uhr
Form1.cs
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace _274_Taschenrechner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.InitControls();
        }

        private void InitControls()
        {
            this.Width = 350;
            this.Height = 400;
            this.BackColor = Color.Green;
            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.Fixed3D;
            this.Text = "A Simple Green Calculator";

            TextBox t = new TextBox();
            t.Name = "output";
            t.Width = this.Width - 40;
            t.ReadOnly = true;
            t.Location = new Point(10, 10);

            this.Controls.Add(t);

            int size = 50;
            int x = this.Width - 100, y = t.Bottom + 20;

            Button b;

            for (int i = 9; i >= 1; i--)
            {
                b = new Button();
                b.Name = "" + i;
                b.Text = b.Name;
                b.Width = size;
                b.Height = size;
                b.FlatStyle = FlatStyle.Flat;
                b.BackColor = Color.LightGreen;
                b.Click += Click_Digit;

                if (i % 3 == 0 && i != 9)
                {
                    y += 75;
                    x = this.Width - 175;
                }
                else
                {
                    x -= 75;
                }

                b.Location = new Point(x, y);

                this.Controls.Add(b);
            }

            x = 28;

            b = new Button();
            b.Name = "" + 0;
            b.Text = b.Name;
            b.Width = size;
            b.Height = size;
            b.FlatStyle = FlatStyle.Flat;
            b.BackColor = Color.LightGreen;
            b.Location = new Point(x, y + 75);
            b.Click += Click_Digit;
            this.Controls.Add(b);

            x += 75;

            b = new Button();
            b.Name = "C";
            b.Text = b.Name;
            b.Width = size;
            b.Height = size;
            b.FlatStyle = FlatStyle.Flat;
            b.BackColor = Color.LightPink;
            b.Location = new Point(x, y + 75);
            b.Click += Click_Reset;
            this.Controls.Add(b);

            x += 75;

            b = new Button();
            b.Name = "=";
            b.Text = b.Name;
            b.Width = size;
            b.Height = size;
            b.FlatStyle = FlatStyle.Flat;
            b.BackColor = Color.Gold;
            b.Location = new Point(x, y + 75);
            b.Click += Click_Calculate;
            this.Controls.Add(b);

            x = this.Width - 100;
            y = t.Bottom + 20;

            List<string> operators = new List<string>() { "+", "-", "*", "/" };

            foreach(string s in operators)
            {
                b = new Button();
                b.Name = s;
                b.Text = b.Name;
                b.Width = size;
                b.Height = size;
                b.FlatStyle = FlatStyle.Flat;
                b.BackColor = Color.LightSkyBlue;
                b.Location = new Point(x, y);
                b.Click += Click_Operator;

                y += 75;

                this.Controls.Add(b);
            }
        }

        private TextBox getOutputTextbox()
        {
            return this.Controls.Find("output", true).FirstOrDefault() as TextBox;
        }

        private void Click_Digit(object sender, EventArgs e)
        {
            Button b = sender as Button;
            TextBox tb = this.getOutputTextbox();
            tb.Text += b.Name;
        }

        private void Click_Reset(object sender, EventArgs e)
        {
            TextBox tb = this.getOutputTextbox();
            tb.Clear();
        }

        private void Click_Calculate(object sender, EventArgs e)
        {
            TextBox tb = this.getOutputTextbox();
            tb.Text = new DataTable().Compute(tb.Text, null).ToString();
        }
        

        private void Click_Operator(object sender, EventArgs e)
        {
            Button b = sender as Button;
            TextBox tb = this.getOutputTextbox();
            tb.Text += b.Name;
        }
    }
}


Form1.Designer.cs
Quellcode ausblenden C#-Code
namespace _274_Taschenrechner
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(182, 25);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben
2092073

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.