Reputation: 205
I am developing an application (in c# using Visual studio 2012) where i have to read a config file which is in .txt format. I am also using tab control to switch between different tab. what i want to do is that, when i click on config tab, my application should read the config file and place the data that i require into the text boxes that are present under that tab. Any help would be appreciated.
i have tried placing the code to autofill my textboxes in the form constructor and it works, but i don't want to do it through constructor because it will only read the config file when the form is created and not when i switch from one tab to another.
Upvotes: 0
Views: 826
Reputation: 10552
In your design
view select the entire tabControl.
Then click the events button (lightning bolt) on the properties window.
Then double click SelectedIndexChanged
here is a sample code:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
// "First Tab Page" is the text of the tab page.
if ((sender as TabControl).SelectedTab.Text == "First Tab Page")
{
string filecontents = File.ReadAllText(@"path\to\configFile.txt");
textBox1.Text = filecontents;
}
}
Simple?
Upvotes: 1
Reputation: 3751
You need to use the SelectedIndexChanged event of tab control.
tabContrl1.TabControl.SelectedIndexChanged += tabControl1_SelectedIndexChanged;
Add the event handler. Name your config tab as "ConfigTab" (Or whatever you want).
private void tabControl1_SelectedIndexChanged(Object sender, EventArgs e) {
if (tabControl1.SelectedTab.Name.Equals("ConfigTab")) {
//Fill textbox here
}
}
Upvotes: 1