I'm adding an event for the Tabpages that are dynamically created. However, when I interact with the UI, the event is not called. Help! :)
I already tried below but doesn't work:
Event handling for dynamically generated controls
C# TabControl TabPage passing events
private void FileListView_DoubleClick(object sender, EventArgs e)
{
if (FileListView.SelectedIndices != null && FileListView.SelectedIndices.Count > 0)
{
FileListViewTask.Expand(settingsForm, FileListView, FileListView.SelectedIndices[0]);
string filepathSelected=FileListViewTask.getFilePath(FileListView.SelectedIndices[0]);
if (filepathSelected != "")
{
bool tabAlreadyExists = false;
foreach (TabPage tabpage in SourceTabControl.TabPages)
{
if (tabpage.GetNextControl(new Label(), true).Text == filepathSelected)
{
SourceTabControl.SelectTab(tabpage);
tabAlreadyExists = true;
break;
}
}
if (!tabAlreadyExists)
{
TabPage tabpage = (new CodePageGenerator(filepathSelected)).createPage();
tabpage.Parent = SourceTabControl;
tabpage.MouseDoubleClick += new MouseEventHandler(tabpage_MouseDoubleClick);
tabpage.Click += new EventHandler(this.tabpage_Click);
SourceTabControl.SelectTab(tabpage);
}
}
}
}
void tabpage_Click(object sender, EventArgs e)
{
TabPage tabpage = (TabPage)sender;
SourceTabControl.TabPages.Remove(tabpage);
MessageBox.Show("yow");
}
void tabpage_MouseDoubleClick(object sender, MouseEventArgs e)
{
TabPage tabpage = (TabPage)sender;
SourceTabControl.TabPages.Remove(tabpage);
MessageBox.Show("yow");
}
UPDATE
the code above works when a click occurs inside the TabPage content. But doesn't work when tabs are clicked
I already figured it out!
The key is to add an event to TabControl and not TabPage! The event on clicking the tab buttons are passed to the TabControl.
Initialize an eventhandler
SourceTabControl.MouseDoubleClick += new MouseEventHandler(SourceTabControl_MouseDoubleClick);
then,
private void SourceTabControl_MouseDoubleClick(object sender, MouseEventArgs e)
{
SourceTabControl.TabPages.Remove(SourceTabControl.SelectedTab);
}