Hi All,
i am new in dyamically created textbox
i try sum code from internet, but its not work properly. i want to use (Enter Key or Tab) to create next textbox and move cursour to newly created textbox but when i press Shift+Tab or enter its automaticaly focus gose to newly created text box
hear is my code
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
TextBox txt = new TextBox();
int r=0, c=0, r1, c1, cwidth;
int rows = 50;
public Form1()
{
InitializeComponent();
txt.BorderStyle = BorderStyle.None;
txt.Name = "txt" + 0 + 0;
txt.Width = 350;
txt.Location = new Point(20, 50);
txt.GotFocus += new EventHandler(OnFocus1);
txt.KeyUp += TextBoxKeyUp;
txt.LostFocus += new EventHandler(OnFocusLost);
txt.AutoSize = false;
this.Controls.Add(txt);
}
private void OnFocusLost(object sender, EventArgs e)
{
var box = sender as TextBox;
box.BackColor = Color.CornflowerBlue;
}
private void OnFocus1(object sender, EventArgs e)
{
var box = sender as TextBox;
box.BackColor = Color.White;
}
private void CreateNewColumn()
{
c1++;
TextBox txt = new TextBox();
txt.BorderStyle = BorderStyle.None;
if (c > 3)
{
c1 = 0; r1++; rows = rows + 20;
txt.Name = "txt" + r1 + c1;
txt.Width = 350;
txt.Location = new Point(20, rows);
cwidth = 0;
}
else
{
txt.Name = "txt" + r1 + c1;
txt.Width = 90;
cwidth = cwidth + Controls["txt" + r + c].Width;
if (c == 0)
{
txt.Location = new Point(cwidth + 90, rows);
}
else
{
if (c > 3)
{
}
else
{
txt.Location = new Point(cwidth + 90, rows);
}
}
}
txt.AutoSize = false;
txt.GotFocus += new EventHandler(OnFocus1);
txt.LostFocus += new EventHandler(OnFocusLost);
txt.KeyUp += TextBoxKeyUp;
this.Controls.Add(txt);
r = r1; c = c1;
txt.Focus();
}
private void TextBoxKeyUp(object sender, KeyEventArgs e)
{
var box = sender as TextBox;
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
{
if (box.Text.Trim() == "")
{
box.Focus();
}
else
{
int i = int.Parse(box.Name.Substring(3, box.Name.Length-3).Trim());
int ic = int.Parse(Convert.ToDecimal(r).ToString("0") + Convert.ToDecimal(c).ToString("0"));
if (i == ic)
{
if (i < ic)
{ }
else
{
CreateNewColumn();
}
}
else
{
SendKeys.Send("{TAB}");
}
}
}
}
}
}