C# :: Aufgabe #266 :: Lösung #4

4 Lösungen Lösungen öffentlich
#266

Flächenberechnung - Einstieg in Windows Forms

Anfänger - C# von paddlboot - 29.10.2019 um 11:12 Uhr
Erstelle ein Windows Forms Programm, in dem die Flächen von einem Dreieck und einem Rechteck berechnet werden können.

Die Eingben von Höhe und Breite sollen in einer Textbox erfolgen.
Nachdem auf einen Button (Rechteck, Dreieck, Beenden) geklickt wurde, soll das Ergebnis in einer Dritten Textbox ausgegeben werden, in welcher keine Eingaben gemacht werden können.
Bei Klicken des Beenden Buttons soll das Programm geschlossen werden.

Wie das ganze Aussehen könnte, sieht man in dem angehängten Screenshot.

Viel Spaß :)
#4
vote_ok
von crazyfrien (80 Punkte) - 07.11.2019 um 11:04 Uhr
Quellcode ausblenden C#-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 Flächenberechnung
{
    public partial class Form1 : Form
    {
        private double height = 0;
        private double width = 0;
        private double ergebnis = 0;
        private bool expanded = false;

        public Form f;
        public Label l1;
        public Label l2;
        public TextBox t1;
        public TextBox t2;
        public TextBox t3;
        public Button btn;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f = new Form();

            l1 = new Label();
            l1.Text = "Höhe";
            l1.Top = 25;
            l1.Left = 25;

            l2 = new Label();
            l2.Text = "Breite";
            l2.Top = 50;
            l2.Left = 25;

            t1 = new TextBox();
            t1.Top = l1.Top;
            t1.Left = l1.Width+30;
            t1.Width = 150;

            t2 = new TextBox();
            t2.Top = l2.Top;
            t2.Left = l2.Width+30;
            t2.Width = 150;



            btn = new Button();
            btn.Text = "Bestätigen";
            btn.Left = 25;
            btn.Width = t2.Right;
            btn.Top = t2.Bottom + 10;
            btn.Click += Blubb;

            f.Width = t1.Right + 50;
            f.Height = btn.Bottom + 50;

            f.Controls.Add(l1);
            f.Controls.Add(l2);
            f.Controls.Add(t1);
            f.Controls.Add(t2);
            f.Controls.Add(btn);

            f.Show();
        }

        public void Blubb(object sender, EventArgs e)
        {
            height = Convert.ToInt32(t1.Text);
            width = Convert.ToInt32(t2.Text);
            f.Close();



            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!expanded)
            {
                double hvor = this.Height;
                double hsoll = this.Height + 30;
                for (int i = Convert.ToInt32(hvor); i < Convert.ToInt32(hsoll); i++)
                {
                    System.Threading.Thread.Sleep(5);
                    this.Height = i;
                }

                System.Threading.Thread.Sleep(500);
                expanded = true;
                showBlubb();
            }
            ergebnis = height * width;
            showErgebnis();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!expanded)
            {
                double hvor = this.Height;
                double hsoll = this.Height + 30;
                for (int i = Convert.ToInt32(hvor); i < Convert.ToInt32(hsoll); i++)
                {
                    System.Threading.Thread.Sleep(5);
                    this.Height = i;
                }

                System.Threading.Thread.Sleep(500);
                expanded = true;
                showBlubb();
            }
            ergebnis = (height * width) / 2;
            showErgebnis();
        }
        private void showBlubb()
        {
            t3 = new TextBox();
            t3.Width = this.Width - 50;
            t3.Left = 15;
            t3.Top = button2.Bottom + 10;
            t3.ReadOnly = true;
            this.Controls.Add(t3);
        }
        private void showErgebnis()
        {
            t3.Text = Convert.ToString(ergebnis);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.google.com/search?q=dieses+Programm+funktioniert&rlz=1C1GCEU_deDE819DE819&oq=Dieses+Programm+funktioniert&aqs=chrome.0.69i59j0.4258j0j7&sourceid=chrome&ie=UTF-8");
            Application.Exit();
        }
    }
}

Kommentare:

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

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

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.