I'm wondering how to restrict my checkbox from adding to my listbox. At the moment when the user checks the checkbox it will add "Anchovies" to the listbox. What I don't want to happen is when the user deselects the checkbox and re selects it again, "Anchovies" is added to the listbox again (showing two lots of "Anchovies").
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
}
The key is to check if Anchovies already exists on the listBox1 items.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
//If the item is already there, we don't do anything.
if (!listBox1.Items.Contains("Anchovies")) {
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
}
}
Do it this way
if (checkBox1.Checked)
{
if(!listBox1.Items.Contains("Anchovies"))
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
To fix this issue, you need to check your list box(for this value, either it is already there or not) before inserting any value in it.
e.g
foreach (var item in listBox1.Items )
{
if(item.ToString() != "Anchovies")
{
listBox1.Items.Add("Anchovies");
}
// other code here.
}
Related
Okay, so I'm sure this has been asked before but I can't seem to find anything in relation. I'm trying to create an assignment for school. It's a lunch order menu where you can place your order. We have to calculate the subtotal, the sales tax, and the order total. I can't seem to figure out the right code to use and I'm not 100% sure on what to try here.
public partial class Form1 : Form
{
decimal subtotal = 0m;
decimal salesTax = 0m;
decimal orderTotal = 0m;
public Form1()
{
InitializeComponent();
rdoBurger.Checked = true;
rdoPizza.Checked = true;
rdoSalad.Checked = true;
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void clearTotals()
{
}
private void btnPlaceOrder_Click(object sender, EventArgs e)
{
if (sender is RadioButton)
{
clearTotals();
}
if (rdoBurger.Checked)
{
decimal subtotal = 6.95m;
subtotal = Convert.ToDecimal(lblSubtotal.Text);
}
This is what I have but it's not showing the actual subtotal, it's still blank. What am I missing here?
It's not a bad start. This is more like what I'd expect to see:
private void btnPlaceOrder_Click(object sender, EventArgs e)
{
// only ONE of these can be checked, so "else if" is used
if (rdoBurger.Checked)
{
subtotal = 6.95m;
}
else if (rdoPizza.Checked)
{
subtotal = 5.95m;
}
else if (rdoSalad.Checked)
{
subtotal = 4.95m;
}
// multiple of these could be checked, so only "if" is used
if (checkBox1.Checked)
{
subtotal = subtotal + 100.00m; // whatever this item costs
}
if (checkBox2.Checked)
{
subtotal = subtotal + 4.99m; // whatever this item costs
}
if (checkBox3.Checked)
{
subtotal = subtotal + 10.99m; // whatever this item costs
}
// compute the tax and the total:
salesTax = subtotal * 0.0775m;
orderTotal = subtotal + salesTax;
// output it to your labels/textboxes?
lblSubtotal.Text = "$" + subtotal.ToString("F2");
lblSalesTax.Text = "$" + salesTax.ToString("F2");
lblOrderTotal.Text = "$" + orderTotal.ToString("F2");
}
Radio buttons can only have a single button selected at a time in a container. In this case it appears that the GroupBox is the container. If you had several groups of radio buttons you could use GroupBox as a container and have one radio button selected in each GroupBox. So, you cannot set all the radio buttons checked property to true.
In your btnPlaceOrder_Click the sender can not posibly be a radio button. The sender is the button that was clicked to run the event code.
private void button1_Click(object sender, EventArgs e)
{
//Find the radio button that is selected
RadioButton rButton = groupBox1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked == true);
switch (rButton.Text)
{
case "Hamburger - $6.95":
subTotal = 6.95m;
break;
case "Pizza - $5.95":
subTotal = 5.95m;
break;
case "Salad - $4.95":
subTotal = 4.95m;
break;
}
//Add code to handle Add-on items
//For example - The first check box is "Add Onions" - $0.50
if (checkBox1.Checked)
subTotal += .5m;
lblSubTotal.Text = subTotal.ToString();
decimal tax = subTotal * .0775m;
lblTax.Text = tax.ToString();
decimal total = subTotal + tax;
lblTotal.Text = total.ToString();
}
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();
}
private void Form1_Load(object sender, EventArgs e){
for (int i = 0; i < listBox1.Items.Count; i++)
{
lst.Add(listBox1.Items[i].ToString());
}
foreach (var item in lst)
{
lst1.Add(item[2].ToString());
}
}
private void button1_Click(object sender, EventArgs e) {
if (lst1.Contains(textBox1.Text))
{
// *Need to find that particular item from listbox and clear rest of them*\\
}
}
my input is
1-2-3-4-5
6-7-8-9-10
1-9-4-2-3
7-8-1-4-9
so when textbox has value 7
then my listbox must show 6-7-8-9-10 as output and clear rest all items in listbox
Using what you have posted, I do not understanding what exactly you are trying to achieve. Using the two (2) List’s lst and lst1 looks very odd. Without having more information as to what your ultimate goal is I question why you would do this the way you are.
The code below removes the items in the ListBox where the second character does not match the character in the text box. Hope this helps.
private void button1_Click(object sender, EventArgs e) {
if (lst1.Contains(textBox1.Text)) {
int index = lst1.IndexOf(textBox1.Text);
string temp = listBox1.Items[index].ToString();
MessageBox.Show("Character: " + textBox1.Text + " Found at index: " + index + " the string is: " + temp);
listBox1.Items.Clear();
listBox1.Items.Add(temp);
// *Need to find that particular item from listbox and clear rest of them*\\
} else {
MessageBox.Show("Not Found");
}
}
Im trying to get the selected value of my 4 comboboxes and add them together automatically in a windows form.
The comboboxes items are decimals, 0,75 , 0,8 etc.
How do i add all the values selected from the comboboxes together into a textbox?
I have tried for 5 hours now and really cant figure it out.
FYI im really a beginner.
Thanks!
You can handle TextChanged event on all combo boxes, calculate the sum and assign the result to the text box.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var comboBox in this.Controls.OfType<ComboBox>())
{
comboBox.TextChanged += ComboBox_TextChanged;
InitializeComboBox(comboBox);
}
}
private void ComboBox_TextChanged(object sender, EventArgs e)
{
double result = 0;
foreach (var comboBox in this.Controls.OfType<ComboBox>())
{
if (!string.IsNullOrEmpty(comboBox.Text))
{
result += Convert.ToDouble(comboBox.Text);
}
}
textBox1.Text = result.ToString();
}
private void InitializeComboBox(ComboBox comboBox)
{
for (int index = 0; index < 10; index++)
{
comboBox.Items.Add(index + 0.5);
}
}
i have made a mistake of re-inventing the wheel. There are options
but somehow i like the feel of this.
Sorry but don't have enough rep to post an image.
This is how the form looks like:
SNO.-------ITEMS--------FROM--------TO---------QUANTITY // labels
[ 1 ]-------[-----------▼]---[--------]----[--------]------[-------------] {NEW} {DELETE} //textboxes and buttons
I've got the 'new' button click event to generate a row, and serial number to be automatic
and inserted the items into the collections from Properties panel.
Delete button deletes an entire row and shifts both the button up on Y position.
I need to assign the value of quantity [(TO - FROM ) + 1] in the QUANTITY text boxes,
for which i have the code as :
public void print_quant(object Sender, EventArgs e)
{
TextBox quanty;
quanty = (TextBox)this.Controls.Find("QUANTITY" + (count), true)[0];
calculate_quant(this, e);
quanty = result;
}
public static string result;
public string calculate_quant(object sender, EventArgs e)
{
TextBox sfrom;
sfrom = (TextBox)this.Controls.Find("SFRM" + count, true)[0];
TextBox sto;
sto = (TextBox)this.Controls.Find("STO" + count, true)[0];
TextBox quan;
quan = (TextBox)this.Controls.Find("QUANTITY" + count, true)[0];
//if (!string.IsNullOrEmpty(sfrom.Text) && !string.IsNullOrEmpty(sto.Text))
{
int to = Convert.ToInt32(sto.Text);
int from = Convert.ToInt32(sfrom.Text);
int quantity = (to - from) + 1;
result = quantity.ToString();
quan.Text = result;
}
return result;
}
count is initialized at 1 on form load, keeps increasing with number of rows
the same code works in the delete row method
public void delete_row(object sender, EventArgs e) //function to delete a row
{
TextBox snum;
snum = (TextBox)this.Controls.Find("SNO"+count, true)[0];
snum.Dispose();
...//delete other row elements
}
please help me figure out why it doesnt work for the print_quant / calculate_quant methods
I made some changes to your code. I changed the return on your calculate method to a string, and added a quanty.Text=calculatemethod line to your print method
public void print_quant(object Sender, EventArgs e)
{
TextBox quanty;
quanty = (TextBox)this.Controls.Find("QUANTITY" + (count), true)[0];
//add this line
quanty.Text = calculate_quant(this, e).ToString();
}
public static string result;
//change this
//public void calculate_quant(object sender, EventArgs e)
//to
public string calculate_quant(object sender, EventArgs e)
{
TextBox sfrom;
sfrom = (TextBox)this.Controls.Find("SFRM" + count, true)[0];
TextBox sto;
sto = (TextBox)this.Controls.Find("STO" + count, true)[0];
//this isn't being used here
//TextBox quan;
//quan = (TextBox)this.Controls.Find("QUANTITY" + count, true)[0];
//if (!string.IsNullOrEmpty(sfrom.Text) && !string.IsNullOrEmpty(sto.Text))
{
int to = Convert.ToInt32(sto.Text);
int from = Convert.ToInt32(sfrom.Text);
int quantity = (to - from) + 1;
return quantity.ToString();
}
}
Edit
try this.
Create a usercontrol and make it look exactly like one of your rows.
add a property variable for each of the boxes
//whenever you Sno="something" the textbox will automatically be updated.
private string _Sno="00000";
public string Sno{get{return _Sno;}set{_sno=value; SnoTextBox.Text=value;}}
do this for each of your textboxes.
on your main form now you can add a flowpanel, they a bit tricky at first. when you add your new Usercontrol to it, they will automatically be added from the top down, or up, or however you set it up.
When you want to add a new row, just add your new Usercontrol to the flowpanel
FlowPanel flowPanel =new FlowPanel();
FlowPanel.Controls.Add(new myUserControl());
to delete
FlowPanel.Controls.RemoveAt(2);
This is really poorly written, but I am out of time. Either ignore me altogether, or try to figure it out. Sorry I couldn't be more help.
this worked for me
private void textBox_TextChanged(object sender, EventArgs e)
{
TextBox quant;
int x = count - 1;
string num = Convert.ToString(x);
quant = (TextBox)this.Controls.Find("QUANTITY" + x , true)[0];
TextBox to = (TextBox)this.Controls.Find("STO" + x, true)[0];
TextBox from = (TextBox)this.Controls.Find("SFRM" + x, true)[0];
string tovalue = to.Text;
int to1 = Convert.ToInt32(tovalue);
string fromvalue = from.Text;
int from1 = Convert.ToInt32(fromvalue);
int result = (to1 - from1) + 1 ;
if (result > 0)
{
string result1 = Convert.ToString(result);
quant.Text = result1;
}
}
after adding
STO.TextChanged += new System.EventHandler(textBox_TextChanged);
at the function that was generating the boxes where i needed to calculate :)