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";
Loading

Roei BarRank #1,271 · 1.4K reputationPosted Sep 12, 2009, 4:41 AM EDT
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());
}
}
}
Roei BarRank #1,271 · 1.4K reputationPosted Sep 11, 2009, 7:27 AM EDT
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 :)
Miodrag Grahovac31 reputationPosted Sep 11, 2009, 7:44 AM EDT
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.
Master BillaRank #2,229 · 630 reputationPosted Sep 11, 2009, 8:57 AM EDT
You can use the
this.TopLevelControl.SelectNextControl(this, true, true, true, true);
Thank you
Miodrag Grahovac31 reputationPosted Sep 11, 2009, 9:38 AM EDT
Roei BarRank #1,271 · 1.4K reputationPosted Sep 11, 2009, 1:04 PM EDT
Miodrag Grahovac31 reputationPosted Sep 12, 2009, 2:31 AM EDT
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; } } } }Miodrag Grahovac31 reputationPosted Sep 12, 2009, 3:04 PM EDT