C# Create a bill message box with items that were checked displayed - c#

There's a form with three checkboxes and a button. Upon clicking on the button a messagebox is suppose to show up and display all the items that were checked on the form with price before tax and total. The price before tax and total are taken care of, but how would I display what was checked by the user on that form in the message box that will serve as a bill.
if (checkCheeseSnackBread.Checked == true)
{
price += 10;
items += "Cheese Snack Bread - $10";
}
else
{
price -= 10;
}
Just need some guidance.

First I must say that the code you've written seems to hold a logical eror. If the checkbox isn't checked then you shouldn't do anything. In your code you subtract the item-price from the total price. This way you give a discount of 10. If they don't buy it, don't add it. That's all.
So, now you know what they've bought, you can use a StringBuilder to populate the message.
Quick and dirty:
StringBuilder builder = new StringBuilder();
builder.AppendLine("Ticket")
builder.AppendLine();
if (checkCheeseSnackBread.Checked) // == true is not needed
{
price += 10;
items += "Cheese Snack Bread - $10";
builder.AppendLine(Cheese Snack Bread - $10);
}
// Do the same for other checkboxes
// Add the totals
Messagebox.Show(builder.ToString());
There is an other way: loop through the controls on the form, if it's a checkbox ==> add the text to the StringBuilder. This way it doesn't matter how much checkboxes there are. You just have to make sure that the text-property is used to print on the ticket (messagebox).

If you just want to display a string you generate.
Check this out:
http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx

Related

Using Switch Case with Radio Buttons and Appending Result to a List Box

I am pretty new to programming and have a C# Assignment for school. I have been searching for a couple hours now and I can't find a similar case that seems to address what I am trying to accomplish.
I have a windows form with 5 radio buttons. My assignment is to create a sales calculator using a switch statement to determine which radio button has been selected and based on the radio button selected get a sales total by multiplying the quantity of items sold by the price of each item and finally appending the result to a list box in dollar format.
Product 1 is $3.99 ea. and there are 5 products total.
Product 2 is $1.40 ea. etc. etc.
The first radio button is named RdbProduct1 and the Text for it is Product 1
I have this block of code. C# did not find any syntax errors, but the result is not being appended to the list box. Any help with this is appreciated, thank you.
private void BtnCalculate_Click(object sender, EventArgs e)
{
// Declare quantity variable convertted from Quantity text box
decimal quantity = Convert.ToDecimal(TxtQuanity.Text);
//Determine Product price using a Switch
//Calculate total sale by multiplying product price by quantity
//append result to list box
RadioButton radioBtn = this.Controls.OfType<RadioButton>()
.Where(x => x.Checked).FirstOrDefault();
if (radioBtn != null)
{
switch (radioBtn.Name)
{
case "Product 1":
decimal cost = 3.99m;
decimal subtotal = quantity * cost;
LstSuccess.Items.Add (subtotal.ToString("c"));
break;
}
}
}
To sum up the comments of the OP getting to the correct answer:
Solution is changing
switch (radioBtn.Name)
To
switch (radioBtn.Text)
Name is the name of the Control. Text is (in this case) what is displayed.

