public static void StoredPrintToRichTextBox(
            RichTextBox rtb, StoredPrint sp, StoredPrintColors colors, bool isExternalError = false,
            RoutedEventHandler linkClickHandler = null)
        {
            // access
            if (rtb == null || sp == null)
            {
                return;
            }

            // append
            TextRange tr = new TextRange(rtb.Document.ContentEnd, rtb.Document.ContentEnd);

            tr.Text  = "" + sp.msg;
            tr.Text += Environment.NewLine;

            if (isExternalError || sp.isError)
            {
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, colors.BrushError);
                tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            }
            else
            {
                if (sp.color == StoredPrint.ColorRed)
                {
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, colors.BrushRed);
                }

                if (sp.color == StoredPrint.ColorBlue)
                {
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, colors.BrushBlue);
                }
            }

            if (sp.linkTxt != null && sp.linkUri != null)
            {
                // see: https://stackoverflow.com/questions/762271/
                // clicking-hyperlinks-in-a-richtextbox-without-holding-down-ctrl-wpf
                // see: https://stackoverflow.com/questions/9279061/dynamically-adding-hyperlinks-to-a-richtextbox

                // try modify existing
                tr.Text = tr.Text.TrimEnd('\r', '\n') + " ";

                // make another append!
                var link = new Hyperlink(rtb.Document.ContentEnd, rtb.Document.ContentEnd);
                link.IsEnabled = true;

                // ReSharper disable EmptyGeneralCatchClause
                try
                {
                    link.Inlines.Add("" + sp.linkTxt + Environment.NewLine);
                    link.NavigateUri = new Uri(sp.linkUri);
                    if (linkClickHandler != null)
                    {
                        link.Click += linkClickHandler;
                    }
                }
                catch { }
                // ReSharper enable EmptyGeneralCatchClause
            }
        }
Exemple #2
0
        public static void StoredPrintToRichTextBox(
            RichTextBox rtb, StoredPrint sp, StoredPrintColors colors, bool isExternalError = false,
            RoutedEventHandler linkClickHandler = null)
        {
            // access
            if (rtb == null || sp == null)
            {
                return;
            }

            // append
            TextRange tr = new TextRange(rtb.Document.ContentEnd, rtb.Document.ContentEnd);

            tr.Text  = "" + sp.msg;
            tr.Text += Environment.NewLine;

            if (isExternalError || sp.isError)
            {
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, colors.BrushError);
                tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            }
            else
            {
                switch (sp.color)
                {
                default:
                    throw ExhaustiveMatch.Failed(sp.color);

                case StoredPrint.Color.Red:
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, colors.BrushRed);
                    break;

                case StoredPrint.Color.Blue:
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, colors.BrushBlue);
                    break;

                case StoredPrint.Color.Black:
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Black));
                    break;
                }
            }

            if (sp.linkTxt != null && sp.linkUri != null)
            {
                // see: https://stackoverflow.com/questions/762271/
                // clicking-hyperlinks-in-a-richtextbox-without-holding-down-ctrl-wpf
                // see: https://stackoverflow.com/questions/9279061/dynamically-adding-hyperlinks-to-a-richtextbox

                // try modify existing
                tr.Text = tr.Text.TrimEnd('\r', '\n') + " ";

                // make another append!
                var link = new Hyperlink(rtb.Document.ContentEnd, rtb.Document.ContentEnd);
                link.IsEnabled = true;

                try
                {
                    link.Inlines.Add("" + sp.linkTxt + Environment.NewLine);
                    link.NavigateUri = new Uri(sp.linkUri);
                    if (linkClickHandler != null)
                    {
                        link.Click += linkClickHandler;
                    }
                }
                catch (Exception ex)
                {
                    AdminShellNS.LogInternally.That.SilentlyIgnoredError(ex);
                }
            }
        }