I have already created in c# from a drawn image inside my tab control to close together the mdi form. I am trying to make the opposite (i.e. pressing the "x" of the form and close its tab as well)
I tried to add a code in my child's closing even but it doesn't recognize functions I used in my main form (Form1).
any suggestions?
Thanks in advanced.
VulpesPosted Apr 19, 2011, 9:31 AM
VulpesPosted May 16, 2011, 10:23 AM
However, are you sure that's a good idea here, when you have code which depends on being able to find the correct tabpage by name?
George ParoglouPosted May 16, 2011, 4:25 AM
George ParoglouPosted May 1, 2011, 3:01 PM
cheers for you help vulpes.
If you have any new thought post it to try it.
VulpesPosted May 1, 2011, 12:29 PM
The code in Spoudastes_Card_FormClosed is virtually the same as the code in TabControl1_MouseClick so, if the former works but the latter doesn't, it's very hard to see why that should be.
The only slight difference is that I used RemoveAt() in the FormClosed handler but RemoveByKey() in TabControl1_MouseClick. You can try changing that if you like and I'd also try calling Refresh() on the tabcontrol in case it's a redrawing problem:
foreach (Form f in this.MdiChildren)
{
if (f.Name == tabControl1.SelectedTab.Name)
{
int index = tabControl1.SelectedIndex; // get the index of the selected page before we remove it
tabControl1.TabPages.RemoveAt(index);
tabControl1.Refresh();
if (index >= 1)
{
tabControl1.SelectedTab = tabControl1.TabPages[index - 1];
}
else (if index == 0 && tabControl1.TabPages.Count > 0) // if removing first page and there's at least one other
{
tabControl1.SelectedTab = tabControl1.TabPages[0]; // set to new first page
}
tabControl1.Refresh();
f.Close();
break;
}
}
George ParoglouPosted May 1, 2011, 9:41 AM
VulpesPosted May 1, 2011, 9:10 AM
George ParoglouPosted May 1, 2011, 8:17 AM
VulpesPosted May 1, 2011, 7:38 AM
if (index >= 1)
George ParoglouPosted May 1, 2011, 7:19 AM
VulpesPosted May 1, 2011, 7:13 AM
George ParoglouPosted May 1, 2011, 6:30 AM
VulpesPosted May 1, 2011, 6:13 AM
George ParoglouPosted May 1, 2011, 5:40 AM
for(int i = 0; i < tc.TabPages.Count; i++)
{
if (tc.TabPages[i].Name == this.Name) // or use Text properties {
tc.TabPages.RemoveAt(i);
break; // can't remove more than one tab}
}
in Spoudastes_Card_FormClosed and both work fine...cheers for your help Vulpes ;)
If I want to make as an active tab with its tabpage exactly the previous tab of the one that I close how this can be done. I am asking cause it always activates the first tab.
George ParoglouPosted May 1, 2011, 5:34 AM
VulpesPosted Apr 29, 2011, 2:57 PM
George ParoglouPosted Apr 29, 2011, 4:02 AM
VulpesPosted Apr 24, 2011, 3:28 PM
Sam HobbsPosted Apr 24, 2011, 2:48 PM
Perhaps if you describe what you need to do from the point of view of a user that knows nothing about programming, then it will be easier to understand what you need. Don't tell us how you want to write the program and then ask for help with that; tell us what you need to do. Please don't talk about it in terms of a tab control; describe the UI. Then if there is something more appropriate than a tab control then we can suggest the alternative.
George ParoglouPosted Apr 24, 2011, 2:45 PM
Let's assume we use this code:
public void TabControl1_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
TabControl tc = (TabControl)sender;
Point p = e.Location;
int _tabWidth = 0;
_tabWidth = this.tabControl1.GetTabRect(tc.SelectedIndex).Width - (_imgHitArea.X);
Rectangle r = this.tabControl1.GetTabRect(tc.SelectedIndex);
r.Offset(_tabWidth, _imgHitArea.Y);
r.Width = 13;
r.Height = 13;
if (r.Contains(p))
{
mnuTab.DropDownItems.RemoveByKey(tabControl1.SelectedTab.Name);
this.ActiveMdiChild.Close();
tabControl1.SelectedTab.Dispose();
}
}
this seems to work fine
VulpesPosted Apr 24, 2011, 2:17 PM
George ParoglouPosted Apr 24, 2011, 4:07 AM
By removing tabControl1.SelectedTab.Dispose(); from TabControl1_MouseClick it closes the tabpage and leaves open its tab...That's why I mentioned earlier that when one of these works ok the other acts incorrectly... There are not "synchronized".
VulpesPosted Apr 23, 2011, 4:39 PM
What's all this about: "it closes the tabpage but all tabs are active and doesn't close at all" ?
All the remaining tabs will be active and I thought you didn't want any more to close?
George ParoglouPosted Apr 23, 2011, 4:16 PM
When the tabs' x doesn't work tabpages' x works fine and just the opposite...:(
VulpesPosted Apr 23, 2011, 3:56 PM
George ParoglouPosted Apr 23, 2011, 3:06 PM
{
TabControl tc = (TabControl)sender;
Point p = e.Location;
int _tabWidth = 0;
_tabWidth = this.tabControl1.GetTabRect(tc.SelectedIndex).Width - (_imgHitArea.X);
Rectangle r = this.tabControl1.GetTabRect(tc.SelectedIndex);
r.Offset(_tabWidth, _imgHitArea.Y);
r.Width = 13;
r.Height = 13;
if(r.Contains(p))
{
//if you want to close tab, you must close the corresponding form
mnuTab.DropDownItems.RemoveByKey(tabControl1.SelectedTab.Name); //this is why you must make sure menu's and tab page's name are same
this.ActiveMdiChild.Close(); //because of synchronize routine, you must close the form first before tab.
tabControl1.SelectedTab.Dispose(); //if tab first, activated tab will change and so the child form, this will be the closed form.
//the wrong form will close....
}
}
VulpesPosted Apr 23, 2011, 2:43 PM
Can you post the code of the TabControl1_MouseClick eventhandler, as you currently have it.
George ParoglouPosted Apr 23, 2011, 2:19 PM
any suggestion?
George ParoglouPosted Apr 23, 2011, 1:32 PM
for (int i = 0; i < tc.TabPages.Count; i++)
this shoud be removed.
cheers Vulpes
VulpesPosted Apr 23, 2011, 1:18 PM
George ParoglouPosted Apr 23, 2011, 1:11 PM
any idea?
VulpesPosted Apr 22, 2011, 6:37 PM
If that child is handling the Form_Closed event then as soon as you call ActiveMdiChild.Close() that will trigger the Form_Closed event which will attempt to remove the tab associated with the form. However, you've then removing what then becomes the selected tab within the MouseClick eventhandler so you're potentially removing two tabs!
It would be better therefore not to remove any tabs in the MouseClick handler (unless they're not associated with the relevant form) and leave the Form_Closed event which gets triggered by the Close() method to remove the tab associated with the form.
George ParoglouPosted Apr 22, 2011, 3:44 PM
any idea what it is going wrong?
VulpesPosted Apr 21, 2011, 5:39 AM
Before I can move on to anything else, I need to know whether the Form_Closed evenhandler works or not?
If it doesn't work, then there must be some unexpected side effect that we need to track down.
George ParoglouPosted Apr 21, 2011, 4:29 AM
VulpesPosted Apr 20, 2011, 6:36 PM
However, some of the things you've said recently are making me wonder now whether two tabs are removed when you close the child form (which is what I've been assuming) or only when you close a tabpage from the first form?
George ParoglouPosted Apr 20, 2011, 3:13 PM
I attached the screenshot if you can see anything strange....:(
I do believe that the problem is in TabControl1_MouseClick as when clicked in the X pic of first tab, it closes the last two tabs but it keeps open their tabpages. the error that prints is in tabControl1.SelectedTab.Dispose(); inside the TabControl1_MouseClick...
VulpesPosted Apr 20, 2011, 2:59 PM
George ParoglouPosted Apr 20, 2011, 2:51 PM
what do you mean "to put a breakpoint"? how can be done?
VulpesPosted Apr 20, 2011, 9:51 AM
But, as I said in my previous post, the only way to find out is to put a breakpoint in the FormClosed eventhandler and single step from there to see what (if any) other handlers are getting called.
George ParoglouPosted Apr 20, 2011, 9:36 AM
George ParoglouPosted Apr 19, 2011, 3:13 AM
VulpesPosted Apr 18, 2011, 4:49 PM
If the code is closing two tab pages at once, then either there are two pages with the same key or the closure of the second one is being triggered by some other aspect of your program.
George ParoglouPosted Apr 18, 2011, 1:52 PM
private void Spoudastes_Card_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 mainForm = (Form1)Application.OpenForms["Form1"];
TabControl tc = (TabControl)mainForm.Controls["tabControl1"];
tc.TabPages.RemoveByKey((string)this.Name);
}
Can I, in any way, make tabControl1 variable active in my second form?
VulpesPosted Apr 18, 2011, 9:02 AM
George ParoglouPosted Apr 18, 2011, 7:55 AM
It seems to work for just for more than one tabpages already opened. At the last tabpage prints an error in the number of index that doesn't exist.
also noticed that this depends on the closing order of the tabpages( i.e. if I close the 1 out of 2 tabpages and open a brand new tabpage it stucks every tab to the same tabpage....:(
VulpesPosted Apr 18, 2011, 7:14 AM
If the TabControl is on Form2 then it would close automatically when Form2 was closed.
If it's on Form1 but Form2 isn't inside a TabPage, then what do you mean about Form2 having a specific tab?
If you mean that Form2 relates to the second TabPage, then this code should work:
George ParoglouPosted Apr 18, 2011, 4:39 AM
I used the code you gave me but it gives an error in 3rd line.
VulpesPosted Apr 17, 2011, 11:50 AM
Are you saying that there's a TabControl on Form1 and one of its TabPages contains an instance of Form2?
If so, then I don't see how Form2 can be an MDI child of Form1 because Form1 is not its container - the TabPage is.
If this is the situation and you wanted to close the TabPage on Form1 when Form2 was closed, then you'd need to handle Form2's FormClosed event as follows:
George ParoglouPosted Apr 17, 2011, 10:41 AM
VulpesPosted Apr 17, 2011, 7:56 AM
If Form1 isn't the MDI parent, then you can get a reference instead using:
Form1 mainForm = (Form1)Application.OpenForms["Form1"];