I am trying to trigger a Button1_Click event located in UserControl2 from UserControl1.
Controls are Loaded dynamically from my Main Form
//UserControl2
public partial class ItemsModule : UserControl
{
private void ButtonRefreshProperties_Click(Object sender, EventArgs e)
{
RefreshControls();
}
public void RefreshControls()
{
SomeCode();
}
}
/// MainForm this is how i add the Control from the Main Form
ItemsModule im = new ItemsModule();
im.Name = "ItemsModule";
flpModules.Controls.Add(im);
//UserControl1
public partial class TreeViewControl : UserControl
{
private void ItemTreeView_MouseDoubleClick(Object sender, MouseEventArgs e)
{
String ItemId = ItemTreeView.SelectedNode.Name;
Variables.CurrentItemID = ItemId;
if (LoadedModules.Items)
{
//Here I would like a way to trigger ItemsModule.ButtonRefreshProperties_Click
}
}
}
UserControl1
will need to have a references to UserControl2
. Once you have a reference UserControl1
should call a method on UserControl2
that causes the event to be raised.
You can pass the reference to UserContro2
in to UserControl1
via the constructor, a sub, or a property depending on how your application is structured. You should store the reference in a private field of UserControl1
so that you can access it from the DoubleClick
event handler.
It is also possible to set this up in other ways. You could make UserControl2
be a property on the parent form, then UserControl1 can cast it's .Parent
property in to the correct type and use that property as a reference.
You could also set up a 3rd class as a singleton
that has a reference to UserControl2
.
The thing that all these solutions have in common is that somehow UserControl1
needs to have a reference to UserControl2
to be able to do anything with it.
You could avoid the reference problem by having UserControl1
raise an event that it's parent form listens for. Since the parent form should already have a reference to all the controls on it, you can make the form proxy the calls in to UserControl2
for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.