dcolton147
dcolton147

Reputation: 23

Add to int each time a button is clicked and show in textbox c#

I am very new to programming and I am trying to do that every time you click a button, it adds one to the value of an int and shows it in a textbox. My code is:

private void button1_Click(object sender, EventArgs e)
{
    int a = 100;
    a++;

    txtBox1.Text = a.ToString();
}

So when I click the button it shows in the text box 101, but when I click it again, I want the textbox to show 102 and 103 etc etc. Any ideas? I assume it's very easy and using some variation of a loop but I have tried a few things and nothing seems to work. Any tips would be greatly appreciated! Thanks.

Upvotes: 1

Views: 20049

Answers (8)

Minhaj Patel
Minhaj Patel

Reputation: 579

if you want to optimization your code than firstly set the textbox property text = 100 and write only one line code in button click event

private void button1_Click(object sender, EventArgs e)
{            
    txtBox1.Text = (Convert.ToInt32(txtBox1.Text) + 1).ToString();
}

as you know C# complie the code line by line and you have only one line code than it's give faster perfomance.

Upvotes: 0

Syed Burhan
Syed Burhan

Reputation: 21

static int a = 100;

private void button1_Click(object sender, EventArgs e)
{        
     a++;

      txtBox1.Text = a.ToString();
}

Upvotes: 0

New Developer
New Developer

Reputation: 3305

Placing int a = 100; inside the button1_Click(object sender, EventArgs e) will set a to 100 when each time function executing. If you need to have a counter place it outside from the function(Then it will initialize only once.) and increment it when executing the function.

Solution

int a = 100;
private void button1_Click(object sender, EventArgs e)
{        
    a++;

    txtBox1.Text = a.ToString();
}

Upvotes: 0

Igoy
Igoy

Reputation: 2962

int a = 100;
txtBox1.Text = a.ToString();
  ......

private void button1_Click(object sender, EventArgs e) {

    a++;

   txtBox1.Text = a.ToString();
}

Upvotes: 0

algreat
algreat

Reputation: 9002

It is possible not to create global fields and store count of clicks inside the textbox. This is especially convenient if you have several buttons.

private void button1_Click(object sender, EventArgs e)
    {     
        if (txtBox1.Tag is int)
        {
            int a = (int)txtBox1.Tag;
            a++;

            txtBox1.Tag = a;

            txtBox1.Text = a.ToString();
        }
        else
        {
            txtBox1.Tag = 100;
            txtBox1.Text = 100;
        }
}

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

You need to declare a as a member of the class containing your method:

private int _a = 100;

private void button1_Click(object sender, EventArgs e)
{
    _a++;

    txtBox1.Text = _a.ToString();
}

If you don't do that, you will have a new instance every time the button is clicked, so you will always see 101 in your text box.

Upvotes: 0

gzaxx
gzaxx

Reputation: 17590

You have to store your value outside of Method Body.

private int a = 100;

private void button1_Click(object sender, EventArgs e)
{        
     a++;

      txtBox1.Text = a.ToString();
}

What you did in your program is anytime you clicked the button, new Integer a was declared with value of 100, then you are increasing it by 1 and that's why you always seen '101'.

Upvotes: 5

Andrey Gordeev
Andrey Gordeev

Reputation: 32449

In your code you delcare a and assign a value to it over and over again every time you click on the button.

You should declare the variable outside of button1_Click method:

class Window1
{
    int a = 100;
    ....
    private void button1_Click(object sender, EventArgs e)
        {
            a++;
            txtBox1.Text = a.ToString();
        }
}

Upvotes: 0

Related Questions