Reputation: 11197
I have a tabbar control and inside the first tab I've a textbox. What I want is, when the first tab is selected the textbox will be auto focused.
I can access the tabbar selected index changed event but can not access the textbox to be auto focused. Any Idea?
While adding to tabbar I am doing this:
capture = new CaptureForm(photoGrapherName);
capture.TopLevel = false;
capture.Visible = true;
capture.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
capture.Dock = DockStyle.Fill;
tabControl1.TabPages[0].Controls.Add(capture); capture = new CaptureForm(photoGrapherName);
capture.TopLevel = false;
capture.Visible = true;
capture.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
capture.Dock = DockStyle.Fill;
tabControl1.TabPages[0].Controls.Add(capture);
And this is my tab change event:
private void TabControl1_SelectedIndexChanged(Object sender, EventArgs e)
{
switch(tabControl1.SelectedIndex)
{
case 0:
//I want to access the textbox from here.
break;
case 1:
break;
}
}
CaptureForm has a textbox named 'ClientCode', I want to make this textbox focus when anyone select Capture tab.
Upvotes: 0
Views: 157
Reputation: 4170
did you try with SelectedIndexChanged event? if you talking about TabControl we can do this using selectedIndexChanged event with the help of switch case..
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
switch ((sender as TabControl).SelectedIndex)
{
case 0:
//nothing to do.. or you can, if you want.. :)
break;
case 1:
tbFName.Focus();
break;
}
}
UPDATE
as you modfied I got to know you are adding control to TabPage dynamically so use the below snippet to get the dynamically added TextBox
public Form1()
{
InitializeComponent();
TextBox tb = new TextBox();
tb.Name = "dynamic";
tb.Text = "Text dynamic";
tabControl1.TabPages[1].Controls.Add(tb);
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
TabControl tc = (sender as TabControl);
switch (tc.SelectedIndex)
{
case 0:
break;
case 1:
Control[] temp = tc.TabPages[1].Controls.Find("dynamic", true);
if (temp.Length == 1)
{
(temp[0] as TextBox).Focus();
}
break;
}
}
Hope it make sense to you.. !
Upvotes: 2