Adding values from textbox to label [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wanted to know how I would be able to keep adding the values from the textbox to a label, without the label resetting to zero every time I add another Pizza
I basically need code showing me how to add keep adding text box value to a label
private void SummaryBox_TextChanged(object sender, EventArgs e)//Textbox:the value is shown of the pizza selected
{
}
private void txtPizzaPrice_TextChanged(object sender, EventArgs e)
{
}
Menu(object sender, System.ComponentModel.CancelEventArgs e)
{
private void lblPrice_Click(object sender, EventArgs e)
{
// Label where the Pizza prices needs to be added up each time I press add pizza
}
}
}
//HERE ARE THE PIZZA VALUES
double PizzaPrice;//Global
double ExtraTopping;//Global
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
string CT;
if (radioButton2.Enabled == true) //PIZZA CHEESE TOMATO
{
double ctp = 3.50;
PizzaPrice = ctp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice
CT = radioButton2.Text;
SummaryBox.Text = CT;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
string VS;
if (radioButton1.Enabled == true) //PIZZA Veg SUpreme
{
double vsp = 5.20;
PizzaPrice = vsp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice
VS = radioButton1.Text;
SummaryBox.Text = VS;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton3_CheckedChanged_1(object sender, EventArgs e)
{
string SV;
if (radioButton3.Enabled == true) //PIZZA SPicy Veg
{
double svp = 5.20;
PizzaPrice = svp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice
SV = radioButton3.Text;
SummaryBox.Text = SV;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton6_CheckedChanged(object sender, EventArgs e)
{
string MF;
if (radioButton6.Enabled == true) //PIZZA Veg SUpreme
{
double mfp = 5.80;
PizzaPrice = mfp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice
MF = radioButton6.Text;
SummaryBox.Text = MF;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton7_CheckedChanged(object sender, EventArgs e)
{
string HP;
if (radioButton7.Enabled == true) //PIZZA Ham pineapple
{
double hpp = 4.20;
PizzaPrice = hpp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
^ Value I want to add to lblPrice^
HP = radioButton7.Text;
SummaryBox.Text = HP;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
string SF;
if (radioButton4.Enabled == true) // PIZZA Veg SUpreme
{
double sfp = 5.60;
PizzaPrice = sfp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice
SF = radioButton4.Text;
SummaryBox.Text = SF;
}
else
{
SummaryBox.Clear();
}
}

I believe what you are trying to do is to add a value to PizzaPrice, then display it on txtPizzaPrice.Text with the £ sign appended to the front.
PizzaPrice should be a property rather than a field.
public double PizzaPrice { get; set; }
Notice that I += the value to pizza price, then display it on the text property:
private void radioButton7_CheckedChanged(object sender, EventArgs e)
{
if (radioButton7.Checked)
{
double hpp = 4.20;
PizzaPrice += hpp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
SummaryBox.Text = radioButton7.Text;
}
else
{
SummaryBox.Clear();
}
}
You could do a lot to shorten your code. Try adding the pizza type to the Tag of the radio button, and using a struct for your pizza values. But I'll leave that to you.

Related

Maximum value in the track bar

I have a button that adds +100 to the track Bar.
Maximum value 43000, if the value is at 43000 and clicking the button will give error.
Value '43001' is not valid for 'Value'. 'Value' must be between 'Minimum' and 'Maximum'.
private void button41_Click(object sender, EventArgs e)
{
trackBar1.Value = trackBar1.Value += 100;
label27.Text = "" + trackBar1.Value;
}
Issue resolved:
public Form1()
{
me = this;
InitializeComponent();
trackBar1.Maximum = 43000;
trackBar1.Minimum = 40;
}
button
private void button41_Click(object sender, EventArgs e)
{
if (trackBar1.Value + 100 <= trackBar1.Maximum)
{
trackBar1.Value = trackBar1.Value += 100;
label27.Text = "Frequency = " + trackBar1.Value;
}
else
{
MessageBox.Show("Max value = " + trackBar1.Maximum);
}
}
42990 + 100 without errors if I click add
Message displayed when trying add more than the supported value
The message already says all: The value may not be greater than the max-value.
Just add a condition before you increment the value:
if (trackBar1.Value < trackBar1.Maximum)
trackBar1.Value++;
Or here your complete event handler:
private void button41_Click(object sender, EventArgs e)
{
if (trackBar1.Value < trackBar1.Maximum)
{
trackBar1.Value++;
label27.Text = trackBar1.Value;
}
else
{
MessageBox.Show("Max value = " + trackBar1.Maximum);
}
}

How to continually add data to a label until I press a specific button in Visual C#?

I made a simple calculator and it works perfectly. I've added a textbox and a label. textbox shows the result while the label shows the current operation. I want to keep showing values that I type in the label until i press the equal button to get the answer. for example I add 1 + 2 + 3
the label should show --> 1 + 2 +3
the textbox should show --> 6 as the final answer.
Here is my code.
namespace My_First_Calculator
{
public partial class Form1 : Form
{
Double resultVal = 0;
String operationPerformed = "";
bool isOperationPerformed = false;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button_Click(object sender, EventArgs e) //common method to other number buttons
{
if (textBox_Result.Text == "0" || isOperationPerformed)
textBox_Result.Clear();
isOperationPerformed = false;
Button button = (Button)sender;
if(button.Text == ".")
{
if(!textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;
}
else
textBox_Result.Text = textBox_Result.Text + button.Text;
}
private void operator_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultVal != 0)
{
buttonEqual.PerformClick();
operationPerformed = button.Text;
labelCurrentOpration.Text = labelCurrentOpration.Text + resultVal + " " + operationPerformed;
isOperationPerformed = true;
}
else
{
operationPerformed = button.Text;
resultVal = Double.Parse(textBox_Result.Text);
labelCurrentOpration.Text = labelCurrentOpration.Text + resultVal + " " + operationPerformed;
isOperationPerformed = true;
}
}
private void button18_Click(object sender, EventArgs e) //buttonClaerAll
{
textBox_Result.Text = "0";
resultVal = 0;
labelCurrentOpration.Text = "";
}
private void button17_Click(object sender, EventArgs e) //buttonClear
{
textBox_Result.Text = "0";
//resultVal = 0;
}
private void button16_Click(object sender, EventArgs e) //buttonEqual
{
switch(operationPerformed)
{
case "+":
textBox_Result.Text = (resultVal + Double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
textBox_Result.Text = (resultVal - Double.Parse(textBox_Result.Text)).ToString();
break;
case "*":
textBox_Result.Text = (resultVal * Double.Parse(textBox_Result.Text)).ToString();
break;
case "/":
textBox_Result.Text = (resultVal / Double.Parse(textBox_Result.Text)).ToString();
break;
default: break;
}
resultVal = Double.Parse(textBox_Result.Text);
labelCurrentOpration.Text = "";
}
}
You need to append the new string to the existing text in the label.
private void button_Click(object sender, EventArgs e)
{
if (textBox_Result.Text == "0" || isOperationPerformed)
textBox_Result.Clear();
isOperationPerformed = false;
Button button = (Button)sender;
if(button.Text == ".")
{
if(!textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;
}
else
textBox_Result.Text = textBox_Result.Text + button.Text;
labelCurrentOperation.Text = labelCurrentOperation.Text + " " + button.Text;
}
private void operator_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultVal != 0)
{
button16.PerformClick();
operationPerformed = button.Text;
labelCurrentOpration.Text = labelCurrentOpration.Text + " " + resultVal + " " + operationPerformed;
isOperationPerformed = true;
}
else
{
operationPerformed = button.Text;
resultVal = Double.Parse(textBox_Result.Text);
labelCurrentOpration.Text = labelCurrentOpration.Text + " " + resultVal + " " + operationPerformed;
isOperationPerformed = true;
}
}

Reading data from the DataGridView just reads the last row c#

I already could show the tooltip whenever the quantity less than 5 for that productcode bound with, but once the tooltip has been shown, it just read the last row of datagridview, not all of the datas inside it.
Here is the image:
From the above image, you can see there are two productcode that has a quantity less than 5, but if you see from the right bottom corner, it just show the last row of the data.
Here is the code that I am using:
void CheckQuantity()
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string productCode = row.Cells[0].Value.ToString();
decimal quantity = Convert.ToDecimal(row.Cells[1].Value);
if (quantity < 5)
{
SystemManager.SoundEffect("C:/Windows/Media/Speech Off.wav");
customToolTip1.Show("- Product Code: " + productCode + "\n- Quantity: " + quantity, this, _screen.Right, _screen.Bottom, 5000);
timeLeft = 15;
_timer.Start();
}
else
{
timeLeft = 15;
_timer.Start();
}
}
}
void Timer_Tick(object sender, EventArgs e)
{
timeLeft--;
if (timeLeft == 0)
{
_timer.Stop();
CheckQuantity();
}
}
void Database_Load(object sender, EventArgs e)
{
_timer.Start();
}
void Database_FormClosed(object sender, FormClosedEventArgs e)
{
_timer.Stop();
}
uint timeLeft = 15;
I appreciate your answer.
Thanks.
It's a bit unclear the result you are expecting, is this what you mean?
void CheckQuantity()
{
string msg = "";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string productCode = row.Cells[0].Value.ToString();
decimal quantity = Convert.ToDecimal(row.Cells[1].Value);
if (quantity < 5)
{
msg += "- Product Code: " + productCode + " - Quantity: " + quantity + "\n";
}
}
if (msg != "")
{
SystemManager.SoundEffect("C:/Windows/Media/Speech Off.wav");
customToolTip1.Show(msg, this, _screen.Right, _screen.Bottom, 5000);
}
}
void Timer_Tick(object sender, EventArgs e)
{
_timer.Stop();
CheckQuantity();
}
void Database_Load(object sender, EventArgs e)
{
_timer.Interval = 15 * 1000;
_timer.Start();
}
void Database_FormClosed(object sender, FormClosedEventArgs e)
{
_timer.Stop();
}

Label is not updating as I am changing values

I am writing a small program to manage store and price based on the item quantity. In the desire functionality I want the Grand Total of all items cost to be displayed in the label. This label updates as I change the values in qty text boxes.
So far I have done writing this code and it is working perfectly and even the values are exactly the same in variables as I want but the problem is label doesn't update even I have null values in the qty text boxes. It keep shows the previous total until I change the values, and it seems get stuck on the last updated total.
namespace wpfTest
{
public partial class MainWindow : Window
{
List<int> myList = new List<int>();
int val1, val2;
int total;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
qtyTxt1.IsEnabled = false;
priceTxt1.IsEnabled = false;
qtyTxt2.IsEnabled = false;
priceTxt2.IsEnabled = false;
}
private void chk1_Click(object sender, RoutedEventArgs e)
{
if (chk1.IsChecked == true)
{
qtyTxt1.IsEnabled = true;
priceTxt1.IsEnabled = true;
}
else
{
qtyTxt1.IsEnabled = false;
priceTxt1.IsEnabled = false;
qtyTxt1.Clear();
priceTxt1.Text = "#50";
}
}
private void chk2_Click(object sender, RoutedEventArgs e)
{
if (chk2.IsChecked == true)
{
qtyTxt2.IsEnabled = true;
priceTxt2.IsEnabled = true;
}
else
{
qtyTxt2.IsEnabled = false;
priceTxt2.IsEnabled = false;
qtyTxt2.Clear();
priceTxt2.Text = "#100";
}
}
private void qtyTxt1_TextChanged(object sender, TextChangedEventArgs e)
{
if (qtyTxt1.Text=="")
{
priceTxt1.Text = "#50";
val1 = 0;
}
if (qtyTxt1.Text.Length > 0)
{
priceTxt1.Text = (Convert.ToInt32(qtyTxt1.Text) * 50).ToString();
val1 = Convert.ToInt32(priceTxt1.Text);
updateTotal();
}
}
private void qtyTxt2_TextChanged(object sender, TextChangedEventArgs e)
{
if (qtyTxt2.Text.Length == 0)
{
priceTxt2.Text = "#100";
val2 = 0;
}
if (qtyTxt2.Text.Length > 0)
{
priceTxt2.Text = (Convert.ToInt32(qtyTxt2.Text) * 100).ToString();
val2 = Convert.ToInt32(priceTxt2.Text);
updateTotal();
}
}
private void updateTotal()
{
lblTotal.Content = "";
lblTotal.Content = (val1 + val2).ToString();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Total : " + total + "\n" + "Val1 : " + val1 + "\n" + "Val2 : " + val2);
}
}
}
I have also uploaded screen shot at my Microsoft OneDrive account because I have limitation here to upload photograph. :
http://1drv.ms/1lM0IdE
Assign to Label Property Text
Try this
lblTotal.Text = "";
lblTotal.Text = (val1 + val2).ToString();
Well the first glaring problem is the fact that your trying to change the UILabel text through a "Content" property that doesn't exist unless you defined it yourself. You should be using the "text" property. Refer to the Apple documentation:
https://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html

displaying line number in rich text box c#

I have a Multiline richtextbox control into which i want to integrate the feature of adding a line number. i have considered many approaches
Add a label and updating the line numbers as the line count changes
Add a picturebox along with to draw string on it.
Add another textbox along with and show line numbers on it
Add listbox along and display line numbers in it.
I got two doubts.
The richtextbox which i'm using is a custom made control and derieves from RichTextBox class. How can i add multiple controls to it.
What is the best approach to show line numbers for the multiline text in c#
My own example. All is fine, but wordwrap must be disabled :(
int maxLC = 1; //maxLineCount - should be public
private void rTB_KeyUp(object sender, KeyEventArgs e)
{
int linecount = rTB.GetLineFromCharIndex( rTB.TextLength ) + 1;
if (linecount != maxLC)
{
tB_line.Clear();
for (int i = 1; i < linecount+1; i++)
{
tB_line.AppendText(Convert.ToString(i) + "\n");
}
maxLC = linecount;
}
}
where rTB is my richtextbox and tB is textBox next to rTB
J.T. jr
this code helped me thank you, needed to convert visual basic but could:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Dim maxlc As Integer = 1
Dim linecount As Integer = TextBox1.GetLineFromCharIndex(TextBox1.Height) + 1
If linecount <> maxlc Then
TextBox2.Clear()
For i = 0 To linecount - 1 Step 1
TextBox2.AppendText(Convert.ToString(i) + vbNewLine)
Next i
maxlc = linecount
End If
End Sub
public int getWidth()
{
int w = 25;
// get total lines of richTextBox1
int line = richTextBox1.Lines.Length;
if (line <= 99)
{
w = 20 + (int)richTextBox1.Font.Size;
}
else if (line <= 999)
{
w = 30 + (int)richTextBox1.Font.Size;
}
else
{
w = 50 + (int)richTextBox1.Font.Size;
}
return w;
}
public void AddLineNumbers()
{
// create & set Point pt to (0,0)
Point pt = new Point(0, 0);
// get First Index & First Line from richTextBox1
int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
// set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively
pt.X = ClientRectangle.Width;
pt.Y = ClientRectangle.Height;
// get Last Index & Last Line from richTextBox1
int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
// set Center alignment to LineNumberTextBox
LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center;
// set LineNumberTextBox text to null & width to getWidth() function value
LineNumberTextBox.Text = "";
LineNumberTextBox.Width = getWidth();
// now add each line number to LineNumberTextBox upto last line
for (int i = First_Line; i <= Last_Line + 2; i++)
{
LineNumberTextBox.Text += i + 1 + "\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
LineNumberTextBox.Font = richTextBox1.Font;
richTextBox1.Select();
AddLineNumbers();
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
if (pt.X == 1)
{
AddLineNumbers();
}
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
LineNumberTextBox.Text = "";
AddLineNumbers();
LineNumberTextBox.Invalidate();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.Text == "")
{
AddLineNumbers();
}
}
private void richTextBox1_FontChanged(object sender, EventArgs e)
{
LineNumberTextBox.Font = richTextBox1.Font;
richTextBox1.Select();
AddLineNumbers();
}
private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e)
{
richTextBox1.Select();
LineNumberTextBox.DeselectAll();
}
private void Form1_Resize(object sender, EventArgs e)
{
AddLineNumbers();
}
WORKS 100%!!! But you need to add richTextBox2 for line numbers, if you want change it to other
form like listbox, anyway it served me well.
private void richTextBox1_keyDown(object sender, KeyEventArgs e)
{
for (int i = 0; i <= richTextBox1.Lines.Count(); i++)
{
if (!(e.KeyCode == Keys.Back))
{
if (!richTextBox2.Text.Contains(i.ToString()))
{
richTextBox2.Text += i.ToString() + "\n";
}
}
else
{
richTextBox2.Clear();
}
}
}

Categories

Resources