/// <summary>
        /// Attempts to attach existing targets that have yet no textboxes to controls that exist on specified form if appropriate
        /// </summary>
        /// <remarks>
        /// Setting <see cref="AllowAccessoryFormCreation"/> to true (default) actually causes target to always have a text box
        /// (after having <see cref="InitializeTarget"/> called), so such targets are not affected by this method.
        /// </remarks>
        /// <param name="form">a Form to check for RichTextBoxes</param>
        /// /// <param name="configuration">NLog's configuration. <c>null</c> is not allowed</param>
        public static void ReInitializeAllTextboxes(Form form, LoggingConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration), "NLog configuration is empty");
            }

            foreach (var target in GetRichTextBoxTargets(configuration))
            {
                if (target.FormName == form.Name)
                {
                    //can't use InitializeTarget here as the Application.OpenForms would not work from Form's constructor
                    RichTextBox textBoxControl = FormHelper.FindControl <RichTextBox>(target.ControlName, form);
                    if (textBoxControl != null && !textBoxControl.IsDisposed)
                    {
                        if (target.TargetRichTextBox == null ||
                            target.TargetRichTextBox.IsDisposed ||
                            target.TargetRichTextBox != textBoxControl
                            )
                        {
                            target.AttachToControl(form, textBoxControl);
                        }
                    }
                }
            }
        }
        private void FindControlAndSendTheMessage(string logMessage)
        {
            Form form = null;

            if (Form.ActiveForm != null)
            {
                form = Form.ActiveForm;
            }

            if (Application.OpenForms[FormName] != null)
            {
                form = Application.OpenForms[FormName];
            }

            if (form == null)
            {
                InternalLogger.Info("Form {0} not found", FormName);
                return;
            }

            Control control = FormHelper.FindControl(ControlName, form);

            if (control == null)
            {
                InternalLogger.Info("Control {0} on Form {1} not found", ControlName, FormName);
                return;
            }
            try
            {
                control.BeginInvoke(new DelSendTheMessageToFormControl(SendTheMessageToFormControl), control, logMessage);
            }
            catch (Exception ex)
            {
                InternalLogger.Warn(ex.ToString());

                if (LogManager.ThrowExceptions)
                {
                    throw;
                }
            }
        }
 /// <summary>
 /// Attempts to attach existing targets that have yet no textboxes to controls that exist on specified form if appropriate
 /// </summary>
 /// <remarks>
 /// Setting <see cref="AllowAccessoryFormCreation"/> to true (default) actually causes target to always have a textbox
 /// (after having <see cref="InitializeTarget"/> called), so such targets are not affected by this method.
 /// </remarks>
 /// <param name="form">a Form to check for RichTextBoxes</param>
 public static void ReInitializeAllTextboxes(Form form)
 {
     InternalLogger.Info("Executing ReInitializeAllTextboxes for Form {0}", form);
     foreach (Target target in LogManager.Configuration.AllTargets)
     {
         RichTextBoxTarget textboxTarget = target as RichTextBoxTarget;
         if (textboxTarget != null && textboxTarget.FormName == form.Name)
         {
             //can't use InitializeTarget here as the Application.OpenForms would not work from Form's constructor
             RichTextBox textboxControl = FormHelper.FindControl <RichTextBox>(textboxTarget.ControlName, form);
             if (textboxControl != null && !textboxControl.IsDisposed)
             {
                 if (textboxTarget.TargetRichTextBox == null ||
                     textboxTarget.TargetRichTextBox.IsDisposed ||
                     textboxTarget.TargetRichTextBox != textboxControl
                     )
                 {
                     textboxTarget.AttachToControl(form, textboxControl);
                 }
             }
         }
     }
 }
        /// <summary>
        /// Initializes the target. Can be used by inheriting classes
        /// to initialize logging.
        /// </summary>
        protected override void InitializeTarget()
        {
            base.InitializeTarget();

            if (TargetRichTextBox != null)
            {
                //already initialized by ReInitializeAllTextboxes call
                return;
            }

            CreatedForm = false;
            Form        openFormByName;
            RichTextBox targetControl;

            if (AllowAccessoryFormCreation)
            {
                //old behaviour which causes creation of accessory form in case specified control cannot be found on specified form

                if (FormName == null)
                {
                    InternalLogger.Info("FormName not set, creating acceccory form");
                    CreateAccessoryForm();
                    return;
                }

                openFormByName = Application.OpenForms[FormName];
                if (openFormByName == null)
                {
                    InternalLogger.Info("Form {0} not found, creating accessory form", FormName);
                    CreateAccessoryForm();
                    return;
                }

                if (string.IsNullOrEmpty(ControlName))
                {
                    HandleError("Rich text box control name must be specified for {0}.", GetType().Name);
                    CreateAccessoryForm();
                    return;
                }

                targetControl = FormHelper.FindControl <RichTextBox>(ControlName, openFormByName);
                if (targetControl == null)
                {
                    HandleError("Rich text box control '{0}' cannot be found on form '{1}'.", ControlName, FormName);
                    CreateAccessoryForm();
                    return;
                }

                //finally attached to proper control
            }
            else
            {
                //new behaviour which postpones attaching to textbox if it's not yet available at the time,

                if (FormName == null)
                {
                    HandleError("FormName should be specified for {0}.{1}", GetType().Name, this.Name);
                    return;
                }

                if (string.IsNullOrEmpty(ControlName))
                {
                    HandleError("Rich text box control name must be specified for {0}.{1}", GetType().Name, this.Name);
                    return;
                }

                openFormByName = Application.OpenForms[FormName];
                if (openFormByName == null)
                {
                    InternalLogger.Info("Form {0} not found, waiting for ReInitializeAllTextboxes.", FormName);
                    return;
                }

                targetControl = FormHelper.FindControl <RichTextBox>(ControlName, openFormByName);
                if (targetControl == null)
                {
                    InternalLogger.Info("Rich text box control '{0}' cannot be found on form '{1}'. Waiting for ReInitializeAllTextboxes.", ControlName, FormName);
                    return;
                }

                //actually attached to a target, all ok
            }

            AttachToControl(openFormByName, targetControl);
        }