RichTextBox for UWP | ComponentOne
Tutorials / Creating an AppBar Application / Step 4 of 5: Adding Code for the BottomAppBar
In This Topic
    Step 4 of 5: Adding Code for the BottomAppBar
    In This Topic

    In this step, you'll add the code that handles the flyout and click events for the BottomAppBar items.

    1. The first section of code to add to your page contains five regions: Clipboard, Undo/Redo, Lists, clear formatting, and sub/superscript:
    C#
    Copy Code
    #region Clipboard
            private void btnCopy_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                rtb.ClipboardCopy();
            }
    
            private void btnCut_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                if (rtb.IsReadOnly)
                    rtb.ClipboardCopy();
                else
                    rtb.ClipboardCut();
            }
    
            private void btnPaste_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                if (!rtb.IsReadOnly)
                {
                    rtb.ClipboardPaste();
                }
            }
            #endregion
    
            #region Undo/Redo
            void btnRedo_Click(object sender, RoutedEventArgs e)
            {
                if (rtb.DocumentHistory.CanRedo)
                {
                    rtb.DocumentHistory.Redo();
                }
                morePopUp.Hide();
            }
    
            void btnUndo_Click(object sender, RoutedEventArgs e)
            {
                if (rtb.DocumentHistory.CanUndo)
                {
                    rtb.DocumentHistory.Undo();
                }
                morePopUp.Hide();
            }
            #endregion
    
            #region Lists
            void btnBulletedList_Click(object sender, RoutedEventArgs e)
            {
                // check if selection is already a list
                if (rtb.Selection.Lists.Count<C1.Xaml.RichTextBox.Documents.C1List>() > 0)
                {
                    // undo list
                    rtb.Selection.UndoList();
                }
                else
                {
                    // make bullet list
                    rtb.Selection.MakeList(C1.Xaml.RichTextBox.Documents.TextMarkerStyle.Disc);
                }
                morePopUp.Hide();
            }
    
            void btnNumberedList_Click(object sender, RoutedEventArgs e)
            {
                // check if selection is already a list
                if (rtb.Selection.Lists.Count<C1.Xaml.RichTextBox.Documents.C1List>() > 0)
                {
                    // undo list
                    rtb.Selection.UndoList();
                }
                else
                {
                    // make number list
                    rtb.Selection.MakeList(C1.Xaml.RichTextBox.Documents.TextMarkerStyle.Decimal);
                }
                morePopUp.Hide();
            }
            #endregion
    
            #region clear formatting
            void btnClear_Click(object sender, RoutedEventArgs e)
            {
                // clear foreground and background colors
                rtb.Selection.InlineBackground = null;
                rtb.Selection.Foreground = rtb.Foreground;
    
                // clear font
                rtb.Selection.FontWeight = FontWeights.Normal;
                rtb.Selection.FontStyle = FontStyle.Normal;
                rtb.Selection.TextDecorations = null;
            }
            #endregion
    
            #region sub/super script
            void btnSubscript_Click(object sender, RoutedEventArgs e)
            {
                // subscript
                if (rtb.Selection.InlineAlignment != C1VerticalAlignment.Sub && rtb.Selection.InlineAlignment != null)
                {
                    rtb.Selection.InlineAlignment = C1VerticalAlignment.Sub;
                    ShrinkFont(4);
                }
                else
                {
                    rtb.Selection.InlineAlignment = C1VerticalAlignment.Baseline;
                    GrowFont(4);
                }
                morePopUp.Hide();
            }
    
            void btnSuperscript_Click(object sender, RoutedEventArgs e)
            {
                // superscript
                if (rtb.Selection.InlineAlignment != C1VerticalAlignment.Super && rtb.Selection.InlineAlignment != null)
                {
                    rtb.Selection.InlineAlignment = C1VerticalAlignment.Super;
                    ShrinkFont(4);
                }
                else
                {
                    rtb.Selection.InlineAlignment = C1VerticalAlignment.Baseline;
                    GrowFont(4);
                }
                morePopUp.Hide();
            }
    
            private void GrowFont(int size)
            {
                // grow font
                rtb.Selection.TrimRuns();
                foreach (var run in rtb.Selection.Runs)
                {
                    run.FontSize += size;
                }
            }
    
            private void ShrinkFont(int size)
            {
                // shrink font
                rtb.Selection.TrimRuns();
                foreach (var run in rtb.Selection.Runs)
                {
                    run.FontSize -= size;
                }
            }
            #endregion
    
    1. The next region of code to be added contains the events for the More button and menu:
    C#
    Copy Code
    #region More
            Flyout morePopUp = new Flyout();
            private void btnMore_Click(object sender, RoutedEventArgs e)
            {
                morePopUp.Placement = FlyoutPlacementMode.Top;
                morePopUp.ShowAt(sender as FrameworkElement);
            }
    
            void InitMorePopup()
            {
                Border menuBorder = new Border();
                menuBorder.Height = 260;
                menuBorder.Width = 150;
                StackPanel panel = new StackPanel();
                panel.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
    
                // bulleted list
                Button btnBulletedList = new Button();
                btnBulletedList.Content = "Bulleted List";
                btnBulletedList.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnBulletedList.Margin = new Thickness(20, 5, 20, 5);
                btnBulletedList.Click += btnBulletedList_Click;
    
                // numbered list
                Button btnNumberedList = new Button();
                btnNumberedList.Content = "Numbered List";
                btnNumberedList.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnNumberedList.Margin = new Thickness(20, 5, 20, 5);
                btnNumberedList.Click += btnNumberedList_Click;
    
                // undo
                Button btnUndo = new Button();
                btnUndo.Content = "Undo";
                btnUndo.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnUndo.Margin = new Thickness(20, 5, 20, 5);
                btnUndo.Click += btnUndo_Click;
    
                // undo
                Button btnRedo = new Button();
                btnRedo.Content = "Redo";
                btnRedo.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnRedo.Margin = new Thickness(20, 5, 20, 5);
                btnRedo.Click += btnRedo_Click;
    
                // clear formatting
                Button btnClear = new Button();
                btnClear.Content = "Clear Formatting";
                btnClear.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnClear.Margin = new Thickness(20, 5, 20, 5);
                btnClear.Click += btnClear_Click;
    
                // superscript
                Button btnSuperscript = new Button();
                btnSuperscript.Content = "Superscript";
                btnSuperscript.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnSuperscript.Margin = new Thickness(20, 5, 20, 5);
                btnSuperscript.Click += btnSuperscript_Click;
    
                // subscript
                Button btnSubscript = new Button();
                btnSubscript.Content = "Subscript";
                btnSubscript.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnSubscript.Margin = new Thickness(20, 5, 20, 5);
                btnSubscript.Click += btnSubscript_Click;
    
                // strikethrough
                Button btnStrikethrough = new Button();
                btnStrikethrough.Content = "Strikethrough";
                btnStrikethrough.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnStrikethrough.Margin = new Thickness(20, 5, 20, 5);
                btnStrikethrough.Click += btnStrikethrough_Click;
    
                panel.Children.Add(btnBulletedList);
                panel.Children.Add(btnNumberedList);
                panel.Children.Add(btnSubscript);
                panel.Children.Add(btnSuperscript);
                panel.Children.Add(btnStrikethrough);
                panel.Children.Add(btnUndo);
                panel.Children.Add(btnRedo);
                panel.Children.Add(btnClear);
                menuBorder.Child = panel;
                morePopUp.Content = menuBorder;
    
            }
    
            void btnStrikethrough_Click(object sender, RoutedEventArgs e)
            {
                // strikethrough
                var range = rtb.Selection;
                var collection = new C1TextDecorationCollection();
                if (range.TextDecorations == null)
                {
                    collection.Add(C1TextDecorations.Strikethrough[0]);
                }
                else if (!range.TextDecorations.Contains(C1TextDecorations.Strikethrough[0]))
                {
                    foreach (var decoration in range.TextDecorations)
                        collection.Add(decoration);
    
                    collection.Add(C1TextDecorations.Strikethrough[0]);
                }
                else
                {
                    foreach (var decoration in range.TextDecorations)
                        collection.Add(decoration);
    
                    collection.Remove(C1TextDecorations.Strikethrough[0]);
                    if (collection.Count == 0)
                        collection = null;
                }
                range.TextDecorations = collection;
                morePopUp.Hide();
            }
            #endregion
    
    1. The last section of code handles the PointerPressed event:
    C#
    Copy Code
    private void rtb_PointerPressed(object sender, PointerRoutedEventArgs e)
            {
                bottomAppBar.IsOpen = true;
                topAppBar.IsOpen = true;
            }
        }
    }
    

    In this step, you added the code for the Bottom AppBar events. In the next step, you'll run your application.