none
How can i clone a BindingNavigator object? RRS feed

  • Question

  • Hi there!

    I tried this to clone controls from one TabPage (tab1) to another (tab2) and it works fine with most of the controls but the BindingNavigator isn' t cloned correctly:

    foreach (Control ctl in tab1.Controls)
    {
    	try
    	{
    		var typ = ctl.GetType();
    		var ctl_clone1 = Activator.CreateInstance(typ);
    		PropertyInfo[] controlProperties = typ.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    		foreach (PropertyInfo propInfo in controlProperties)
    		{
    			if (propInfo.CanWrite)
    			{
    				if (propInfo.Name != "WindowTarget")
    				{
    					propInfo.SetValue(ctl_clone1, propInfo.GetValue(ctl));
    				}
    			}
    		}
    		Control ctl_clone2 = (Control)ctl_clone1;
    		tab2.Controls.Add(ctl_clone2);
    	}
    	catch (Exception ex)
    	{
    		Debug.WriteLine("Exception: " + ex.Message + " - TargetSite:" + ex.TargetSite + " - Source: " + ex.Source + " - InnerException: " + ex.InnerException.Message.ToString());
    	}
    }
    

    What is the reason, that I cannot clone a BindingNavigator control?

    Greetings,

    Christoph

    • Moved by Kristin Xie Tuesday, August 4, 2015 8:40 AM winform related
    Monday, August 3, 2015 1:14 PM

Answers

  • Hi,

    Check this:

    https://support.microsoft.com/en-us/kb/828991

    To work around this problem, either rename the file that   exists in the current application directory so that this file has a different   file name from the dependency of the loaded assembly, or install the dependency   in the Global Assembly Cache (GAC) that has precedence over the current   application directory during probing.

    Rename the File That Exists in the Current Application Directory

    Thursday, August 6, 2015 9:10 AM

All replies

  • What are the symptoms in regards to cloning?

    Why not create a BindingNavigator for each tab or place the BindingNavigator outside of the tab control?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    Monday, August 3, 2015 2:13 PM
    Moderator
  • Hi Christoph,

    Based on your description, I am afraid that you posted in an inappropriate forum.

    could you tell me what's your project? Winform or WPF? I will help redirect to more appropriate forum.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 4, 2015 6:36 AM
  • Hi Kristin,

    it is a Winform application, where I have tried to clone an BindingNavigator. Is this the right forum?

    Best regards,

    Christoph

    Tuesday, August 4, 2015 8:37 AM
  • Hi Kristin,

    it is a Winform application, where I have tried to clone an BindingNavigator. Is this the right forum?

    Best regards,

    Christoph

    No,  this forum is for C# code issues in specific. I will move your case to Winform forum for better support.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 4, 2015 8:40 AM
  • Problem is that I need to do a deep clone of BindingNavigator i.e. because of the objects in ToolStripItemCollection Items: they have to be cloned, too.

    Maybe this could be a part of the solution: How can i deep clone an instance of any object?

    I've tried to integrate it and my Sourcecode looks now like this:

    foreach (Control ctl in tab1.Controls) { try { if (ctl != null) { object obj_clone = CloneObject(ctl); if (obj_clone != null && obj_clone is Control) { Control ctl_clone = (Control)CloneObject(ctl); tab2.Controls.Add(ctl_clone); } } else { } } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message + " - TargetSite:" + ex.TargetSite + " - Source: " + ex.Source + " - InnerException: " + ex.InnerException.Message.ToString()); } } private Object CloneObject(Object obj) { var typ = obj.GetType(); var obj_clone = CreateInstanceOfType(typ); PropertyInfo[] controlProperties = typ.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo propInfo in controlProperties) { if (propInfo.CanWrite && propInfo.Name != "WindowTarget") { try { if (propInfo.PropertyType.IsValueType || propInfo.PropertyType.IsEnum || propInfo.PropertyType.Equals(typeof(System.String))) { propInfo.SetValue(obj_clone, propInfo.GetValue(obj)); } else { object value = propInfo.GetValue(obj); if (value == null) propInfo.SetValue(obj_clone, null); else propInfo.SetValue(obj_clone, CloneObject(value)); } } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message + " - TargetSite:" + ex.TargetSite + " - Source: " + ex.Source + " - InnerException: " + ex.InnerException.Message.ToString()); } } } return (Object)obj_clone; }

    public object CreateInstanceOfType(Type type)
    {
        // First make an instance of the type.
        object instance = null;

        // If there is an empty constructor, call that.
        if (type.GetConstructor(Type.EmptyTypes) != null)
            instance = Activator.CreateInstance(type);
        else
        {
            // Otherwise get the first available constructor and fill in some default values.
            // (we are trying to set all properties anyway so it shouldn't be much of a problem).
            ConstructorInfo ci = type.GetConstructors()[0];
            ParameterInfo[] parameters = ci.GetParameters();

            object[] ciParams = new object[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                if (parameters[i].DefaultValue != null)
                {
                    ciParams[i] = parameters[i].DefaultValue;
                    continue;
                }

                Type paramType = parameters[i].ParameterType;
                ciParams[i] = CreateInstanceOfType(paramType);
            }

            instance = ci.Invoke(ciParams);
        }

        return instance;
    }


    But I've got exceptions, i.e. System.Reflection.TargetInvocationException

    Greetings,

    Christoph

    • Edited by chris_rall Tuesday, August 4, 2015 12:38 PM
    Tuesday, August 4, 2015 12:06 PM
  • Hi,

    Check this:

    https://support.microsoft.com/en-us/kb/828991

    To work around this problem, either rename the file that   exists in the current application directory so that this file has a different   file name from the dependency of the loaded assembly, or install the dependency   in the Global Assembly Cache (GAC) that has precedence over the current   application directory during probing.

    Rename the File That Exists in the Current Application Directory

    Thursday, August 6, 2015 9:10 AM