How do I calculate values from multiple textboxes and display in seperate box? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I got 4 textboxes that I want to put values in, and I want it to find the average number from the values of the inputted values to the 4(or more) textboxes.
I want the average to display in a readonly box (is richtextbox good for this?).
You can use both a RichTextBox and normal TextBox for this. To ensure that it is read-only, in the designer page do the following;
Select the TextBox > Scroll under properties window > Behavior Section > Read-Only property
Setting this property to true will make the TextBox non-editable by the user.
After you have the 4 editable TextBoxes and 1 non-editable, you can implement something like the following to add up the numeric values of TextBoxes and display it in the readonly TextBox.
private void AverageAndDisplay()
{
try
{
//Convert values to numeric
decimal one = Convert.ToDecimal(textBox1.Text);
decimal two = Convert.ToDecimal(textBox2.Text);
decimal three = Convert.ToDecimal(textBox3.Text);
decimal four = Convert.ToDecimal(textBox4.Text);
//Find the average
decimal average = (one + two + three + four) / 4.0m;
//Show the average, after formatting the number as a two decimal string representation
textBox5.Text = string.Format("{0:0.00}", average);
}
catch(Exception e) //Converting to a number from a string can causes errors
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
So you should have this steps:
Create 4 textboxes. Let's this buttons has ID Value1Txt, Value2Txt, Value3Txt, Value4Txt
Add button with text Calculate Average Value. The ID of the button should be CalculateAverageBtn
Add the Label in which you will show the Average. The ID of this Label should be AverageValueLbl
Attach OnClick event on the CalculateAverageBtn. You could do it using the signature of the button OnClick="CalculateAverageBtn_Click" or doing it in the code
//this should be inside InitializeComponents method
CalculateAverageBtn.OnClick += CalculateAverageBtn_Click;
protected void CalculateAverageBtn_Click(object sender, EventArgs e)
{
//...code
}
Now in the body of CalculateAverageBtn_Click you should Parse the values of the TextBoxes and calculate the average. You could do this using decimal.TryParse method
decimal value1 = 0;
if(!decimal.TryParse(Value1Txt.Text, out value1)
{
//if you come in this if the parsing of the value is not successful so
you need to show error message to the user. This happen when the user
enters text like 123Test, this is not a number. So you need to show
error message
}
Create a label in which you will show the error message. Let's call it ErrorLbl. So when the parsing is not successful we will write in this label the error message.
if(!decimal.TryParse(Value1Txt.Text, out value1)
{
ErrorLbl.Text = "The value of value 1 textbox is not valid number"
return; //exit the button click method
}
Calculate the average of 4 textboxes and write it in the AverageValueLbl. This should be in button event click
AverageValueLbl.Text = (value1+value2+value3+valu4)/4.ToString();
Try to understand what you are doing, don't copy/paste mindlessly code. This is pretty much beginner programming. For sure this is homework, so try to understand it because in the future harder homeworks you will be lost.

checking if the user input has changed from the previous input

Hi I am new to programming
I have a masked textbox into which a user inputs an account number, then I have a label which displays the number of users or rather display the number of times the account number has been changed. I.e. as each account number is being entered the customer count should increase.
I don't have an example code because I do not even know where to start
Please show me how to code this, I am using a form in Visual Studio
have it add the input to a list or array, and then you can run a check to see if the array/list contains that input already. If so, do not add the input again, if it does not, then add the input. You can then tell how many customers you have by the size of the list.
If you are taking in the users input as a string do a String Comparison or if you're doing strictly a numerical account number and am taking it in as an int then simply see if the numbers are different
For strings use:
result = input1.Equals(input2, StringComparison.OrdinalIgnoreCase);
For ints just use an If statement to test:
if(input1 != input2) {
Console.WriteLine("Input has changed")
else
Console.WriteLine("Input has not changed");
}
I'm guessing the user has to push a button after the user has entered the password? If so, we can easily track this. First we make a global int variable like so: private int userCount = 0;
Then we add an event to our button:
private void btnAccountNumber_Click(object sender, EventArgs e)
{
userCount = userCount + 1;
displayLabel.Text = userCount.ToString() + " Customers";
maskedTextBox.Clear();
}
So in this button we add our customer that just clicked on our button to the total of users.
Next we show that number in the label you created. And finally we clear the maskedTextBox of userInput so the next customer can use it.

How to deduct total amount when deleted on the datagridview

So I have a school program which is the ordering system.
I have 3 buttons namely add order, delete order, update order.
I need some help on the subtraction part
in which the selected rows and the cell where column name Amount belong will be deducted from the txtTotalAmount
I already did the addition part like when add order button clicked total Amount textbox sum it
Here's the code:
int totalx = 0;
int Totalxr = int.Parse(txtTotal.Text);
totalx += Totalxr;
txtTotalAmount.Text = totalx.ToString();
I tried the subtraction part but I failed to do it.
Here's my code
int Totalxr = int.Parse(txtTotal.Text);
int txrs = int.Parse(txtTotalAmount.Text);
int str = txrs - Totalxr;
txtTotalAmount.Text = str.ToString();
clarification of what I want to achieved.
Example the total would be 7300.
if selected row has an amount of 4300 which is shown in the image above then totalamount textbox should update when it is deleted and making the total amount of 3000. then again if user again selects the row with 3000 under the column amount the totalamount will be updated from 3000 to 0 since the selected row amount has 3000 and the current total is 3000.
Hope I already clarify what I wanted to do.
Lastly Why I can't remove the txtAmount.textwhich I will be doing after add order is successful
like when I used txtAmount.Clear(); the text is still there. while the other txts which I use the clear function works.

How To add all the element in textboxes into an array?

Actually I'm facing problems with my textboxes and just to say that its my first Visual C# Application i do.
I just have many textboxes, around 14 textboxes and all i want is that after the user enter the values in each on i need this value to be saved in an array Temp[]
for example (
textbox1.text=1 so Temp[0]=1
textbox2.text=4 so Temp[1]=4
and etc....
I've included also a pic for my app:
so as you can see i the user must enter values for the first set and another values for the second set and i need to save the entered values in each set in an array to show later the union and the intersection of the sets.
Try this.
For 1st panel
int a=0;
foreach (TextBox tbx in Panel1.Controls.OfType<TextBox>())
{
Temp1[a]= tbx.text ;
a++;
}
For 2nd Panel
a=0;
foreach (TextBox tbx in Panel2.Controls.OfType<TextBox>())
{
Temp2[a]= tbx.text ;
a++;
}
Edit: You have to define 1st set of textboxes in panel1 and 2nd set in panel2.

Categories