I have a user control that contains a tab control on it. The control is used on a form. When I debug/test the control events (by running the app and loading the form), they don't seem to be firing on the form. I want to take action when the user changes tabs.
Thanks in advance.
Daniel
Loading
Mike GoldPosted Aug 9, 2007, 11:58 PM
Here is a full sample:
User Control:
namespace
TestPropogateTabEvent{
public partial class UserControlWithTab : UserControl{
public UserControlWithTab(){
InitializeComponent();
}
public delegate void TabSelectionEventHandler(object sender, EventArgs e); public event TabSelectionEventHandler PropogateSelectionChange; private void tabControl1_SelectedIndexChanged(object sender, EventArgs e){ if (PropogateSelectionChange != null)
{
PropogateSelectionChange(sender, e);
}
}
}
}
Form Class:
namespace
TestPropogateTabEvent{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
userControlWithTab1.PropogateSelectionChange +=
new UserControlWithTab.TabSelectionEventHandler(userControlWithTab1_PropogateSelectionChange);}
void userControlWithTab1_PropogateSelectionChange(object sender, EventArgs e){
MessageBox.Show("Tab In User Control Pressed");}
}
}
Mike GoldPosted Aug 9, 2007, 11:38 PM
Hi Daniel,
I've constructed a sample in the reply below. basically you are "bubbling up" the event from the tab in the user control into the form.
DanielPosted Aug 9, 2007, 9:38 AM
UserControl.cs
(declared outside of class)
public delegate void TabPageClickHandler(object sender, TabPageClickEventArgs e);
(declared inside of class)
public event TabPageClickHandler TabPageClick;
protected void OnTabPageClick(string tabName)
{
//if anyone listening for event data
//if an object is registered to be notifed of the event
if (TabPageClick != null)
{
TabPageClickEventArgs args = new TabPageClickEventArgs();
args.TabName = tabName;
TabPageClick(this, args);
MessageBox.Show("OnTabPageClick fired");
}
}
//To run my test
private void tabClassProdEng_Selected(object sender, TabControlEventArgs e)
{
OnTabPageClick(e.TabPageIndex.ToString());
}
Form.cs
(within initializecomponent() section)
ucProdEngineering.TabPageClick +=new TabPageClickHandler(ucProdEngineering_TabPageClick);
// I get an error above: "The name 'ucProdEngineering_TabPageClick' does not exist in the current context"
Thanks.
Daniel
DanielPosted Aug 9, 2007, 9:08 AM
Can you recommend the proper event to put the user control side code into?
- tabPage_Click
- tabControl_TabIndexChanged
- ???
Is there a benefit to one over the other....?Thanks,
Daniel
DanielPosted Aug 8, 2007, 10:32 PM
Thanks a million for posted an example of this. I will try it tomorrow and let you know how it works out.
I really appreciate the help.
Daniel
Jan MontanoPosted Aug 8, 2007, 9:50 PM
public event TabControlEventHandler TabSelectedEvent;
tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
void tabControl1_Selected(object sender, TabControlEventArgs e)
{
}
Form1.cs
userControl11.TabSelectedEvent+= new TabControlEventHandler(userControl11_TabSelectedEvent);
void userControl11_TabSelectedEvent(object sender, TabControlEventArgs e)
{
}
*If you want to handle tabpage_click event, just call the tabselectedevent from the tabpage_click event instead.
DanielPosted Aug 8, 2007, 1:13 PM
I have read some initial info on custom events, but may need some additional help on the concept.
I want to handle the TabPage_click event (on my user control) on the form that that the control resides on. I am not quite sure how to pull that off.
Thanks in advance for any help you or ANYONE can provide.
Thanks,
Daniel
DanielPosted Aug 8, 2007, 11:17 AM
I will google customer events first and then contain post back if I am still having problems.
Daniel
Richard BlythePosted Aug 7, 2007, 5:08 PM
The event is fired but it is only visible inside the confines of you usercontrol class. If you want to trap the event inside the form you will need to write a custom event. Aw it ain't that hard! Do a google search on custom events and if you are still confused, let me know and I'll help you.
Cheers!
Richard