/// <summary> /// Shows an alert window. /// </summary> /// <param name="caption">The caption of the alert window.</param> /// <param name="message">The message to display in the alert window.</param> /// <param name="image">The image to display in the alert window. /// Must meet the size specified in <see cref="ImageWidth"/> and <see cref="ImageHeight"/> if /// <see cref="EnforceImageSize"/> is true. /// Can be null.</param> /// <param name="actionClick">An action to invoke when the alert window is clicked. /// The input object is the value supplied in <paramref name="message"/>.</param> /// <remarks>Will invoke on the main thread if needed.</remarks> public static void ShowAlert(string caption, string message, Bitmap image, Action actionClick) { if (_form == null) { throw new Exception("AlertManager have not been initialized yet."); } if (string.IsNullOrWhiteSpace(message)) { throw new ArgumentException("The message cannot be empty or consist only of white-space characters.", nameof(message)); } //Should image size be enforced? if (image != null && EnforceImageSize && (image.Size.Width != ImageWidth || image.Size.Height != ImageHeight)) { throw new ArgumentException(string.Format("Alert images must be of size {0}x{1}", ImageWidth, ImageHeight), nameof(image)); } var info = new CustomAlertInfo(caption, message, image) { ClickAction = actionClick }; //If the call to ShowAlert originated on a thread other than the main thread, // we invoke the call to ShowAlertInternal on the main thread. if (_form.InvokeRequired) { _form.Invoke(new Action(() => ShowAlertInternal(info))); } else { ShowAlertInternal(info); } }
private static void ShowAlertInternal(CustomAlertInfo alertInfo) { //Lock the sync object to prevent multiple threads calling mAlertControl.Show() at the same time. //This should help prevent the hot tracking problem explained below. lock (_alertSync) { //If no click action have been set then we disable hot tracking to prevent text from appearing like a link. //note: This might cause problems if two items are shown right after each other i.e. // Alert1 is shown with AllowHotTrack set to false; but before Alert1 is shown to the user, // Alert2 changes AllowHotTrack to true when it is shown. //This have not been tested. if (alertInfo.ClickAction == null) { _alertControl.AllowHotTrack = false; } else { _alertControl.AllowHotTrack = true; } _alertControl.Show(_form, alertInfo); } }