Min and max button and label - c#

I'm trying to build a exam grader using C#. I'm new to this and don't know very much. What code would I use to add min and max buttons and to add a label stating whether it's a min or max?
private void btnAdd_Click(object sender, EventArgs e)
{
int points;
try
{
points = int.Parse(txtPoints.Text);
lstPoints.Items.Add(points);
txtPoints.Clear();
txtPoints.Focus();
if (lstPoints.Items.Count == 12)
{
txtPoints.Enabled = false;
btnAdd.Enabled = false;
}
if (lblResult.Text != "")
{
lblResult.Text = "";
}
}
catch
{
MessageBox.Show("Please enter only whole numbers");
txtPoints.Clear();
txtPoints.Focus();
}
}
private void btnAvg_Click(object sender, EventArgs e)
{
double total = 0;
for (int i = 0; i < lstPoints.Items.Count; i++)
{
total += (int)lstPoints.Items[i];
}
total /= lstPoints.Items.Count;
lblResult.Text = total.ToString();
}
private void btnClear_Click(object sender, EventArgs e)
{
lstPoints.Items.Clear();
txtPoints.Enabled = true;
btnAdd.Enabled = true;
}
}
}

hope this works
private void getMax()
{
int max=0;
for (int i = 0; i < lstPoints.Items.Count; i++)
{
if(max<(int)lstPoints.Items[i])
{
max=(int)lstPoints.Items[i];
}
}
lblResult.Text = max.ToString();
}
}
private void getMin()
{
int min=(int)lstPoints.Items[0];
for (int i = 1; i < lstPoints.Items.Count; i++)
{
if(min>(int)lstPoints.Items[i])
{
min=(int)lstPoints.Items[i];
}
}
lblResult.Text = min.ToString();
}
}

There are two possiblities as I see:
1) When you are writing this:
lstPoints.Items.Add(points);
Instead of adding to List(Of Integer) use SortedList. So the
list will always have the sorted result sets.
2) Use Array.Sort() to sort the records.
Once you have sorted records the first one is the minimum and the last one is the maximum (Assuming sorted in ascending order).
Take out two buttons and placed on the form, set Text Property from property window to Min and Max respectively and in event handler handle the Click event and pick the relevant resultset from lstPoints array.
Hope it helps!

Related

Handle user number input value condition with ReadOnly false of numericUpDown

In C# WinForms desktop application I use 2 interdependent numericUpDown1 min and numericUpDown2 max value numericUpDown controls:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value <= numericUpDown2.Value)
{
min = (int)numericUpDown1.Value;
}
else
{
numericUpDown1.Value = min - 1;
}
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown2.Value >= numericUpDown1.Value)
{
max = (int)numericUpDown2.Value;
}
else
{
numericUpDown2.Value = max + 1;
}
}
with using of ReadOnly = true; to avoid making the maximal number less than minimal manually from numericUpDown input.:
min = 20;
max = 1999;
numericUpDown1.Value = min;
numericUpDown2.Value = max;
numericUpDown1.ReadOnly = true;
numericUpDown2.ReadOnly = true;
numericUpDown1.Increment = 1;
numericUpDown2.Increment = 1;
numericUpDown1.Maximum = 2000;
numericUpDown1.Minimum = 1;
numericUpDown2.Maximum = 2000;
numericUpDown2.Minimum = 1;
but I use a big range from 1 to 2000, and want to allow the user to change the number of numericUpDown manually with ReadOnly = false;.
I'm trying to figure out, how to control the user input condition with ReadOnly = false; of numericUpDown to avoid the input of maximal number less than minimal or minimal bigger then maximal.
Try this solution:
First of all Set your numericUpDown property ReadOnly=false or write a line of code in your FormLoad_Function,
numericUpDown1.ReadOnly = false;
numericUpDown2.ReadOnly = false;
Then
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value > numericUpDown2.Value)
{
numericUpDown1.Value = numericUpDown1.Value - 1;
MessageBox.Show("Min value always less then Max value");
}
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value > numericUpDown2.Value)
{
numericUpDown2.Value = numericUpDown2.Value + 1;
MessageBox.Show("Max value always greater then Min value");
}
}

Assigning unique values to the index of an array

