Click here to Skip to main content
16,012,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one user control & used it in two different forms. How do I get done two different actions when double click on the user controller.
for ex: In Form1 double click will opens a form(let's say it Form3). In Form2 double clicking will causes opening of Form4.
Posted
Updated 23-May-14 2:20am
v3
Comments
ZurdoDev 23-May-14 8:09am    
You'll have to pass in a parameter or set a property for the user control to know the difference.
oshan2csd 23-May-14 8:15am    
Thanks for the quick reply. Can you please provide me with some example code.
ZurdoDev 23-May-14 8:19am    
Create a property on your user control. Then each form can set that property.
oshan2csd 24-May-14 7:20am    
By creating a property in User Control & then set it's value in each form I achieved what I wanted. Thank you very much for your support.
ZurdoDev 24-May-14 8:02am    
Glad to hear. You are welcome.

create method to get next form by giving current form name. You can use this.Name to find the current form name and next form data can be taken from database table. when you receive next form name need to be opened; try with below code to open next page.
C#
public Form TryGetFormByName(string frmname)
{
    var formType = Assembly.GetExecutingAssembly().GetTypes()
        .Where(a => a.BaseType == typeof(Form) && a.Name == frmname)
        .FirstOrDefault();

    if (formType == null) // If there is no form with the given frmname
        return null;

    return (Form)Activator.CreateInstance(formType);
}
 
Share this answer
 
As mentioned in the comments, all you need to do is create a property on your user control and then each form can set that property and then your control will know whatever it is you need it to know.
 
Share this answer
 
Your question does not make sense. Each time you click-drag-drop a UserControl from the ToolBox onto a Form, you have created a separate instance of that UserControl, and it is added to the Controls Collection of the Container (like a Form, or a Panel on a Form) that you dropped it into.

You are not re-using the same/identical UserControl !

So, in Form1 you open the Properties/Events of the UserControl instance you drag-dropped onto Form1 and double-click on the DoubleClick Event: that writes the EventHandler stub for you in Form1. Do the same thing in Form2.

Of course we don't know anything about the functions of your UserControl, the other Controls inside it, etc. But, it's a rather strange design decision to have a DoubleClick on a UserControl itself do something "significant" like opening another Form.

I suggest you consider putting a Button on the UserControl with its Text clearly indicating what it does,and use that to open another Form.
 
Share this answer
 
Comments
oshan2csd 24-May-14 0:07am    
Thanks for the answer. As you've mentioned writing code added to the form. so is it ok with the user control's logic? what happened to the usability ?
BillWoodruff 24-May-14 1:41am    
What possible problems do you see with "usability" ? Keep in mind that each instance of the UserControl is a separate "entity" that belongs to the ContainerControl, or Form, it is sited in.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900