Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with C# programming Given Code: ********Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace FormsDictionsry { static class Program {

Need help with C# programming

image text in transcribed

Given Code:

********Program.cs

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms;

namespace FormsDictionsry { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

*************Form1.cs

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 FormsDictionsry { public partial class Form1 : Form { Dictionary myDictionary = new Dictionary(); public Form1() { InitializeComponent(); }

private void Form1_Load(object sender, EventArgs e) { textBoxType.Text = ""; textBoxWeight.Text = ""; textBoxDom.Text = ""; textBoxLifeSpan.Text = ""; }

private void buttonAdd_Click(object sender, EventArgs e) { Animal newAnimal = new Animal(); newAnimal.Type = textBoxType.Text; newAnimal.AverageWeight = Convert.ToInt32(textBoxWeight.Text); newAnimal.Domesticated = false; if (textBoxDom.Text.ToUpper() == "Y") { newAnimal.Domesticated = true; } newAnimal.LifeSpan = Convert.ToInt32(textBoxLifeSpan.Text); myDictionary.Add(newAnimal.Type, newAnimal); }

private void buttonLookUp_Click(object sender, EventArgs e) { if (myDictionary.ContainsKey(textBoxType.Text)) { textBoxWeight.Text = myDictionary[textBoxType.Text].AverageWeight.ToString(); textBoxDom.Text = myDictionary[textBoxType.Text].Domesticated.ToString(); textBoxLifeSpan.Text = myDictionary[textBoxType.Text].LifeSpan.ToString(); } else { textBoxWeight.Text = "Could not find that animal"; textBoxDom.Text = "Could not find that animal"; textBoxLifeSpan.Text = "Could not find that animal"; } }

private void buttonClear_Click(object sender, EventArgs e) { textBoxType.Text = ""; textBoxWeight.Text = ""; textBoxDom.Text = ""; textBoxLifeSpan.Text = ""; } }

public class Animal { public string Type { get; set; } public int AverageWeight { get; set; } public bool Domesticated { get; set; } public int LifeSpan { get; set; }

}

}

**************Form1.Designer.cs

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

If you run it, you will notice that every time you exit the program, all your data in your dictionary is lost. Please fix that. Add 2 buttons to the form, one is "Save Data" the other is "Load Data" Save data reads each animal object from the Dictionary and saves the 4 values in a SQL table. Load data reads that SQL table and creates new objects for each row and adds them to the dictionary. So, if the user starts the program, add 3 animals using the form, and then clicks the new Save button, and then stops the program and re-starts it, and clicks the load button, those 3 animals should be back in the dictionary. If they then add 2 more animals, those 2 should be ADDED to the existing data in SQL. So you can either - in the save, clear all SQL rows and then just add all the entries from the DB (easier, but not very efficient!) Or, - you could add a 5th property to the objects called "saved", and then as you write dictionary objects out to SQL, check that property, if it is false, set it to true and write it to SQL, if it is already true, skip it. C# FormsDictionsry namespace FormsDictionsry ** Forms Dictionsry.Form1 textBox Weight 3 references partial class Form1 /// // Required designer variable. /// private System.ComponentModel.IContainer components = null; 13 14 /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. O references protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components. Dispose(); } base.Dispose(disposing); pngBusss982898289 #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// 1 reference private void InitializeComponent() this.textBoxType = new System.Windows.Forms.T 5.TextBox(); this.textBoxWeight = new System.Windows.Forms.TextBox(); this.textBoxDom = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms. Label(); this. label2 = new System.Windows.Forms. Label(); this.label3 = new System.Windows.Forms. La Label(); this.label4 = new System.Windows.Forms. Label(); .textBoxLifeSpan = new System.Windows.Forms.TextBox(); this.buttonAdd = new System.Windows.Forms.Button(); this.buttonLookup = new System.Windows.Forms.Button(); this.buttonClear = new System.Windows.Forms.Button(); this. SuspendLayout(); 11 textBoxType this.textBoxType.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle. Regular, System.Drawing. GraphicsUnit.Point, ((byte)())); this.textBoxType. Location = new System.Drawing.Point(400, 61); this.textBoxType.Name = "textBoxType"; this.textBoxType.Size = new System.Drawing.Size(198, 26); this.textBoxType.TabIndex = 0; this. = 50 51 Forms Dictionsry.Form1 textBox Weight C FormsDictionsry 51 52 53 Il textBoxWeight this.textBoxweight.Font = new System.Drawing. Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle. Regular, System.Drawing. GraphicsUnit.Point, ((byte)())); this.textBoxweight. Location = new System.Drawing.Point(400, 98); this.textBoxWeight. Name "textBoxWeight"; this.textBoxweight.Size = new System.Drawing.Size(198, 26); this.textBoxWeight.TabIndex = 1; V textBoxDom this.textBoxDom.Font = new System.Drawing. Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle. Regular, System.Drawing. GraphicsUnit.Point, ((byte) (0))); this.textBoxDom. Location = new System.Drawing.Point(400, 137); this.textBoxDom.Name = "textBoxDom"; this.textBoxDom.Size = new System.Drawing.Size(198, 26); this.textBoxDom.TabIndex = 2; // labeli . this.label1. AutoSize = true; sve this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.Fontstyle. Regular, System.Drawing. GraphicsUnit.Point, (byte) (0))); this.label1. Location = new System.Drawing.Point(240, 67); this.label1.Name = "labell"; this.label1.Size = new System.Drawing.Size(43, 20); this.label1.TabIndex = 3; this.label1.Text = "Type"; 78 // label2 !! this. label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.Fontstyle. Regular, System.Drawing. GraphicsUnit.Point, (byte)(0))); this.label2. Location = new System.Drawing.Point(240, 104); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(122, 20); this.label2.TabIndex = 4; this.labe12.Text = "Average weight": label3 17 this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.Fontstyle. Regular, System.Drawing. GraphicsUnit.Point, (byte) (0))); this.label3. Location = new System.Drawing.Point(240, 143); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(154, 20); this.label3.TabIndex = 5; this.labe13. Text = "Domisticated: Y or N"; label4 97 90 po 99 2009 108 101 101 102 103 184 105 17 this. label4.AutoSize = true; this. labe14.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.Fontstyle. Regular, System.Drawing. GraphicsUnit.Point, ((byte)(0))); this.label4. Location = new System.Drawing.Point(240, 188); this.label4.Name = "labe14"; this.label4.Size = new System.Drawing.Size(133, 20); this label Tablndey = 7. $ FormsDictionsry.Form1 textBox Weight this.label4.TabIndex = 7; this.label4.Text = "Average Lifespan"; // // textBoxLifeSpan // this.textBoxLifeSpan.Font = new System.Drawing. Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle. Regular, System.Drawing. Graphics Unit.Point, ((byte) (@))); this.textBoxLifeSpan.Location = new System.Drawing.Point(400, 182); this.textBoxLifeSpan.Name = "textBoxLifeSpan"; this.textBoxLifeSpan.Size = new System.Drawing.Size(198, 26); this.textBoxLifeSpan.TabIndex = 6; // // buttonAdd // . this.buttonAdd.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.Fontstyle. Regular, System.Drawing.GraphicsUnit.Point, (byte)())); this.buttonAdd. Location = new System.Drawing.Point(164, 250); this.buttonAdd.Name = "buttonAdd"; this.buttonAdd.Size = new System.Drawing.Size(198, 29); this.buttonAdd.TabIndex = 8; this.buttonAdd.Text = "Add Animal"; co this.buttonAdd. UseVisualstyleBackcolor = true; this.buttonAdd. Click += new System.EventHandler(this.buttonAdd_Click); = C# Forms Dictionsry 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 133 134 23 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 Sec 156 157 158 159 // button Lookup // . this.buttonLookUp.Font = new System.Drawing. Font("Microsoft Sans Serif", 12F, System.Drawing. Fontstyle. Regular, System.Drawing.GraphicsUnit.Point, (byte)())); this.buttonLookUp. Location = new System.Drawing.Point(627, 250); 1 this.buttonLookUp. Name = "buttonLookUp"; this.buttonLookup.Size = new System.Drawing.Size(198, 29); this.buttonLookUp.TabIndex = 9; 9 this.buttonLookUp.Text = "Look Up Animal"; this.buttonLookUp. UseVisualStyleBackColor = true; = this.buttonLookup.Click += new System.EventHandler(this.buttonLookup_Click); // // buttonClear // this.buttonClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing. FontStyle. Regular, System.Drawing. GraphicsUnit.Point, ((byte)())); this.buttonClear. Location = new System.Drawing.Point(400, 250); this.buttonClear.Name = "buttonClear"; this.buttonClear.Size = new System.Drawing.Size(198, 29); this.buttonClear.TabIndex = 10; this.buttonClear.Text = "Clear Form"; this.buttonClear.UseVisualStyleBackColor = true; = ; this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click); // // Formi // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this. AutoScaleMode = System.Windows.Forms. AutoScaleMode. Font; this.clientSize = new System.Drawing.Size(965, 594); this.Controls.Add(this.buttonClear); this.Controls.Add(this.buttonLookUp); this.Controls.Add(this.buttonAdd); this.Controls.Add(this.label4); this.Controls.Add(this.textBoxLifeSpan); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.labell); this.Controls.Add(this.textBoxDom); this.Controls.Add(this.textBoxWeight); this.Controls.Add(this.textBoxType); this.Name = "Formi"; this.Text = "Formi"; this.Load += new System.EventHandler(this.Form1_Load); this. Resume Layout(false); this.PerformLayout(); #endregion 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 private System.Windows.Forms. TextBox textBoxType; private System.Windows.Forms.TextBox textBoxWeight; private System.Windows.Forms.TextBox textBoxDom; private System.Windows.Forms. Label label1; private System.Windows.Forms. Label label2; private System.Windows.Forms. Label label3; private System.Windows.Forms. Label label4; private System.Windows.Forms.TextBox textBoxLifeSpan; private System.Windows.Forms.Button buttonAdd; private System.Windows.Forms.Button buttonLookup; private System.Windows.Forms.Button buttonClear; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions