简体   繁体   English

无法获取执行简单计算并在文本框中输入答案的按钮

[英]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”中的代码中得到了错误,该代码说明了

  • The left-hand side of an assignment must be avariable, property or indexer 分配的左侧必须是变量,属性或索引器
  • Cannot take the address of, get the size of, or declare a pointer to a managed type ('capacity') 无法获取地址,获取大小或声明指向托管类型的指针(“容量”)
  • 'WindowsFormApplication1.Form1.Capacity' is a 'field' but is used like a 'type' “ WindowsFormApplication1.Form1.Capacity”是一个“字段”,但其用法类似于“类型”
  • Pointers are fixed size buffers may only be used in a unsafe context 指针是固定大小的缓冲区,只能在不安全的上下文中使用
  • cannot implicitly convert type 'int' to 'capacity*'. 无法将类型'int'隐式转换为'capacity *'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)
  • The best overloaded method match for 'System.Window.Forms.TextBoxBase.AppendText(String)'hassome invalid arguments 最佳重载方法匹配'System.Window.Forms.TextBoxBase.AppendText(String)'有一些无效的参数
  • Argument 1: cannot convert from 'int' to 'string' 参数1:无法从“ int”转换为“ string”

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM