C# :: Aufgabe #273
2 Lösungen

Funktionswerte einer Parabel berechnen - Windows Forms
Anfänger - C#
von paddlboot
- 04.12.2019 um 16:13 Uhr
Lösungen:
Form1.cs
C#-Code
Form1.Designer.cs
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

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 } }
Hallo,
danke für diese Aufgabe, habe mich zum ersten mal mit der ListBox beschäftigt und viel gelernt.
Ich hoffe das passt so weit :-)
C#-Code
C#-Code
danke für diese Aufgabe, habe mich zum ersten mal mit der ListBox beschäftigt und viel gelernt.
Ich hoffe das passt so weit :-)

using System; using System.Windows.Forms; namespace Aufgabe_273_Parabel { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Reset_Button_Click(object sender, EventArgs e) { Startwert.Text = "0"; Endwert.Text = "0"; Schrittweite.Text = "1"; listBox1.Items.Clear(); } private void Start_Button_Click(object sender, EventArgs e) { listBox1.Items.Add("Berechnete Werte"); listBox1.Items.Add("- - - - - - - - - - - - - - - - -"); double start = Convert.ToDouble(Startwert.Text); double schritt = Convert.ToDouble(Schrittweite.Text); double ende = Convert.ToDouble(Endwert.Text); for (double i = start; i <= ende; i = i + schritt) { listBox1.Items.Add(" X-Wert " + i + " Y-Wert " + i * i); } } } }

namespace Aufgabe_273_Parabel { 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.groupBox1 = new System.Windows.Forms.GroupBox(); this.Schrittweite = new System.Windows.Forms.TextBox(); this.Endwert = new System.Windows.Forms.TextBox(); this.Startwert = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.listBox1 = new System.Windows.Forms.ListBox(); this.Start_Button = new System.Windows.Forms.Button(); this.Reset_Button = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.Schrittweite); this.groupBox1.Controls.Add(this.Endwert); this.groupBox1.Controls.Add(this.Startwert); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.groupBox1.Location = new System.Drawing.Point(27, 32); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 180); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "X-Werte"; // // Schrittweite // this.Schrittweite.Location = new System.Drawing.Point(125, 135); this.Schrittweite.Name = "Schrittweite"; this.Schrittweite.Size = new System.Drawing.Size(34, 20); this.Schrittweite.TabIndex = 5; this.Schrittweite.Text = "1"; this.Schrittweite.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; // // Endwert // this.Endwert.Location = new System.Drawing.Point(125, 95); this.Endwert.Name = "Endwert"; this.Endwert.Size = new System.Drawing.Size(34, 20); this.Endwert.TabIndex = 4; this.Endwert.Text = "0"; this.Endwert.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; // // Startwert // this.Startwert.Location = new System.Drawing.Point(125, 50); this.Startwert.Name = "Startwert"; this.Startwert.Size = new System.Drawing.Size(34, 20); this.Startwert.TabIndex = 3; this.Startwert.Text = "0"; this.Startwert.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(21, 135); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(74, 13); this.label3.TabIndex = 2; this.label3.Text = "Schrittweite"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(21, 95); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(53, 13); this.label2.TabIndex = 1; this.label2.Text = "Endwert"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(21, 53); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(58, 13); this.label1.TabIndex = 0; this.label1.Text = "Startwert"; // // listBox1 // this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.listBox1.FormattingEnabled = true; this.listBox1.Location = new System.Drawing.Point(264, 32); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(211, 277); this.listBox1.TabIndex = 1; // // Start_Button // this.Start_Button.Location = new System.Drawing.Point(27, 252); this.Start_Button.Name = "Start_Button"; this.Start_Button.Size = new System.Drawing.Size(90, 50); this.Start_Button.TabIndex = 2; this.Start_Button.Text = "Start"; this.Start_Button.UseVisualStyleBackColor = true; this.Start_Button.Click += new System.EventHandler(this.Start_Button_Click); // // Reset_Button // this.Reset_Button.Location = new System.Drawing.Point(137, 252); this.Reset_Button.Name = "Reset_Button"; this.Reset_Button.Size = new System.Drawing.Size(90, 50); this.Reset_Button.TabIndex = 3; this.Reset_Button.Text = "Reset"; this.Reset_Button.UseVisualStyleBackColor = true; this.Reset_Button.Click += new System.EventHandler(this.Reset_Button_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(506, 340); this.Controls.Add(this.Reset_Button); this.Controls.Add(this.Start_Button); this.Controls.Add(this.listBox1); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Aufgabe_273_Funktionswerte Parabel"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.TextBox Schrittweite; private System.Windows.Forms.TextBox Endwert; private System.Windows.Forms.TextBox Startwert; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button Start_Button; private System.Windows.Forms.Button Reset_Button; } }