Tabpages
-
Ive some problems with tabpages i have one form with one tabcontrol with one tabpage The first tabpage contain a button. When you click the button a new tabpage will be created and added to the tabcontrol. This works. But the second created tabpage contain a button to for closing that page. I used the next code: ((Tabcontrol)this.parent).Tabpages.remove (this); It works but after i used these code i connot close the form (the close button doesnt work anymore) However a "Application.exit" command in the menus works. Can anyone help me? Ive another problem: When i click the button in the first tabpage, how con i focus the second? I used the tabpage.focus (); command but it doesnt work Jonathan Slenders
-
Ive some problems with tabpages i have one form with one tabcontrol with one tabpage The first tabpage contain a button. When you click the button a new tabpage will be created and added to the tabcontrol. This works. But the second created tabpage contain a button to for closing that page. I used the next code: ((Tabcontrol)this.parent).Tabpages.remove (this); It works but after i used these code i connot close the form (the close button doesnt work anymore) However a "Application.exit" command in the menus works. Can anyone help me? Ive another problem: When i click the button in the first tabpage, how con i focus the second? I used the tabpage.focus (); command but it doesnt work Jonathan Slenders
When remove a
TabPage
orControl
from a collection like this, make sure you do it in the main UI thread, no another thread. This can lead to problems processing the message loop that internally dispatches messages to the appropriate windows. There are other causes of this behavior and we've discussed it before. You could try searching the comments to find more causes. Instead ofTabPage.Focus
, useTabControl.SelectedIndex
orTabControl.SelectedTab
:// Initialize the TabPage and its contents
TabPage page = new TabPage("Second");
//...tabControl1.TabPages.Add(page);
tabControl1.SelectedTab = page;-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
When remove a
TabPage
orControl
from a collection like this, make sure you do it in the main UI thread, no another thread. This can lead to problems processing the message loop that internally dispatches messages to the appropriate windows. There are other causes of this behavior and we've discussed it before. You could try searching the comments to find more causes. Instead ofTabPage.Focus
, useTabControl.SelectedIndex
orTabControl.SelectedTab
:// Initialize the TabPage and its contents
TabPage page = new TabPage("Second");
//...tabControl1.TabPages.Add(page);
tabControl1.SelectedTab = page;-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
// The TabControl.SelectedIndex Property works good. Thanks for that. // But i doesnt know how i can do it in another thread. // Now i have created an instance of a class inheritted from the Tabpage. // That class contain a property which refer to a function with the delegate: public delegate void delRemoveTabPage (TabPage tabPage1); // When i create that instance i set the RemoveTabPage property TabPage1.RemoveTabPage += new delRemoveTabPage (RemoveTabpage); // My RemoveTabPage function is these void RemoveTabpage (TabPage tabPage1) { tabCtrlMain1.TabPages.Remove (tabPage1); } // It works (This tabpage disapears but the close butten doesnt work anymore like i said before.) // Well, i have an other possibility but i don't know if this is good: // the TabPage.Hide ( ) method works perfect. And if i add code in the tabpage thad set his childs null i think there will be not mush memory used for thad hidden tabpage. Is this a good method?
-
// The TabControl.SelectedIndex Property works good. Thanks for that. // But i doesnt know how i can do it in another thread. // Now i have created an instance of a class inheritted from the Tabpage. // That class contain a property which refer to a function with the delegate: public delegate void delRemoveTabPage (TabPage tabPage1); // When i create that instance i set the RemoveTabPage property TabPage1.RemoveTabPage += new delRemoveTabPage (RemoveTabpage); // My RemoveTabPage function is these void RemoveTabpage (TabPage tabPage1) { tabCtrlMain1.TabPages.Remove (tabPage1); } // It works (This tabpage disapears but the close butten doesnt work anymore like i said before.) // Well, i have an other possibility but i don't know if this is good: // the TabPage.Hide ( ) method works perfect. And if i add code in the tabpage thad set his childs null i think there will be not mush memory used for thad hidden tabpage. Is this a good method?
Next time you post, please do not prefix each sentance with "//". It's very hard to read and completely unnecessary. See the
Control.InvokeRequired
andControl.Invoke
methods. These make sure that the action is performed in the main thread by invoking it from another method. For instance:private delegate void RemoveHandler(TabPage);
//...
private void removeButton_Click(object sender, EventArgs e)
{
Button b = sender as Button; if (b == null) return;
TabPage tab = b.Parent as TabPage; if (tab == null) return;
if (this.tabControl1.InvokeRequired)
{
RemoveHandler handler =
new RemoveHandler(this.tabControl1.TabPages.Remove);
this.tabControl1.Invoke(d, new object[] {tab});
}
else this.tabControl1.TabPages.Remove(tab);
}And no, simply hiding the tab is not a very good idea unless you plan on showing it again. When you're done with objects, dispose of them.
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----