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 BarPosted Sep 12, 2009, 8:41 AM
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());
}
}
}
Miodrag GrahovacPosted Sep 12, 2009, 7:04 PM
Miodrag GrahovacPosted Sep 12, 2009, 6:31 AM
Roei BarPosted Sep 11, 2009, 5:04 PM
Miodrag GrahovacPosted Sep 11, 2009, 1:38 PM
Master BillaPosted Sep 11, 2009, 12:57 PM
You can use the
this.TopLevelControl.SelectNextControl(this, true, true, true, true);
Thank you
Miodrag GrahovacPosted Sep 11, 2009, 11:44 AM
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.
Roei BarPosted Sep 11, 2009, 11:27 AM
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 :)