Forum guidelines
AuthorQuestion
write to selected (active? focused?) textbox
Posted on: 11 Sep 2009
I have 3 buttons (taster1, taster2, taster3), and 3 textboxes (tbA, tbD, tbS. I'd like every button to write something in every textbox successively. First textBox is focused on form_load, and after user fill it I'd like next textBox to be focused.
tbA.Text=tbA.Text+"A";
tbD.Text=tbD.Text+"D";
tbS.Text=tbS.Text+"S";


AuthorReply
Re: write to selected (active? focused?) textbox
Posted on: 11 Sep 2009  
what you are looking for is TabIndex which actually means that on Tab click the next control will be focused

just do the following:

View menu - tab order which draws little numbers on all the controls.
Just click the numbers to get to the tab index you want :)

If  answer helped you don't forget to Mark  "Do you like Answer"
Thank you
:)


Roei bar
Cellcom LTD
Software Architect
Re: write to selected (active? focused?) textbox
Posted on: 11 Sep 2009  
@Roei Bar Thank you for fast reply, but I have tried to use TabIndex and have get focus on next textbox, but when I click one of taster buttons, this textBox lost focus, and I can't write in it.

I'd just like when form loads that all buttons write strings in first textBox, then in second, and finally in third.
Looks simple, but I can't solve it all day.

Re: write to selected (active? focused?) textbox
Posted on: 11 Sep 2009  
Hi

You can use the

this.TopLevelControl.SelectNextControl(this, true, true, true, true);

Thank you

If its helped to you, make it green
Re: write to selected (active? focused?) textbox
Posted on: 11 Sep 2009  
I forgot to say that it is application for touch screen, and user will not have keyboard. He can only use buttons (taster1, taster2, taster3) to add some strings to textBoxes. So, when I click tasters, my text box lost focus. How to solve this?

Re: write to selected (active? focused?) textbox
Posted on: 11 Sep 2009  
if each teaser is connected to a single txtBox just set the focus on the txtBox by MouseOut Event of the Teaser or directly after Teaser_Click Event


If  answer helped you don't forget to Mark  "Do you like Answer"
Thank you
:)


Roei bar
Cellcom LTD
Software Architect
Re: write to selected (active? focused?) textbox
Posted on: 12 Sep 2009  
I am sorry, but each taster is connected to all txtBoxes. I've tried something like this:
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
{
public Form1()
{
InitializeComponent();
}

public int TabIndex { get; set; }

private void button1_Click(object sender, EventArgs e)
{

if (textBox1.Enabled == true)
{
textBox1.Text = textBox1.Text + "1";
textBox2.Enabled = false;
textBox3.Enabled = false;
}

if (textBox2.Enabled == true)
{
textBox2.Text = textBox2.Text + "1";
textBox1.Enabled = false;
textBox3.Enabled = false;
}

if (textBox3.Enabled == true)
{
textBox3.Text = textBox3.Text + "1";
textBox1.Enabled = false;
textBox2.Enabled = false;
}

}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Enabled == true)
{
textBox1.Text = textBox1.Text + "2";
textBox2.Enabled = false;
textBox3.Enabled = false;
}

if (textBox2.Enabled == true)
{
textBox2.Text = textBox2.Text + "2";
textBox1.Enabled = false;
textBox3.Enabled = false;
}

if (textBox3.Enabled == true)
{
textBox3.Text = textBox3.Text + "2";
textBox1.Enabled = false;
textBox2.Enabled = false;
}

}

private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Enabled == true)
{
textBox1.Text = textBox1.Text + "3";
textBox2.Enabled = false;
textBox3.Enabled = false;
}

if (textBox2.Enabled == true)
{
textBox2.Text = textBox2.Text + "3";
textBox1.Enabled = false;
textBox3.Enabled = false;
}

if (textBox3.Enabled == true)
{
textBox3.Text = textBox3.Text + "3";
textBox1.Enabled = false;
textBox2.Enabled = false;
}
}

private void Form1_Load(object sender, EventArgs e)
{
TabIndex = 1;
textBox1.Enabled = true;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox1.Focus();


}

private void textBox1_TextChanged(object sender, EventArgs e)
{

if (textBox1.Text.Length > 4)
textBox1.Text = textBox1.Text.Substring(0, 4);
textBox1.SelectionStart = textBox1.Text.Length;
}





private void btnTab_Click(object sender, EventArgs e)
{
TabIndex = TabIndex + 1;

this.panel1.TopLevelControl.SelectNextControl(this, true, true, true, true);
switch (TabIndex)
{
case 1:
textBox1.Enabled = true;
textBox2.Enabled = false;
textBox3.Enabled = false;
break;
case 2:
textBox1.Enabled = false;
textBox2.Enabled = true;
textBox3.Enabled = false;
break;

case 3:
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = true;
break;

}

}



}
}

It' looks odd, but it works. The only problem is that I have to write this code for much more buttons, and textBoxes.

Roei Bar
  • 0
  • 0
accepted
Re: write to selected (active? focused?) textbox
Posted on: 12 Sep 2009   Accepted Answer
is this code better for you :

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
    {
        const int MAX_TABINDEX = 3;
        public Form1()
        {
            InitializeComponent();
        }

        public int TabIndex { get; set; }

        private void button1_Click(object sender, EventArgs e)
        {
            addTextToEnabledTextControl("1");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            addTextToEnabledTextControl("2");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            addTextToEnabledTextControl("3");
        }

        private void addTextToEnabledTextControl(string textToAdd)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.GetType().Name == "TextBox")
                {
                    if (item.Enabled)
                    {
                        item.Text += textToAdd;
                    }
                }
            }
        }

        private void disableAllTextControlButSelected(string selectedTextBoxId)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.GetType().Name == "TextBox")
                {
                    if (item.Name.EndsWith(selectedTextBoxId))
                    {
                        item.Enabled = true;
                    }
                    else
                    {
                        item.Enabled = false;
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TabIndex = 1;
            textBox1.Enabled = true;
            textBox2.Enabled = false;
            textBox3.Enabled = false;
            textBox1.Focus();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            if (textBox1.Text.Length > 4)
                textBox1.Text = textBox1.Text.Substring(0, 4);
            textBox1.SelectionStart = textBox1.Text.Length;
        }

        private void btnTab_Click(object sender, EventArgs e)
        {
            TabIndex = TabIndex + 1;
            if (TabIndex > MAX_TABINDEX)
            {
                TabIndex = 1;
            }
            this.panel1.TopLevelControl.SelectNextControl(this, true, true, true, true);
            disableAllTextControlButSelected(TabIndex.ToString());
        }
    }
}



If  answer helped you don't forget to Mark  "Do you like Answer"
Thank you
:)


Roei bar
Cellcom LTD
Software Architect
Re: write to selected (active? focused?) textbox
Posted on: 12 Sep 2009  
@Roei Bar Thank you!!! I like this code!!!

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.