Reputation: 314
I am new to c# and i would like to know how to count the total number of textBox to end the loop. So, for example: if there are 8 textBoxes and the user entered values in 5 out of 8, i would like to know how to count the 5 that is used and end the loop, and display a messageBox after stating how many textboxes were used.
// here's the 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 GPA_Calculator
{
public partial class FormTitle : Form
{
public FormTitle()
{
InitializeComponent();
}
private void Calculatebtn_Click(object sender, EventArgs e)
{
// Declare variables
double grade = 0.0;
double total_grade = 0.0;
double pre_gpa = 0.0;
double gpa_result = 0.0;
int class_one_textBox = 0;
int class_two_textBox = 0;
int class_three_textBox = 0;
int class_four_textBox = 0;
int class_five_textBox = 0;
#region // Checks if all fields are empty
if ((textBox1.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox1.SelectedText))
&& (textBox2.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox2.SelectedText))
&& (textBox3.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox3.SelectedText))
&& (textBox4.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox4.SelectedText))
&& (textBox5.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox5.SelectedText)))
{
MessageBox.Show("Empty Fields!"); // Display message to user if field is empty
}// end if
#endregion
**/*NEED A BETTER LOOP HERE*/**
for (double i = 0; i <= total_grade.ToString().Count(); i++)
{
#region // Class 1 Calculations
if ((comboBox1.SelectedIndex > -1) && (textBox1.Text.Trim() == string.Empty)
|| (comboBox1.SelectedIndex == -1) && (textBox1.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 1 contains an empty field!");
}
else if ((comboBox1.SelectedIndex > -1) && (textBox1.Text.Trim() != string.Empty))
{
switch (comboBox1.SelectedItem.ToString())
{
case "A":
class_one_textBox = int.Parse(textBox1.Text);
grade = 4.00 * class_one_textBox;
break;
case "A-":
class_one_textBox = int.Parse(textBox1.Text);
grade = 3.67 * class_one_textBox;
break;
case "B+":
class_one_textBox = int.Parse(textBox1.Text);
grade = 3.33 * class_one_textBox;
break;
case "B":
class_one_textBox = int.Parse(textBox1.Text);
grade = 3.00 * class_one_textBox;
break;
case "B-":
class_one_textBox = int.Parse(textBox1.Text);
grade = 2.67 * class_one_textBox;
break;
case "C+":
class_one_textBox = int.Parse(textBox1.Text);
grade = 2.33 * class_one_textBox;
break;
case "C":
class_one_textBox = int.Parse(textBox1.Text);
grade = 2.00 * class_one_textBox;
break;
case "C-":
class_one_textBox = int.Parse(textBox1.Text);
grade = 1.67 * class_one_textBox;
break;
case "D":
class_one_textBox = int.Parse(textBox1.Text);
grade = 1.33 * class_one_textBox;
break;
case "F":
class_one_textBox = int.Parse(textBox1.Text);
grade = 1.00 * class_one_textBox;
break;
} // end switch
} // end if
#endregion
#region // Class 2 Calculations
if ((comboBox2.SelectedIndex > -1) && (textBox2.Text.Trim() == string.Empty)
|| (comboBox2.SelectedIndex == -1) && (textBox2.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 2 contains an empty field!");
}
else if ((comboBox2.SelectedIndex > -1) && (textBox2.Text.Trim() != string.Empty))
{
switch (comboBox2.SelectedItem.ToString())
{
case "A":
class_two_textBox = int.Parse(textBox2.Text);
grade = 4.00 * class_two_textBox;
break;
case "A-":
class_two_textBox = int.Parse(textBox2.Text);
grade = 3.67 * class_two_textBox;
break;
case "B+":
class_two_textBox = int.Parse(textBox2.Text);
grade = 3.33 * class_two_textBox;
break;
case "B":
class_two_textBox = int.Parse(textBox2.Text);
grade = 3.00 * class_two_textBox;
break;
case "B-":
class_two_textBox = int.Parse(textBox2.Text);
grade = 2.67 * class_two_textBox;
break;
case "C+":
class_two_textBox = int.Parse(textBox2.Text);
grade = 2.33 * class_two_textBox;
break;
case "C":
class_two_textBox = int.Parse(textBox2.Text);
grade = 2.00 * class_two_textBox;
break;
case "C-":
class_two_textBox = int.Parse(textBox2.Text);
grade = 1.67 * class_two_textBox;
break;
case "D":
class_two_textBox = int.Parse(textBox2.Text);
grade = 1.33 * class_two_textBox;
break;
case "F":
class_two_textBox = int.Parse(textBox2.Text);
grade = 1.00 * class_two_textBox;
break;
} // end switch
} // end if
#endregion
#region // Class 3 Calculations
if ((comboBox3.SelectedIndex > -1) && (textBox3.Text.Trim() == string.Empty)
|| (comboBox3.SelectedIndex == -1) && (textBox3.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 3 contains an empty field!");
}
else if ((comboBox3.SelectedIndex > -1) && (textBox3.Text.Trim() != string.Empty))
{
switch (comboBox3.SelectedItem.ToString())
{
case "A":
class_three_textBox = int.Parse(textBox3.Text);
grade = 4.00 * class_three_textBox;
break;
case "A-":
class_three_textBox = int.Parse(textBox3.Text);
grade = 3.67 * class_three_textBox;
break;
case "B+":
class_three_textBox = int.Parse(textBox3.Text);
grade = 3.33 * class_three_textBox;
break;
case "B":
class_three_textBox = int.Parse(textBox3.Text);
grade = 3.00 * class_three_textBox;
break;
case "B-":
class_three_textBox = int.Parse(textBox3.Text);
grade = 2.67 * class_three_textBox;
break;
case "C+":
class_three_textBox = int.Parse(textBox3.Text);
grade = 2.33 * class_three_textBox;
break;
case "C":
class_three_textBox = int.Parse(textBox3.Text);
grade = 2.00 * class_three_textBox;
break;
case "C-":
class_three_textBox = int.Parse(textBox3.Text);
grade = 1.67 * class_three_textBox;
break;
case "D":
class_three_textBox = int.Parse(textBox3.Text);
grade = 1.33 * class_three_textBox;
break;
case "F":
class_three_textBox = int.Parse(textBox3.Text);
grade = 1.00 * class_three_textBox;
break;
} // end switch
} // end else/if
#endregion
#region // Class 4 Calculations
if ((comboBox4.SelectedIndex > -1) && (textBox4.Text.Trim() == string.Empty)
|| (comboBox4.SelectedIndex == -1) && (textBox4.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 4 contains an empty field!");
}
else if ((comboBox4.SelectedIndex > -1) && (textBox4.Text.Trim() != string.Empty))
{
switch (comboBox4.SelectedItem.ToString())
{
case "A":
class_four_textBox = int.Parse(textBox4.Text);
grade = 4.00 * class_four_textBox;
break;
case "A-":
class_four_textBox = int.Parse(textBox4.Text);
grade = 3.67 * class_four_textBox;
break;
case "B+":
class_four_textBox = int.Parse(textBox4.Text);
grade = 3.33 * class_four_textBox;
break;
case "B":
class_four_textBox = int.Parse(textBox4.Text);
grade = 3.00 * class_four_textBox;
break;
case "B-":
class_four_textBox = int.Parse(textBox4.Text);
grade = 2.67 * class_four_textBox;
break;
case "C+":
class_four_textBox = int.Parse(textBox4.Text);
grade = 2.33 * class_four_textBox;
break;
case "C":
class_four_textBox = int.Parse(textBox4.Text);
grade = 2.00 * class_four_textBox;
break;
case "C-":
class_four_textBox = int.Parse(textBox4.Text);
grade = 1.67 * class_four_textBox;
break;
case "D":
class_four_textBox = int.Parse(textBox4.Text);
grade = 1.33 * class_four_textBox;
break;
case "F":
class_four_textBox = int.Parse(textBox4.Text);
grade = 1.00 * class_four_textBox;
break;
} // end switch
} // end else if
#endregion
#region // Class 5 Calculations
if ((comboBox5.SelectedIndex > -1) && (textBox5.Text.Trim() == string.Empty)
|| (comboBox5.SelectedIndex == -1) && (textBox5.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 5 contains an empty field!");
}
else if ((comboBox5.SelectedIndex > -1) && (textBox5.Text.Trim() != string.Empty))
{
switch (comboBox5.SelectedItem.ToString())
{
case "A":
class_five_textBox = int.Parse(textBox5.Text);
grade = 4.00 * class_five_textBox;
break;
case "A-":
class_five_textBox = int.Parse(textBox5.Text);
grade = 3.67 * class_five_textBox;
break;
case "B+":
class_five_textBox = int.Parse(textBox5.Text);
grade = 3.33 * class_five_textBox;
break;
case "B":
class_five_textBox = int.Parse(textBox5.Text);
grade = 3.00 * class_five_textBox;
break;
case "B-":
class_five_textBox = int.Parse(textBox5.Text);
grade = 2.67 * class_five_textBox;
break;
case "C+":
class_five_textBox = int.Parse(textBox5.Text);
grade = 2.33 * class_five_textBox;
break;
case "C":
class_five_textBox = int.Parse(textBox5.Text);
grade = 2.00 * class_five_textBox;
break;
case "C-":
class_five_textBox = int.Parse(textBox5.Text);
grade = 1.67 * class_five_textBox;
break;
case "D":
class_five_textBox = int.Parse(textBox5.Text);
grade = 1.33 * class_five_textBox;
break;
case "F":
class_five_textBox = int.Parse(textBox5.Text);
grade = 1.00 * class_five_textBox;
break;
} // end switch
} // end else if
#endregion
total_grade = total_grade + grade;
}
// Sums up the amount of credits
pre_gpa = class_one_textBox + class_two_textBox + class_three_textBox
+ class_four_textBox + class_five_textBox;
gpa_result = total_grade / pre_gpa;
MessageBox.Show("Your GPA is " + gpa_result); // Displays GPA
Upvotes: 1
Views: 2195
Reputation: 20286
You should use this.Controls
int sum = 0;
foreach (TextBox tb in this.Controls.OfType<TextBox>())
{
if(tb.Text != string.Empty) //textbox is not empty do something
sum += int.Parse(tb.Text);
else break; // here you can detect if textbox is empty
}
You can use this code in Load event of Form. The example above couts the sum of textboxes that are not empty and breaks the loop on first empty textbox. You can modify it not to break the loop but just don't count the empty textboxes.
To find TextBox recursively check this link
Upvotes: 1
Reputation: 2686
You can use a extension method to get the textboxes in the form.
public static IEnumerable<TControl> GetChildControls(this Control control) where TControl : Control
{
var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>();
return children.SelectMany(c => GetChildControls(c)).Concat(children);
}
Get all the textboxes.
var allTextBoxes = this.GetChildControls<TextBox>();
check the value in each text box.
int sum = 0;
foreach (TextBox oTextBox in this.GetChildControls<TextBox>())
{
if(oTextBox.Text != string.Empty)
sum += 1;
}
Ref: https://stackoverflow.com/a/4863138/1537422
Upvotes: 2
Reputation: 2374
You can use
int cnt = 0;
foreach (Control c in this.Controls)
if (c is System.Windows.Forms.TextBox)
if (!string.IsNullOrEmpty(c.Text))
cnt++;
Upvotes: 0
Reputation:
int count = 0; // create counter
foreach (Control c in Controls) // start loop through all the controls in the current form
{
if(!(c is TextBox)) continue; // if the control is not a TextBox, continue the loop
TextBox t = c as TextBox; // redeclare
if(t.Text != "") // if the text in the textbox is NOT empty
{
count++; // add to the counter
}
} // end loop
if (count >= 5) // check if the counter is higher or equal than 5
{
// do stuff!
}
I hope this helps you out. I haven't run it myself yet but it should work. (I hope!)
Upvotes: 1