C# :: Aufgabe #273 :: Lösung #1

2 Lösungen Lösungen öffentlich
#273

Funktionswerte einer Parabel berechnen - Windows Forms

Anfänger - C# von paddlboot - 04.12.2019 um 16:13 Uhr
Erstelle eine Windows Form, in der Funktionswerte einer Parabel berechnet werden können.
Dafür sollen Start-, Endwert und Schrittweite über Textboxen eingelesen werden und nach dem Betätigen des Berechnen-Buttons die X- und Y-Werte in einer ListBox ausgegeben werden.
Mögliches Aussehen siehe Anhang.
#1
vote_ok
von Exception (7090 Punkte) - 21.12.2019 um 19:58 Uhr
Form1.cs
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace _273_Parabelrechner
{
    public partial class Form1 : Form
    {
        private static Panel panel_left;
        private static Panel panel_right;
        private static Dictionary<string, int> values;

        public Form1()
        {
            InitializeComponent();
            InitControls();
            values = new Dictionary<string, int>();
        }

        private void InitControls()
        {
            this.Width = 500;
            this.Height = 350;
            this.MaximizeBox = false;
            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.Fixed3D;
            this.Text = "273 - Parabelrechner";

            #region left

            panel_left = new Panel();
            panel_left.Width = this.Width / 2;
            panel_left.Height = this.Height;
            panel_left.Location = new Point(0, 0);

            GroupBox g = new GroupBox();
            g.Width = panel_left.Width - 20;
            g.Height = (int)(panel_left.Height - 150);
            g.Text = "X-Werte";
            g.Name = "GroupBox";
            g.Location = new Point(10, 10);

            int x = 10, y = 50;

            Label l = new Label();
            l.Name = "label_Startwert";
            l.Text = "Startwert";
            l.Location = new Point(x, y);

            TextBox tb = new TextBox();
            tb.Name = "textbox_Startwert";
            tb.Width = (int)(g.Width / 2.5);
            tb.Location = new Point(l.Right + 20, l.Top);
            tb.KeyUp += Textbox_Validation;

            g.Controls.Add(tb);
            g.Controls.Add(l);

            y += 50;

            l = new Label();
            l.Name = "label_Endwert";
            l.Text = "Endwert";
            l.Location = new Point(x, y);

            tb = new TextBox();
            tb.Name = "textbox_Endwert";
            tb.Width = (int)(g.Width / 2.5);
            tb.Location = new Point(l.Right + 20, l.Top);
            tb.KeyUp += Textbox_Validation;

            g.Controls.Add(tb);
            g.Controls.Add(l);

            y += 50;

            l = new Label();
            l.Name = "label_Schrittweite";
            l.Text = "Schrittweite";
            l.Location = new Point(x, y);

            tb = new TextBox();
            tb.Name = "textbox_Schrittweite";
            tb.Width = (int)(g.Width / 2.5);
            tb.Location = new Point(l.Right + 20, l.Top);
            tb.KeyUp += Textbox_Validation;

            g.Controls.Add(tb);

            g.Controls.Add(l);

            panel_left.Controls.Add(g);

            Button b = new Button();
            b.Name = "button_berechnen";
            b.Text = "&Berechnen";
            b.Width = panel_left.Width / 2 - 30;
            b.Height = 85;
            b.Location = new Point(10, g.Bottom + 5);
            b.Click += Click_Calculate;

            panel_left.Controls.Add(b);

            b = new Button();
            b.Name = "button_reset";
            b.Text = "&Reset";
            b.Width = panel_left.Width / 2 - 30;
            b.Height = 85;
            b.Location = new Point(g.Right - b.Width, g.Bottom + 5);
            b.Click += Click_Reset;

            panel_left.Controls.Add(b);

            this.Controls.Add(panel_left);

            #endregion

            #region right

            panel_right = new Panel();
            panel_right.Width = panel_left.Width;
            panel_right.Height = panel_left.Height;
            panel_right.Location = new Point(panel_left.Width, 0);

            ListBox lb = new ListBox();
            lb.Name = "listbox";
            lb.Location = new Point(10, 10);
            lb.Width = panel_right.Width - 45;
            lb.Height = panel_right.Height - 60;

            panel_right.Controls.Add(lb);

            this.Controls.Add(panel_right);
            #endregion
        }

        private void Textbox_Validation(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;

            int value;

            if(int.TryParse(tb.Text, out value))
            {
                tb.BackColor = default(Color);

                string key = tb.Name.Replace("textbox_", "");

                if (values.ContainsKey(key))
                {
                    values[key] = value;
                }
                else
                {
                    values.Add(key, value);
                }
            }
            else
            {
                tb.BackColor = Color.LightPink;
            }
        }

        private void Click_Calculate(object sender, EventArgs e)
        {
            int startwert, endwert, schrittweite;

            if (!values.TryGetValue("Startwert", out startwert) || !values.TryGetValue("Endwert", out endwert) || !values.TryGetValue("Schrittweite", out schrittweite))
            {
                return;
            }

            ListBox lb = panel_right.Controls.Find("listbox", true).FirstOrDefault() as ListBox;
            lb.Items.Clear();

            lb.Items.Add("Berechnete Werte");
            lb.Items.Add("-------------------------------");

            for (int i = startwert; i <= endwert; i += schrittweite)
            {
                int x = i;
                int y = x * x;
                lb.Items.Add($"X: {x}\tY: {y}");
            }
        }

        private void Click_Reset(object sender, EventArgs e)
        {
            values.Clear();

            ListBox lb = panel_right.Controls.Find("listbox", true).FirstOrDefault() as ListBox;
            lb.Items.Clear();
        }
    }
}


Form1.Designer.cs
Quellcode ausblenden C#-Code
namespace _273_Parabelrechner
{
    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(251, 39);
            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