标签未更新,因为我正在更改值 [英] Label Is Not Updating As I Am Changing Values
问题描述
我正在编写一个小程序来根据商品数量来管理商店和价格。在欲望功能中,我希望所有项目的总计费用显示在标签中。当我更改数量文本框中的值时,此标签会更新。
到目前为止,我已经完成了编写这段代码并且它工作得很完美,甚至在变量中的值与我想要的完全相同但问题是标签不更新,即使我有空值数量的文本框。它会一直显示之前的总数,直到我更改值,并且似乎卡在最后更新的总数上。
Hi,
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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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()
{
total = 0;
total = val1 + val2;
totalTxt.Clear();
totalTxt.Text = (val1 + val2).ToString();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Total : " + total + "\n" + "Val1 : " + val1 + "\n" + "Val2 : " + val2);
}
}
}
我还上传了我的屏幕截图微软onedrive帐户,因为我在这里限制上传照片。
[ ^ ]
谢谢
I have also uploaded screen shot at my Microsoft onedrive account because i have limitation here to upload photograph.
[^]
Thanks
推荐答案
尝试自己找到问题。你知道怎么调试吗?
掌握在VS中调试 [ ^ ]
如果您进行调试和检查,您会发现一些未按预期设置的值。
例如,你不会得到Convert.ToInt32
的预期值为什么!
去查看该方法的文档。你会找到原因
http://msdn.microsoft.com/en -us / library / sf1aw27b.aspx [ ^ ]
您将获得@ 50等值的格式异常
try to find the issue by your self. do you know how to debug?
mastering debugging in VS[^]
if you debug and check, you will find some values not set as you expected.
for example here you will not get expected value forConvert.ToInt32
why!
go and check the documentation for that method. you will find the reason
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx[^]
you will get format exception for values like "@50"
这篇关于标签未更新,因为我正在更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!