Beginner TabControl question

T

Trevor

I am making a "switchbar" similar to mIRC & Visual Studio .NET. It is
basically a TabControl docked at the top of a MDI container. There is a
corresponding button for each MDI child form in the MDI container. The user
can click on a tab in the TabControl to activate that MDI window. I have
written the functions to do this, but I need to sink in to the event in the
TabControl when a button changes focus (i.e., the user clicks from one
button to the next, I want to know what button it is about to change the
focus to so I can activate that MDI window).


This function is called to add a new MDI form and add a corresponding button
to the switchbar

/// <summary>
/// Show a Form as a MdiChild and add a button to the TabControl.
/// </summary>
public void AddMdiChild(Form frmForm)
{
string frmText = frmForm.Text;
bool tabDuplicate = false;
int tabCount = tabSwitchview.TabCount;
int tabIndex;
TabPage tabPage = null;

for (tabIndex = 0; tabIndex < tabCount; tabIndex++)
{
tabPage = tabSwitchview.TabPages[tabIndex];
if (tabPage.Text == frmText)
{
tabDuplicate = true;
break;
}
}
if (tabDuplicate == false)
{
frmForm.MdiParent = this;
tabPage = new TabPage(frmText);
tabSwitchview.TabPages.Add(tabPage);
frmForm.Show();
frmForm.Activate();
tabSwitchview.TabIndex = tabCount;
tabSwitchview.TabPages[tabIndex].Select();
}
else
{
tabSwitchview.TabIndex = tabIndex;
tabSwitchview.TabPages[tabIndex].Select();
foreach (Form frmMdiChild in MdiChildren)
{
if (frmMdiChild.Text == frmText)
{
frmMdiChild.Activate();
break;
}
}
}
}



/// <summary>
/// Remove a MdiChild and remove the corresponding button on the TabControl.
/// </summary>
public void RemoveMdiChild(string frmText)
{
foreach (TabPage tabPage in tabSwitchview.TabPages)
{
if (tabPage.Text == frmText)
{
tabSwitchview.TabPages.Remove(tabPage);
break;
}
}
}

****************************************************************************
*************
This doesn't seem to be the event I want. It does get fired when I am
interested, but it contains the old index.
I should be able to run this code somewhere where I have just changed to a
new Tab button, but I don't know how.
****************************************************************************
*************

private void OnSelectedIndexChanged(object sender, System.EventArgs e)
{
// If the user clicks on the Tab with the text "abc" then search for that
MDI child form with text "abc" and activate it.
string frmText = tabSwitchview.TabPages[tabSwitchview.TabIndex].Text;
foreach (Form frmMdiChild in MdiChildren)
{
if (frmMdiChild.Text == frmText)
{
frmMdiChild.Activate();
break;
}
}
}
 
J

John Little

Instead of

string frmText = tabSwitchview.TabPages[tabSwitchview.TabIndex].Text;


use

string frmText = tabSwitchview.TabPages[tabSwitchview.SelectedIndex].Text;


HTH

John Little

Trevor said:
I am making a "switchbar" similar to mIRC & Visual Studio .NET. It is
basically a TabControl docked at the top of a MDI container. There is a
corresponding button for each MDI child form in the MDI container. The user
can click on a tab in the TabControl to activate that MDI window. I have
written the functions to do this, but I need to sink in to the event in the
TabControl when a button changes focus (i.e., the user clicks from one
button to the next, I want to know what button it is about to change the
focus to so I can activate that MDI window).


This function is called to add a new MDI form and add a corresponding button
to the switchbar

/// <summary>
/// Show a Form as a MdiChild and add a button to the TabControl.
/// </summary>
public void AddMdiChild(Form frmForm)
{
string frmText = frmForm.Text;
bool tabDuplicate = false;
int tabCount = tabSwitchview.TabCount;
int tabIndex;
TabPage tabPage = null;

for (tabIndex = 0; tabIndex < tabCount; tabIndex++)
{
tabPage = tabSwitchview.TabPages[tabIndex];
if (tabPage.Text == frmText)
{
tabDuplicate = true;
break;
}
}
if (tabDuplicate == false)
{
frmForm.MdiParent = this;
tabPage = new TabPage(frmText);
tabSwitchview.TabPages.Add(tabPage);
frmForm.Show();
frmForm.Activate();
tabSwitchview.TabIndex = tabCount;
tabSwitchview.TabPages[tabIndex].Select();
}
else
{
tabSwitchview.TabIndex = tabIndex;
tabSwitchview.TabPages[tabIndex].Select();
foreach (Form frmMdiChild in MdiChildren)
{
if (frmMdiChild.Text == frmText)
{
frmMdiChild.Activate();
break;
}
}
}
}



/// <summary>
/// Remove a MdiChild and remove the corresponding button on the TabControl.
/// </summary>
public void RemoveMdiChild(string frmText)
{
foreach (TabPage tabPage in tabSwitchview.TabPages)
{
if (tabPage.Text == frmText)
{
tabSwitchview.TabPages.Remove(tabPage);
break;
}
}
}

****************************************************************************
*************
This doesn't seem to be the event I want. It does get fired when I am
interested, but it contains the old index.
I should be able to run this code somewhere where I have just changed to a
new Tab button, but I don't know how.
****************************************************************************
*************

private void OnSelectedIndexChanged(object sender, System.EventArgs e)
{
// If the user clicks on the Tab with the text "abc" then search for that
MDI child form with text "abc" and activate it.
string frmText = tabSwitchview.TabPages[tabSwitchview.TabIndex].Text;
foreach (Form frmMdiChild in MdiChildren)
{
if (frmMdiChild.Text == frmText)
{
frmMdiChild.Activate();
break;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top