I am trying to make my button take input from a textbox and add it to the index. The problem is that with everything I have tried, I cannot get it to give a unique value to each position in the index.
private void addBtn_Click(object sender, EventArgs e)
{
for (int i = 0; i < nums.Length; i++)
{
if (nums[0] == 0)
{
nums[0] = int.Parse(inputText.Text);
i++;
}
if (nums[1] == 0)
{
nums[1] = int.Parse(inputText.Text);
i++;
}
}
MessageBox.Show(nums[i].ToString());
}
Right now my code inserts the value to both index positions instead of assigning a value to position 0 and then allowing the user to insert a different value into position 1 and so on and so on.
It's not clear what your intent is, but it looks like you might be trying to add numbers to an array. This code will assign the parsed string to the first item in the array that isn't zero.
private void addBtn_Click(object sender, EventArgs e)
{
int i = 0;
for (; i < nums.Length; i++)
{
if (nums[i] == 0)
{
nums[i] = int.Parse(inputText.Text);
break;
}
}
MessageBox.Show(nums[i].ToString());
}
But this would be a better way, because the user might type "0":
private int _lastUsedIndex = -1;
private void addBtn_Click(object sender, EventArgs e)
{
var number = int.Parse(inputText.Text);
// Increment by one
++_lastUsedIndex;
nums[_lastUsedIndex] = number;
MessageBox.Show(number.ToString());
}
But still, arrays aren't a great idea: They can't grow as you add things. First they're bigger than you need, then suddenly they're too small and you crash. We have better options now. Unless your teacher insists that you must use an array, use List<int> instead. In this version, we'll also use a different way to parse the number, which won't crash if the user types "LOLWUT?!?!" instead of a number:
private List<int> nums = new List<int>();
private void addBtn_Click(object sender, EventArgs e)
{
int number;
if (int.TryParse(inputText.Text, out number))
{
nums.Add(number);
MessageBox.Show(number.ToString());
}
else
{
MessageBox.Show(inputText.Text + " isn't a number, smart guy.");
}
}

c# how can i get my listbox item and add it quickly when i pressed checklistboxes

Hello I am still confuse to this how can I get my listbox items and when I click every checklistboxes and I want to add the numbers and display in the textbox. for example I check the checklistbox index 1 which contains 300 it display in the textbox. Then I check also my index 2 of checkboxlist contains 100 then it display 400. Then if I check my index 3 of my checkboxlist contains 200 it display 600 in the checkbox.
My code:
namespace ggg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox2.Items.Clear();
textBox1.Clear();
foreach (string s in checkedListBox1.CheckedItems)
listBox1.Items.Add(s);
foreach (int i in checkedListBox1.CheckedIndices)
{
if (i == 0)
{
listBox2.Items.Add(300);
decimal total = 300;
textBox1.Text += total;
}
if (i == 1)
{
listBox2.Items.Add(100);
decimal total = 100;
textBox1.Text += total;
}
if (i == 2)
{
listBox2.Items.Add(200);
decimal total = 200;
textBox1.Text += total;
}
}
}
}
}
you can sum listbox items as this . After , you can set the total to textbox
int total = 0;
for (int i = 0; i < listBox2.Items.Count; i++)
{
total = total+ int.Parse(listBox2.Items[i].ToString());
}
textBox1.Text = total.ToString();
This code might get your job done. There are still better ways to do what you are looking for. But, this can be a good solution too! Considering your textbox name as myTextBox:
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int sum = 0; // Add a variable to capture the sum
listBox1.Items.Clear();
listBox2.Items.Clear();
textBox1.Clear();
foreach (string s in checkedListBox1.CheckedItems)
listBox1.Items.Add(s);
foreach (int i in checkedListBox1.CheckedIndices)
{
if (i == 0)
{
listBox2.Items.Add(300);
sum += 300; // Add the value to sum
}
if (i == 1)
{
listBox2.Items.Add(100);
sum += 100; // Add the value to sum
}
if (i == 2)
{
listBox2.Items.Add(200);
sum += 200; // Add the value to sum
}
}
// Finally, show the sum in text box
myTextBox.Text = sum.ToString();
}

Don't let me add in a listbox more items than shown in the combobox

