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;
}
Related
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.");
}
}
}
I have 3 button. There are prev, next and add. i have a text file with 6 lines.
So as the form load, it's only display 3 lines ascending text, the other 3 lines is appear when i click next button. But i don't know how to make it appear.
This is my next button code
private void next_Click(object sender, EventArgs e)
{
string[] baca;
baca = System.IO.File.ReadAllLines(#path.Text);
nama.Text = baca[3];
npm.Text = baca[4];
alamat.Text = baca[5];
}
I want it to display another next lines with only 1 next button.
You need to store the current line in a variable so that when you click in buttons you can use this variable as reference.
Another thing, instead of setting the fixed indexes set a var as code below.
Ps: If you are using web plataform storage the current item in a hidden field.
private int i = 0; // or in hidden field
private void next_Click(object sender, EventArgs e)
{
string[] baca;
baca = System.IO.File.ReadAllLines(#path.Text);
nama.Text = baca[i];
npm.Text = baca[i+1];
alamat.Text = baca[i+2];
}
The answer has already been stated by #Plutonix, and I quote "Read the data once and use a form level var to index what to show."
int firstIndex = 0;
var baca = System.IO.File.ReadAllLines(#path.Text).ToList();
private void next_Click(object sender, EventArgs e)
{
firstIndex++;
nama.Text = baca[firstIndex];
npm.Text = baca[firstIndex + 1];
alamat.Text = baca[firstIndex + 2];
}
However, if you don't want a form level variable for some reason this would work:
private void next_Click(object sender, EventArgs e)
{
var baca = System.IO.File.ReadAllLines(#path.Text).ToList();
int firstIndex = 1 + baca.FindIndex(nama.Text);
nama.Text = baca[firstIndex];
npm.Text = baca[firstIndex + 1];
alamat.Text = baca[firstIndex + 2];
}
Remember, this is not the best way, and will not work if there are duplicates.
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
}
private void submitbutton_Click(object sender, EventArgs e)
{
if (fishingrodcomboBox.SelectedIndex == 2)
{
fishingrodcomboBox.SelectedIndex = 2;
decimal two = 18m;
decimal price = two * fishingrodnumericUpDown.Value;
totalfishingrodtextBox.Text = price.ToString("C");
}
else
{
//Do something
}
}
Textbox doesnt display the amount and how i do code it such that when i select something from the combo box i can each a different value?
What I need to do is calculated the value of one field in the grid, based on the values of other fields in the grid. I need to run this calculation After the value in one of the dependent cells is changed, but only if the value was a Valid entry. The EditValueChanged, Validating, and Validated events of the editor/repository all occur before the data is posted back into the datasource. I am wondering if there is any event I can hook into that will allow me to fire this calculation after the data has been post back into the datasource, but before control is returned to the user.
Sample Code
//calculation functions
private void SetCalcROP(MyObjectt Row)
{
//rop = m/hr
TimeSpan ts = Row.ToTime - Row.FromTime;
double diffDepth = Row.EndDepth - Row.StartDepth;
if (ts.TotalHours > 0)//donot divide by 0
Row.ROP = diffDepth / ts.TotalHours;
else
Row.ROP = 0;
}
private void SetCalcDeltaP(MyObject Row)
{
Row.DeltaPress = Row.SPPOnBtm - Row.SPPOffBtm;
}
//events
private void repNumberInput_Validated(object sender, EventArgs e) //is actaully ActiveEditor_Validated
{
if (vwDDJournal.FocusedColumn.Equals(colSPPOff) || vwDDJournal.FocusedColumn.Equals(colSPPOn))
SetCalcDeltaP(vwDDJournal.GetFocusedRow() as MyObject);
}
private void repNumberInput_NoNulls_Validated(object sender, EventArgs e) //is actaully ActiveEditor_Validated
{
if (vwDDJournal.FocusedColumn.Equals(colStartDepth) || vwDDJournal.FocusedColumn.Equals(colEndDepth))
SetCalcROP(vwDDJournal.GetFocusedRow() as MyObject);
}
private void repTimeEdit_Validated(object sender, EventArgs e) //is actaully ActiveEditor_Validated
{
SetCalcROP(vwDDJournal.GetFocusedRow() as MyObject);
}
private void repNumberInput_NoNulls_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
TextEdit TE = sender as TextEdit;
//null is not valid for this entry;
if (string.IsNullOrEmpty(TE.Text))
{
e.Cancel = true;
vwDDJournal.SetColumnError(vwDDJournal.FocusedColumn, "This Column may not be blank");
return;
}
else
{
double tmp;
if (!Double.TryParse(TE.Text, out tmp))
{
e.Cancel = true;
vwDDJournal.SetColumnError(vwDDJournal.FocusedColumn, "This Column must contain a number");
return;
}
}
}
private void repNumberInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
TextEdit TE = sender as TextEdit;
//null is not valid for this entry;
if (!string.IsNullOrEmpty(TE.Text))
{
double tmp;
if (!Double.TryParse(TE.Text, out tmp))
{
e.Cancel = true;
vwDDJournal.SetColumnError(vwDDJournal.FocusedColumn, "This Column must contain a number");
return;
}
}
}
private void repTimeEdit_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (vwDDJournal.FocusedColumn.Equals(colToTime))
{//dont bother to check from time
//TIME TRAVEL CHECK!!!!
DateTime FromTime = Convert.ToDateTime(vwDDJournal.GetRowCellValue(vwDDJournal.FocusedRowHandle, colFromTime));
TimeEdit te = sender as TimeEdit;
DateTime ToTime = Convert.ToDateTime(te.EditValue);
if (ToTime < FromTime)
{//TIME TRAVEL
e.Cancel = true;
vwDDJournal.SetColumnError(vwDDJournal.FocusedColumn, "To Time must be greater than From Time");
return;
}
}
}
the problem is that everywhere I call this from, and whether I use vwDDJournal.GetRowCellValue(...) or vwDDJournal.GetFocusedRow() as MyObject, I still get the old edit value.
Requirements
I have to have the input validated before running the calculation.
I have to run the calculation immediately after making the change.
... What I need to do is calculated the value of one field in the grid, based on the values of other fields in the grid.
The best way to accomplish this task is using Unbound Columns feature.
The following example demonstrates how to implement this feature by handling the ColumnView.CustomUnboundColumnData event:
// Provides data for the Total column.
void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) {
if (e.Column.FieldName == "Total" && e.IsGetData) e.Value =
getTotalValue(e.ListSourceRowIndex);
}
// Returns the total amount for a specific row.
decimal getTotalValue(int listSourceRowIndex) {
DataRow row = nwindDataSet.Tables["Order Details"].Rows[listSourceRowIndex];
decimal unitPrice = Convert.ToDecimal(row["UnitPrice"]);
decimal quantity = Convert.ToDecimal(row["Quantity"]);
decimal discount = Convert.ToDecimal(row["Discount"]);
return unitPrice * quantity * (1 - discount);
}
Original example: How to: Add an Unbound Column Storing Arbitrary Data
You can also implement calculated value for unbound column using expressions:
GridColumn columnTotal = new GridColumn();
columnTotal.FieldName = "Total";
columnTotal.Caption = "Total";
columnTotal.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
columnTotal.UnboundExpression = "[Quantity] * [UnitPrice] * (1 - [Discount])";
gridView1.Columns.Add(columnTotal);
How about CustomCellValue?
After posting back to the data source refresh the data.
It's called whenever data is updated or view is changed.