/
frmELItems.cs
156 lines (142 loc) · 5.7 KB
/
frmELItems.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace ELItems
{
public partial class frmELItems : Form
{
private Manufacture _manu = null;
public frmELItems()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
// get selected tab and see if the textbox is empty
TextBox txt = findControl(tabControl1, "txtResult" + (tabControl1.TabPages.Count - 1).ToString());
if (txt != null)
{
if (txt.TextLength > 0)
{
// create a new tab
tabControl1.TabPages.Add("I: " + cboItems.Text.Substring(0, cboItems.Text.IndexOf("(") - 1));
// add a new textbox to that tab
TextBox txtNew = new TextBox();
txtNew.Location = new System.Drawing.Point(3, 6);
txtNew.Multiline = true;
txtNew.Name = "txtResult" + (tabControl1.TabPages.Count - 1).ToString();
txtNew.Size = new System.Drawing.Size(733, 487);
txtNew.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
tabControl1.TabPages[tabControl1.TabPages.Count - 1].Controls.Add(txtNew);
txt = txtNew;
}
else
{
tabControl1.SelectedTab.Text = "I: " + cboItems.Text.Substring(0, cboItems.Text.IndexOf("(") - 1);
}
tabControl1.SelectedIndex = tabControl1.TabPages.Count - 1;
if (cboItems.SelectedIndex > -1)
{
txt.Text = numAmount.Value.ToString() + " " + cboItems.Text + Environment.NewLine;
string result = _manu.getIngreds(cboItems.Text.Substring(0, cboItems.Text.IndexOf("(") - 1), Int32.Parse(numAmount.Value.ToString()), 1);
txt.Text += result + Environment.NewLine;
txt.Text += "Summary of ingredients:" + Environment.NewLine;
foreach (ingred ing in _manu.Summary.Values)
{
txt.Text += String.Format("{0}\t{1}" + Environment.NewLine, ing.Amount, ing.Name);
}
txt.Text += Environment.NewLine + "Total food used: " + _manu.Food.ToString() + Environment.NewLine;
}
}
else
{
MessageBox.Show("Could not find TextBox!");
}
}
private TextBox findControl(TabControl tab, string name)
{
foreach (TabPage c in tab.TabPages)
{
if (c.Controls.Count > 0)
{
foreach (Control c1 in c.Controls)
{
if (c1.Name == name)
return (TextBox)c1;
}
}
}
return null;
}
private void frmELItems_Load(object sender, EventArgs e)
{
_manu = new Manufacture(ref cboItems);
_manu.parseManufacture();
}
private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
{
_manu.readManufacture();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnUses_Click(object sender, EventArgs e)
{
TextBox txt = findControl(tabControl1, "txtResult" + (tabControl1.TabPages.Count - 1).ToString());
if (txt != null)
{
if (txt.TextLength > 0)
{
// create a new tab
tabControl1.TabPages.Add("U: " + cboItems.Text.Substring(0, cboItems.Text.IndexOf("(") - 1));
// add a new textbox to that tab
TextBox txtNew = new TextBox();
txtNew.Location = new System.Drawing.Point(3, 6);
txtNew.Multiline = true;
txtNew.Name = "txtResult" + (tabControl1.TabPages.Count - 1).ToString();
txtNew.Size = new System.Drawing.Size(733, 487);
txtNew.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
tabControl1.TabPages[tabControl1.TabPages.Count - 1].Controls.Add(txtNew);
txt = txtNew;
}
else
{
tabControl1.SelectedTab.Text = "U: " + cboItems.Text.Substring(0, cboItems.Text.IndexOf("(") - 1);
}
tabControl1.SelectedIndex = tabControl1.TabPages.Count - 1;
txt.Text = _manu.getUses(cboItems.Text);
}
tabControl1.TabPages[tabControl1.TabPages.Count - 1].Select();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (tabControl1.TabPages.Count > 1)
{
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}
else
{
TextBox txt = findControl(tabControl1.SelectedTab);
if(txt != null)
txt.Clear();
}
}
private TextBox findControl(TabPage tabPage)
{
foreach (Control c in tabPage.Controls)
{
if (c.GetType() == typeof(TextBox))
return (TextBox)c;
}
return null;
}
}
}