I have a combobox with the items
1
2
3
...
40
,if I chose the value 4 then I should be able to add in my listbox no more than 4 values.This is what i was thinking of but isn't working.
public Form1()
{
InitializeComponent();
}
private void add_Click(object sender, EventArgs e)
{
int allowedItemsCount = 0;
Int32.TryParse(comboBox1.SelectedText, out allowedItemsCount);
int currentItemsCount = listBox1.Items.Count;
if (currentItemsCount < allowedItemsCount)
{
listBox1.Items.Add(textBox1.Text);
}
}
private void delete_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count != 0)
{
while (listBox1.SelectedIndex != -1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 0;
Int32.TryParse(comboBox1.SelectedText, out x);
int count = listBox1.Items.Count;
if (count > x)
{
listBox1.Items.Clear();
int difference = count - x;
for (int i = 0; i < difference; i++)
{
listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
}
}
}
}
Here is the full code you asked for but is not working...now the add button is not working.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 0;
Int32.TryParse(comboBox1.SelectedText, out x);
int count = listBox1.Items.Count;
if (count > x)
{
listBox1.Items.Clear();
int difference = count - x;
for(int i = 0 ; i < difference ; i++)
{
listBox1.Items.RemoveAt(listBox1.Items.Count-1);
}
}
}
Update
As per your comment, write this code in your add button click event
int allowedItemsCount = 0;
Int32.TryParse(comboBox1.SelectedText, out allowedItemsCount);
int currentItemsCount = listBox1.Items.Count;
if(currentItemsCount < allowedItemsCount)
{
listBox1.Items.Add(textBox1.Text); // I assume your textbox id is TextBox1
}
For removing extra items:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.TrimExcess(Int32.Parse(comboBox1.SelectedText));
//If you control the items on the combo so you know for sure the parse will work, ignore the tryparse
}
And for adding items to the ListBox (and control it does not have to much items:
private int addToListBox(object item)
{
if(listbox1.items.count>=Int32.Parse(comboBox1.SelectedText)) return -1;
listbox1.items.add(item);
return 0;
}
And when you need to add to that listbox use addToListBoxand not listbox1.items.add
This will check if you can add line to listbox1 and if its changed number of line it will delete it.
Adding is made by button.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 0;
Int32.TryParse(comboBox1.SelectedItem.ToString(), out x);
int count = listBox1.Items.Count;
while(count > x){
listBox1.Items.RemoveAt(count - 1);
count = listBox1.Items.Count;
}
}
private void button2_Click(object sender, EventArgs e)
{
int x = 0;
Int32.TryParse(comboBox1.SelectedItem.ToString(), out x);
if (listBox1.Items.Count < x)
{
listBox1.Items.Add(x); //add whatever you want
}
}

Find the highest number in array , using while or for?

Please a i have a Questions , I need find the higghest value in array. To the array will people write name (textbox1) and money (texbox2). I have 2 buttons first button is save to the array and second write the name with the biggest money.
Code for save:
string[] name = new string[50];
int items = 0;
int[] money = new int[50];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Convert.ToInt32(textBox2.Text);
}
catch
{
name[items] = textBox1.Text;
money[items] = Int32.Parse(textBox2.Text);
items++;
}
}
And to the button2 need search the biggest value and write name! Please help me
private void button2_Click(object sender, EventArgs e)
{
int maxIndex = 0;
for(int i = 0; i < 50; i++)
{
if (money[i] > money[maxIndex])
maxIndex = i;
}
MessageBox.Show(name[maxIndex] + " has biggest value " + money[maxIndex]);
}
To get the Max int from your array you can use IEnumerable.Max:
money.Max();
But there could be more than one name with the same high money value, perhaps you need to handle this also, I think Dictionary would be your best option
private Dictionary<string, int> Names = new Dictionary<string, int>();
private void button1_Click(object sender, EventArgs e)
{
int value = 0;
if (int.TryParse(textBox2.Text, out value))
{
if (!Names.ContainsKey(textBox1.Text))
{
Names.Add(textBox1.Text, value);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (Names.Any())
{
int maxMoney = Names.Max(v => v.Value);
var names = Names.Where(k => k.Value.Equals(maxMoney));
foreach (var name in names)
{
// Names with the highest money value
}
}
}

Categories

Resources