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
Related
I have to make a quantile configuration and I have to set a condition so that the second value is higher than the first, the third value is higher than the second and so on. I am using numericupdowns and the value is set by the user. I tried to implement this code but it always shows a message box error even if the values are correct. This is my code so far (using Visual studio and C#):
decimal min = 1;
//when the first numeric is changed:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = min;
min = numericUpDown1.Value;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown2.Minimum = min;
min = numericUpDown2.Value;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown3.Minimum = min;
min = numericUpDown3.Value;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown4.Minimum = min;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value )
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
You need to keep all the NumericUpDown value inside a List or an array where you can manipulate them like a single entity trhough a loop.
Of course changing the minimum value of one of the numeric elements should be linked to a check for the current value inside that control because you can't have a current value lesser than the new minimum value.
So the first thing to do is to create a global variable inside your form class where you keep the references to all the numeric controls that you want to synchronize
public class Form1
{
private List<NumericUpDown> numbers = new List<NumericUpDown>();
public Form1 : Form
{
InitializeComponent();
numbers.AddRange(new [] {n1, n2,n3,n4,n5});
}
......
}
Now you can write a method like this one that adjust the minimum on all the numeric included in the list
private void UpdateMinimum()
{
for (int x = 0; x < numbers.Count-1; x++)
{
if(numbers[x].Value > numbers[x+1].Value)
numbers[x+1].Value = numbers[x].Value;
numbers[x+1].Minimum = numbers[x].Value;
}
}
finally you have all your NumericUpDown event ValueChanged call the same method
void numerics_ValueChanged(object sender, EventArgs e)
{
UpdateMinimum();
}
If you want to set a condition so that the second value is higher than the first, the third value is higher than the second and so on, you can refer to the following code:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = 1;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
numericUpDown2.Minimum = numericUpDown1.Value + 1;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
numericUpDown3.Minimum = numericUpDown2.Value + 1;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
numericUpDown4.Minimum = numericUpDown3.Value + 1;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value)
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
Here is the test result:
I am writing a c# windows form code to
get the number from button1 and button2 and add them together in a text box but The compiler argues on the convert.toint32(textbox3.text) statement
and also it increases the value of the two variable and three variable how can I keep it constant but increase the value of textbox
and I need a solution?
int Three = 0;
int Two = 0;
//int one = 0;
int sum = 0;
// int sum = 0;
//int dec = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
//Three += 3;
//textBox3.Text = sum.ToString();
Three += 3;
sum = Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text = sum.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Two += 2;
sum = Two + Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text =Convert.ToInt32(textBox3.Text) + Two.ToString();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString();
}
`
Change
sum = Convert.ToInt32(textBox3.Text) + Three;
To
sum = Convert.ToInt32(textBox3.Text == "" ? "0" : textBox3.Text) + 3;
Also, remove
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString(); // this
}
because it doesn't make any sense.
Your variables belong to class and they are accessible for initialization in constructor. This can be done in many ways but you need to check if textboxes have values then try to convert it and add it.
private int Two;
private int Three;
private int sum;
public Form1()
{
this.Two = 0;
this.Three = 0;
this.sum = 0;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
this.Three += 3;
sum = textBox3.Text != String.Empty ? Convert.ToInt32(textBox3.Text) : 0;
textBox3.Text = Convert.ToString(sum + this.Three);
}
... same for number Two
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = "0";
}
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);
}
}
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.
I have three radio buttons. The code behind their change events is as follows:
private void uxRajRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (uxRajRadioButton.Checked == true)
{
uxPersonBettingLabel.Text = "Raj";
GuySelected = 0;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
private void uxPaulRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (uxPaulRadioButton.Checked == true)
{
uxPersonBettingLabel.Text = "Paul";
GuySelected = 1;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
private void uxMikeRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (uxMikeRadioButton.Checked == true)
{
uxPersonBettingLabel.Text = "Mike";
GuySelected = 2;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
With just three radio buttons this is acceptable but if I had say 7 radio buttons each with 20 lines of code behind its change event then it would be a lot of (needless?) code. I've attempted to shorten the above and ended up with the following. Is this correct?
private void uxRajRadioButton_CheckedChanged(object sender, EventArgs e)
{
radioButtonCode(this.uxRajRadioButton, 0);
}
private void uxPaulRadioButton_CheckedChanged(object sender, EventArgs e)
{
radioButtonCode(this.uxPaulRadioButton,1);
}
private void uxMikeRadioButton_CheckedChanged(object sender, EventArgs e)
{
radioButtonCode(this.uxMikeRadioButton, 2);
}
int GuySelected=0;
public void radioButtonCode(RadioButton myRadio, int mybettorIndex)
{
if (myRadio.Checked == true)
{
GuySelected = mybettorIndex;
uxPersonBettingLabel.Text = Guys[GuySelected].Name;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
Could you have the same event handler for all the radio buttons? Something like
private void uxRadioButton_CheckedChanged(object sender, EventArgs e)
{
radioButtonCode((RadioButton)sender);
}
public void radioButtonCode(RadioButton myRadio)
{
if (myRadio.Checked == true)
{
int guySelected = getGuySelectedIndex(myRadio);
uxPersonBettingLabel.Text = Guys[guySelected].Name;
uxBetNumericUpDown.Maximum = Guys[guySelected].Cash;
}
}
public int getGuySelectedIndex(RadioButton myRadio)
{
int index = 0;
if (myRadio == this.uxRajRadioButton) index = 0;
else if (myRadio == this.uxPaulRadioButton) index = 1;
else if (myRadio == this.uxMikeRadioButton) index = 2;
return index;
}
Your second code sample, where you have extracted the commonalities out to a function looks like the way I would have done this refactoring.
It is about as good as you can make it, barring the name (UpdateUserInfo might be slightly better).
There is a better way. Declare an array of radio buttons and bind them to above event at run time. This binding code won't be inside designer page. This will result in single ArrayRadio_checkedChange event. In this method you can use sender property to figure out the proper radio button's index and take action accordingly.
you can use
uxRajRadioButton.CheckedChanged += new EventHandler(rb_CheckedChanged);
uxPaulRadioButton.CheckedChanged += new EventHandler(rb_CheckedChanged);
...
uxRajRadioButton.Tag =new KeyValuePair<string,int>("Raj",0);
uxPaulRadioButton.Tag =new KeyValuePair<string,int>("Paul",1);
....
private void rb_CheckedChanged(object sender, EventArgs e)
{
if(!(sender is RadioButton))
return;
RadioButton myRadio= sender as RadioButton;
if (myRadio.Checked == true)
{
myRadio.Text = (myRadio.Tag as KeyValuePair<string,int>).Key;
GuySelected = (myRadio.Tag as KeyValuePair<string,int>).Value;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
What I usually do is I put the index in the tag attribute. This way you can bind this event to every RadioButton
public void uxRadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton myRadio = (RadioButton) sender;
if (myRadio.Checked)
{
GuySelected = (int)myRadio.Tag;
uxPersonBettingLabel.Text = Guys[GuySelected].Name;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}