[英]Cant get the button to do simple calculations and write the answer in textbox
I have just started writing C# a few days ago and would need some help with a program that I'm writing. 几天前我才刚刚开始编写C#,并且在我正在编写的程序上需要一些帮助。
This is not the real code just something I wrote to show you how the original code is built. 这不是真正的代码,而只是我写给您看的如何显示原始代码的东西。
public partial class form1 : form
{
int container;
int resource;
int capacity;
int price;
int total;
int basket = 10; //container that holds 10 slots
int apples = 2; //resource that takes 2 slots per resource
}
public form1()
{
private void checkbox1_checkedchanged(object sender, eventargs e) //basket checkbox
{
if (((CheckBox)checkbox1).Checked == true)
{
basket = container;
}
}
private void checkbox2_checkedchanged(object sender, eventargs e) //apples checkbox
{
if (((CheckBox)checkbox2.Checked == true)
{
apples = resource;
}
}
private void button1_Click(object sender, EventArgs e) //calculate button
{
container / resource = capacity; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text);
capacity * price = total;
textbox2.AppendText(total); //textbox2 is where they will se how much the apples cost
}
}
The problem is that I get errors from the code in "button1" that says everything from 问题是我从“ button1”中的代码中得到了错误,该代码说明了
So I'm wondering what I'm doing wrong to achieve the kind of application I want. 所以我想知道我在做我想要的应用程序时做错了什么。 The application looks something like this:
该应用程序看起来像这样:
Edit: Original code has been removed 编辑:原始代码已被删除
Edit2: This is how the button1 looks like now and there is no errors except one that occurs when I try to divide vehicle with salt (Haven't tried any other resource but thats the one that is listed). Edit2:这是button1现在的样子,除了我尝试用盐分割车辆时没有发生任何错误(没有尝试过其他任何资源,但是那是列出的那个)。 The code looks the same like before but I changed this thanks to David Pilkington.
代码看起来像以前一样,但是我感谢David Pilkington对此进行了更改。
private void button1_Click(object sender, EventArgs e) /* calculate-button */
{
capacity = vehicle / resource;
total = capacity * price;
textBox4.AppendText(total.ToString());
}
private void button1_Click(object sender, EventArgs e) //calculate button
{
capacity = container / resource; //to see how many resources the basket can hold
price = int.Parse(textbox1.text);
total= capacity * price;
textbox2.AppendText(total.ToString()); //textbox2 is where they will se how much the apples cost
}
Your assignments are the wrong way round and you need to convert the int
to a string
before adding it 您的分配方式错误,您需要先将
int
转换为string
然后再添加它
You may also want to use TryParse
to handle messy input 您可能还想使用
TryParse
处理混乱的输入
private void button1_Click(object sender, EventArgs e) //calculate button
{
capacity = container / resource; //to see how many resources the basket can hold
if(int.TryParse(textbox1.text, out price))
{
total= capacity * price;
textbox2.AppendText(total.ToString()); //textbox2 is where they will se how much the apples cost
}
}
BIG EDIT: 大编辑:
It seems your assignments are all the wrong way round. 看来您的作业错了。 This means that resource is never set and will create and error in this line
capacity = container / resource;
这意味着资源永远不会被设置,并且将在此行
capacity = container / resource;
创建并出错capacity = container / resource;
Your code is all wrong in the button2_click() 您的代码在button2_click()中全部错误
container / resource = capacity; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text);
capacity * price = total;
textbox2.AppendText(total);
Should (perhaps?) be 应该(也许吗?)
capacity = container / resource; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text);
total = capacity * price;
textbox2.AppendText(total.ToString());
That should remove most of your errors in the code. 那应该删除代码中的大多数错误。
Hope it helps! 希望能帮助到你! //KH.
// KH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.