Ajay_Kumar
Ajay_Kumar

Reputation: 1387

Value increment of textbox in C#.net

I have 2 textbox and 1 button textbox1,textbox2 and 1 increment button.Both textbox is initialize by 1.If i will click on textbox1 and then click on incr button the value belongs to textbox1 will be increase only.And whenever i will click on textbox2 and again i will click on incr button only textbox2 value will be increment. How will i do this?

Upvotes: 0

Views: 5037

Answers (5)

Sandeep Jamkhandi
Sandeep Jamkhandi

Reputation: 11

This can be done on the client side using javascript. On focus of textbox1 update a hiddenfield value. similarly for the textbox2. then on button click, based on hidden f

Upvotes: 1

David Yaw
David Yaw

Reputation: 27864

You didn't say whether you were in WinForms or in WPF, so I won't show any code.

Have a field TextBox activeTextBox in your class. In the GotFocus events of each textbox, set activeTextBox = this text box. In the button click, convert activeTextBox's text to an integer, add one, and convert back to string and set the text back.

Edit:

activeTextBox is a field you will need to set up, the designer won't make it for you. If you set the GotFocus event of textBox1 set activeTextBox = textBox1, and similar for textBox2, then activeTextBox will always have the 'current' text box. Then in the button's click event, you can do whatever you need to do on activeTextBox. You shouldn't need to access textBox1 or textBox2 from the button click handler at all.

Upvotes: 1

Ajay_Kumar
Ajay_Kumar

Reputation: 1387

if (textBox1.Focused) { textBox1.Text = (Convert.ToInt32(textBox1.Text) + 1) + "";

        }
        else if (textBox2.Focused)
        {
            textBox2.Text = (Convert.ToInt32(textBox2.Text) + 1) + "";

        }

        else if (textBox3.Focused)
        {
            textBox3.Text = (Convert.ToInt32(textBox3.Text) + 1) + "";

        }

But ".Focused" is always returing False. Why ?

Upvotes: 0

user415789
user415789

Reputation:

Create a windows form application and paste this code on form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        TextBox textbox1 = new TextBox(), textbox2 = new TextBox();
        Button button1 = new Button();
        int FocusedTextBox = 0;

        public Form1()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form1_Load);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            button1.Click += new EventHandler(button1_Click);

            textbox1.Text = textbox2.Text = "1";
            textbox1.Location = new Point(100, 100);
            textbox2.Location = new Point(100, 140);
            button1.Location = new Point(100, 180);

            textbox1.Click += new EventHandler(textbox1_Click);
            textbox2.Click += new EventHandler(textbox2_Click);
            textbox1.ReadOnly = true;
            textbox2.ReadOnly = true;

            this.Controls.Add(textbox1);
            this.Controls.Add(textbox2);
            this.Controls.Add(button1);
        }

        void textbox2_Click(object sender, EventArgs e)
        {
            FocusedTextBox = 2;
        }

        void textbox1_Click(object sender, EventArgs e)
        {
            FocusedTextBox = 1;
        }

        void button1_Click(object sender, EventArgs e)
        {
            if (FocusedTextBox ==1)
                textbox1.Text = (int.Parse(textbox1.Text) + 1).ToString();
            else if (FocusedTextBox == 2)
                textbox2.Text = (int.Parse(textbox2.Text) + 1).ToString();
        }
    }
}

Upvotes: 0

Mario
Mario

Reputation: 36487

private int pickedbox = 0

[...]

private void textBox1_Enter(...)
{
    pickedbox = 0;
}

private void textBox2_Enter(...)
{
    pickedbox = 1;
}

private void button1_Click(...)
{
    switch(pickedbox)
    {
        case 0:
            textBox1.Text = int.Parse(textBox1.Text)++;
            break;
        case 1:
            textBox2.Text = int.Parse(textBox2.Text)++;
            break;
    }
}

Upvotes: 0

Related Questions