Search code examples
c#if-statementcheckboxtextboxappendtext

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.

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

  • 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'
  • Pointers are fixed size buffers may only be used in a unsafe context
  • cannot implicitly convert type 'int' to '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
  • Argument 1: cannot convert from 'int' to 'string'

So I'm wondering what I'm doing wrong to achieve the kind of application I want. The application looks something like this:

enter image description here

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). The code looks the same like before but I changed this thanks to David Pilkington.

private void button1_Click(object sender, EventArgs e) /* calculate-button */
    {
        capacity = vehicle / resource;
        total = capacity * price;
        textBox4.AppendText(total.ToString());
    }

Solution

  • 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

    You may also want to use TryParse to handle messy input

    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;