Multiplying two boxes while typing the 2nd operand - c#

Guys im currently making a simple Ordering form, i'm almost done i just need to validate some things, i want to make my Price and Qty multiply after i enter/change the value on Qty without pressing any buttons
for example Price = 10 Qty = Total = 10
if i put a 2 the Total should automatically change to 20 without pressing any buttons
private void btnAddOrder_Click(object sender, EventArgs e)
{
//Get the txtbox then add to dgv
int qty = int.Parse(txtPrice.Text);
int price = int.Parse(txtQty.Text);
txtTotal.Text = (qty * price).ToString();
dgvOrder.Rows.Add(txtItemCode.Text, txtDescription.Text, txtPrice.Text, txtQty.Text,txtTotal.Text);
}
right now that's my code for those 3 txtbox

You can implement TextBox.TextChanged Event for Quantity textbox.
In your Constructor:
MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );
And Then this Method:
private void TextChanged(object Sender, TextChangedEventArgs e)
{
//Do your stuff here
}

Related

Adding Values to Buttons and Get the Sum

I just wanted to add 4 values (100,200,300,400) for 4 buttons and display that in a list and get the total by pressing a another button
Here is the example
As I understand you have got four buttons where each of them has some value as text (100, 200, 300, 400), right? If any of them is clicked the value of the button is added to the list view.
Once the "sum" button is clicked all values from the list are summed and the total value is displayed.
First of all you need to create an event for each button click and add it's Text to the list.
private void button1_Click(object sender, EventArgs e)
{
**yourList**.Items.Add(button1.Text);
}
Then for the "SUM" button action you need to add all yourList values and display
private void sumButton_Click(object sender, EventArgs e)
{
int sum = 0;
foreach (ListViewItem item in listView1.Items)
{
//make sure the text is a number
if (int.TryParse(item.Text, out int result))
{
sum += result;
}
}
MessageBox.Show(sum.ToString());
}
you can try this function in button_click
public int Sum(){
int num1 = int.Parse(button1.Text);
int num2 = int.Parse(button2.Text);
int num3 = int.Parse(button3.Text);
int num4 = int.Parse(button4.Text);
int sum = num1+num2+num3+num4;
return sum;
}

How do I update my label from selection of items in my listBox1 and listBox2?

So I have 2 list boxes within my form. Listbox1 contains different types of items that have a price and Listbox2 contains how much of that item you want to purchase. How do I update my price label so when I select both options from each list box it updates the label and gives me a price. Here's an example to help you better understand.
I select the $1.50 Chocolate Chip Cookie item in my ListBox1 and in ListBox2 I select the 1 Dozen Cookie item. So I would want my priceLabel to update to $18.00. How would I do this?
As of now I have tried creating some code in the listBox1_SelectedIndexChanged method but I am returned these 3 following values... $0.00...$2.00...$4.00
Here's my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
double index = listBox1.SelectedIndex;
double index2 = listBox2.SelectedIndex;
double total = index * index2;
label9.Text = total.ToString("C");
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
const int ESTIMATED_ARRIVAL = 3;
label10.Text = monthCalendar1.SelectionStart.AddDays(ESTIMATED_ARRIVAL).ToShortDateString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
In listBox1_SelectedIndexChanged(object sender, EventArgs e) you use listBox1.SelectedIndex; and listBox2.SelectedIndex;, if you refer to ListBox.SelectedIndex Property
ListBox.SelectedIndex Property
Gets or sets the zero-based index of the currently selected item in a
ListBox.
Property Value
Int32
A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.
it just return index of selected item, so for your purpose you must get value of selected item.
I hope this code be a good guide for you:
Add handler of SelectedIndexChanged event of both list boxes to this method:
private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listBox1.SelectedIndex > -1 && this.listBox2.SelectedIndex > -1)//You can set default SelectedIndex for list boxes and remove this
{
string s1 = this.listBox1.Items[this.listBox1.SelectedIndex].ToString();
string s2 = this.listBox2.Items[this.listBox2.SelectedIndex].ToString();
//Now we extracting the number from string
//NOTE this is a simple implementation. You must change it as your needs.
//for example
//s1 = $1.50 Chocolate Chip Cookie
//s2 = 1 Dozen Cookie
int index = s1.IndexOf(' ');//get the index of first space after 1.50 (Number) in s1
s1 = s1.Substring(1, index);
index = s2.IndexOf(' ');//get the index of first space after 1 (Number) in s2
s2 = s2.Substring(0, index);
if (double.TryParse(s1, out double p1) && double.TryParse(s2, out double p2))
{
const int DOZEN = 12;
double result = p1 * (p2 * DOZEN);
//or
//remove const int DOZEN = 12; and simply
//double result = p1 * (p2 * 12);
this.label9.Text = result.ToString("C");
}
else
{
MessageBox.Show("Can not parse double values.");
}
}
}

Adding Summary Variables

I have an asp.net website with two forms. The first form contains input controls for a user to enter shipping information. The second form contains summary information. The problem I have is that when a user adds an item by pressing the addButton on the first form, they should be able to enter another item and the sum of the price of those items should be passed to the summary form, instead it just passes the price of the most current item entered after addButton is clicked. I'm just beginning asp.net so any help would be appreciated.
protected void addButton_Click(object sender, EventArgs e)
{
var dollA = new List<decimal>();
int i = 0;
for (i = 0; i < 4; i++) {
weightInteger = int.Parse(weightTextBox.Text);
quantityInteger = int.Parse(quanTextBox.Text);
priceDecimal = decimal.Parse(priceTextBox.Text);
// Calculate the current item price.
currentPriceDecimal = priceDecimal * quantityInteger;
// Format and display the current item price.
currentTextBox.Text = currentPriceDecimal.ToString("C");
// Calculate the dollar amount due.
dollarAmountDecimal += currentPriceDecimal;
dollA.Add(dollarAmountDecimal);
dollDec = dollA.Sum();
Session["Amount"] = dollDec;
}
}
Summary Form:
protected void Page_Load(object sender, EventArgs e)
{
decimal amount;
amount = Convert.ToDecimal(Session["Amount"]);
amountTextBox.Text = amount.ToString("C");
}
This seemed to work for the OP based on the comments.
protected void addButton_Click(object sender, EventArgs e)
{
if (Session["Amount"] == null)
Session["Amount"] = Decimal.Zero;
weightInteger = int.Parse(weightTextBox.Text);
quantityInteger = int.Parse(quanTextBox.Text);
priceDecimal = decimal.Parse(priceTextBox.Text);
// Calculate the current item price.
currentPriceDecimal = priceDecimal * quantityInteger;
// Format and display the current item price.
currentTextBox.Text = currentPriceDecimal.ToString("C");
// Calculate the dollar amount due.
dollarAmountDecimal += currentPriceDecimal;
Session["Amount"] = (decimal)Session["Amount"] + dollarAmountDecimal;
}

C# - refresh textbox while typing value

How can i make auto refresh textbox while typing value like this?
i tried to do the same but it did not work. i always to hit ENTER to refresh or click on up/down arrows to refresh the value
here is the code
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
try
{
double a = double.Parse(s1.Text); //textbox 1
double b = double.Parse(s2.Text); //textbox 2
double s = a * b;
resultSpeed.Text = "" + s; //s is the result
}
catch
{
MessageBox.Show("Please input the number");
}
}
Just use event KeyUp. It will trigger every time you put a symbol.
ValueChanged isn't working because it only triggers when you are done with editing - you press enter or change focus.
So basically change your event from ValueChanged to KeyUp.
I'm not posting any code because the only change will be subcribing to other event. Your function is fine, however you should change its name :)
Put your code into textbox's TextChanged Event.
Like this
private void textBox1_TextChanged(object sender, EventArgs e)
{
calculate();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
calculate();
}
private void calculate()
{
double a = 0, b = 0, demo;
if (double.TryParse(textBox1.Text, out demo))
a = double.Parse(textBox1.Text); //textbox 1
if (double.TryParse(textBox2.Text, out demo))
b = double.Parse(textBox2.Text); //textbox 2
double s = a * b;
textBox3.Text = s.ToString(); //s is the result
}

TextBox readline with button command

I'm just trying to figure out how I can enter a number into a TextBox and click a button and make the number go into another TextBox. I know I need to make a double value like price and make it equal to zero. I'm just wondering how I would make the button control the first TextBox.
private void DepositTextBox_TextChanged(object sender, EventArgs e)
{
string deposits = Console.ReadLine();
double deposit = double.Parse(deposits);
deposit += balance;
}
private void WithdrawTextBox_TextChanged(object sender, EventArgs e)
{
string withdraws = Console.ReadLine();
double withdraw = double.Parse(withdraws);
withdraw += balance;
}
This is my code but when I run it as soon as I put a number or letter in the TextBox, it says value cannot be null, parameter name: value.
Why do you speak of a Button Click but your code sample shows a TextChanged event?
Sounds like you should probably create a form-level property to the store the total balance and manipulate that.
In your ButtonClick events, Convert the textbox.Text to a numeric type and then perform the appropriate mathmatical operation on the total balance.
Then simply just display that balance property in the other textbox.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private double balance;
private void btnDeposit_Click(object sender, EventArgs e)
{
double value = Convert.ToDouble(txtDeposit.Text);
balance += value;
txtBalance.Text = balance.ToString();
}
private void btnWithdraw_Click(object sender, EventArgs e)
{
double value = Convert.ToDouble(txtWithdraw.Text);
balance -= value;
txtBalance.Text = balance.ToString();
}
}

Categories