Adding controls to tab page runtime - c#

I am using C# and winForms, I have a few tabcontrols, which contains a few tab pages,
I would like to add all tab pages my user control, it would be best, If I could add it after user click on tab page. It works for me only, when I add this controls in constructor - what makes delay at application start - about 3 seconds, what is very much.
I would like to add this controls at run time, like
tabPage1.onClickEvent()
{
tabPage1.Controls.Add(myUserControl);
}
but it didnt works, I also tryied to use Invalidate, or Refresh method but it not works.
So is there any possibility to add this userControls in other method than constructor?
Maybe its problem, that I have TabControl inside TabControl and I have to add this controls throught parent TabControl?
Thanks!

Try double clicking the tag page in the designer, or you can add the event handler manually like this:
tabPage1.Click += new EventHandler(tagPage1_Click);
I don't know whether tabPage1 is dynamic, but if it's not, you should add the above event handler to your designer.cs file.
Here is the event handler in the code:
protected void tabPage1_Click(object sender, EventArgs e)
{
tagPage1.Controls.Add(new TextBox());
}

Related

how to access a user define controls iner controls in parent form

I'm new to windows forms programming so my question may sound little strange.
I have created a user define control (countdown timer) now I'm creating n no of it dynamically in a form by Click of a button (Add new timer) its working well and good.
Here is the Creation Code
private void Addnew_Click(object sender, EventArgs e)
{
UserControl1.userControl11 = new UserControl1();
flowLayoutPanel1.Controls.Add(userControl11);
}
My user control has a Reset button that reset all the content inside the user define control.
it is also working, but What I want Allow user to reset all the Created timers using the “Reset All” button on the form.
Okay one way to do this.
Create a List<UserControl1> private member on your form called say _myUserControls
In your Addnew Handler add it to the list.
If you have a remove button, don't forget to remove from _myUserControls as well.
Add a Reset method to your UserControl1, that does what it needs to do.
Then in your Reset all button click handler
foreach(UserControl1 ctrl in _myUserControls)
{
ctrl.Reset();
}
Jobs a good 'un
The answer I referred you to in comments, would be a way of finding all instances of your UserControl1 class, so you wouldn't need an internal list.

How to use same textbox with other tabs on the tabcontrol?

Sorry for the simple question and also sorry if there is an answer on the site and I couldn't find.
I want to use same textbox in every tab that I use on my form. How can I do that?
Add this to the TabControl's SelectedIndexChanged event handler.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox1);
}
Adding a control to one tab page's Controls collection automatically removes it from the others.
Note: I have tested it by adding two lables on the form above the tab control and added these two lines to the method shown above:
label1.Text = tabControl1.TabPages[0].Controls.Count.ToString();
label2.Text = tabControl1.TabPages[1].Controls.Count.ToString();
Put the TextBox in the parent control of the TabControl. It can hover over all the rest. You might need to rework the focus traversal though.

how to execute code when the tab index changes?

I currently have a tabcontrol on my windows form in visual studio, i would like it so that when the user clicks on a different tab that i can execute some code (for example populate a listbox).
when i double click on the tab it only brings up an onclick event for the body of the tabcontrol.
i was thinking that i may have to create a thread in the form load that will constantly check whether the tab index changes and if it does then execute some code. but surely there must be an easier way?
You can use TabControl.Selecting or TabControl.SelectedIndexChanged event
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
//Your code goes here.
}
You should look at TabControl events
http://msdn.microsoft.com/en-ie/library/system.windows.forms.tabcontrol.selecting.aspx

Windows Forms - detecting button click from a panel inside the form

I have Windows Form named - Form1 and inside Form1 I have a panel named panel1. I use this panel only to add buttons in him. For now there are exactly 9 buttons but I intend to change their number dynamicly if this has something to do with my current problem. What I need is way to detect a when a button from this panel is clicked (I have other buttons too but, they are in Form1 outside the panel) and also to know exactly which button was clicked.
I tried this:
private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("HI" + sender);
}
As you can see, it's not much, but was enough to see that I can't do that using pnael1's_click event. Using this code I get the message box when I click anywhere in the panel except the buttons. So how can I do that. Is it possible to do it from inside panel1 or I should group those buttons using another approach but it's important to be able to keep the difference between those buttons which are now in panel1 and the other buttons I may (and in in fact I do have)?
When creating the dynamic buttons, you register that button instance's Click event and attach to an event handler (a single handler can handle all buttons' click event):
var dynamicButton1 = new Button();
dynamicButton1.Click += MyButtonClickHandler;
As long as MyButtonClickHandler has a signature that's suitable for a Click event (that's any method returning void and taking an object and an EventArgs, the handler should respond to a dynamic button's click event for as long as the button instance exists.
As long as you aren't adding controls dynamically over time, and the number of buttons is fixed as soon as the form is initialized, you can use this to add a click event handler to all buttons within a panel:
foreach (var button in panel.Controls.OfType<Button>())
{
button.Click += HandleClick;
}

Why is the tab page body not updating with a .NET tab control?

I am having a strange problem with the .NET TabControl in C# (Visual Studio 2010). Start a Windows Forms Application. Add a tab control and a button. Add two different labels to the two tab pages so you can differentiate them. The purpose of the button is just to act as a next button; subscribe to the its Click event with the code:
tabControl1.SelectTab(1);
Let's assume the user entered something wrong on the first tab, so when they try to go to the second tab we want to send them back, so subscribe to the tab control's SelectedIndexChanged event with the code:
if(tabControl1.SelectedIndex == 1)
{
tabControl1.SelectTab(0);
}
Now run the program and click the button. You will notice that as judged by the highlighted tab at the top, the first tab page is the one that appears to be selected, as you'd expect. However, as judged by the tab page that actually appears in the body of the tab control, it's still the second tab page that shows up! Calls to various controls' Focus(), Update(), and Refresh() functions don't seem to help. What is going on here?
I repro. This is a generic problem with event handlers, you can confuse the stuffing out the native Windows control by jerking the floor mat like that. TreeView is another control that's very prone to this kind of trouble.
There's an elegant and general solution for a problem like this, you can use Control.BeginInvoke() to delay the command. It will execute later after the native control is done with the event generation and all side-effects have been completed. Which solves this problem as well, like this:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
if (tabControl1.SelectedIndex == 1) {
this.BeginInvoke(new Action(() => tabControl1.SelectTab(0)));
}
}

Categories

Resources