Problem in accessing an event of a user control from another in Win Forms
-
Hi Guys, I am having an issue with the Windows User Contol communication. I am creating a winform application. I have a tree view in the page (UC1). I am populating data to that from DB. on the leaf node select, i have to call an even in a Grid (User Control2) which will populate the data depending on the selection of the treenode text. Here the Tree & Grid are usercontrols. I have tried to use events for doing the same. But there are issue, If any one can help me, i will be thankful to them Hi Guys, Thanks for your response. This is what i am trying to do. GridUser Control.cs public partial class LogGridViewUserControl : UserControl { public delegate void Data_Bind(object sender, LogGridEventArgs e); public event Data_Bind dataBindHandler; public LogGridViewUserControl() { InitializeComponent(); } public void OnDataBind(Object sender, LogGridEventArgs e) { if (dataBindHandler != null) { string str = CallBusiness(e.Connection); } } } public class LogGridEventArgs { public string Connection { get; set; } public LogGridEventArgs(string connection) { Connection = connection; } } TreeUserControl.cs public partial class TestTreeControl : UserControl { public TestTreeControl() { InitializeComponent(); // BuilTree() } private void trvTest_NodeSelect(object sender, TreeViewEventArgs e) { if (e.Node.Nodes.Count == 0) { I need to call the OnDataBind event in the Grid to populate the data in the Grid } } } In the main form, i will add the both user controls. How do i call the OnDataBind evnthandler of the GridView when a node selection is changed in the treeview. I am not an expert in the Winform development/ not so good with delegates. If any one can chip in, it will be of great help to me. Thanks Tutu
-
Hi Guys, I am having an issue with the Windows User Contol communication. I am creating a winform application. I have a tree view in the page (UC1). I am populating data to that from DB. on the leaf node select, i have to call an even in a Grid (User Control2) which will populate the data depending on the selection of the treenode text. Here the Tree & Grid are usercontrols. I have tried to use events for doing the same. But there are issue, If any one can help me, i will be thankful to them Hi Guys, Thanks for your response. This is what i am trying to do. GridUser Control.cs public partial class LogGridViewUserControl : UserControl { public delegate void Data_Bind(object sender, LogGridEventArgs e); public event Data_Bind dataBindHandler; public LogGridViewUserControl() { InitializeComponent(); } public void OnDataBind(Object sender, LogGridEventArgs e) { if (dataBindHandler != null) { string str = CallBusiness(e.Connection); } } } public class LogGridEventArgs { public string Connection { get; set; } public LogGridEventArgs(string connection) { Connection = connection; } } TreeUserControl.cs public partial class TestTreeControl : UserControl { public TestTreeControl() { InitializeComponent(); // BuilTree() } private void trvTest_NodeSelect(object sender, TreeViewEventArgs e) { if (e.Node.Nodes.Count == 0) { I need to call the OnDataBind event in the Grid to populate the data in the Grid } } } In the main form, i will add the both user controls. How do i call the OnDataBind evnthandler of the GridView when a node selection is changed in the treeview. I am not an expert in the Winform development/ not so good with delegates. If any one can chip in, it will be of great help to me. Thanks Tutu
I'm not quite sure what you want but I think you may have it the wrong way around. Try creating a NodeSelected event in the TestTreeControl control and then fire the event where you need to call the OnDataBind method. Then in the main form subscribe to that event (see pseudo code below) MyTestTree.NodeSelected += new NodeSelectedDelegateHandler(MyTestTree_NodeSelected); void MyTestTree_NodeSelected(object source, LogGridEventArgs e) { MyLogGridViewUserControl.OnDataBind(source, e); } Also your LogGridEventArgs class should inherit from EventArgs Cheers
Architecture is extensible, code is minimal.