public FormatRichText()
        {
            Title = "Format Rich Text";

            // Create DockPanel as content of window.
            DockPanel dock = new DockPanel();
            Content = dock;

            // Create ToolBarTray docked at top of client area.
            ToolBarTray tray = new ToolBarTray();
            dock.Children.Add(tray);
            DockPanel.SetDock(tray, Dock.Top);

            // Create RichTextBox.
            txtbox = new RichTextBox();
            txtbox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

            // Call methods in other files.
            AddFileToolBar(tray, 0, 0);
            AddEditToolBar(tray, 1, 0);
            AddCharToolBar(tray, 2, 0);
            AddParaToolBar(tray, 2, 1);
            AddStatusBar(dock);

            // Fill rest of client area with RichTextBox and give it focus.
            dock.Children.Add(txtbox);
            txtbox.Focus();
        }
 public EditSomeRichTest()
 {
     Title = "Edit Some Rich Text";
     txtBox = new RichTextBox();
     txtBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
     Content = txtBox;
     txtBox.Focus();
 }
Example #3
0
        /// <summary>
        /// Selects the highlight as the current highlight when iterating through hits, bringing the control to focus.
        /// </summary>
        public override void Select()
        {
            System.Windows.Controls.RichTextBox rtb = (BodyAdorner.AdornedElement as System.Windows.Controls.RichTextBox);
            rtb.Focus();//focusing is very important because not only is it correct user behaviour but it also allows us to track the user moving focus to other controls better
            rtb.Selection.Select(run.ContentStart.GetPositionAtOffset(Start), run.ContentStart.GetPositionAtOffset(End));

            base.Select();
        }
Example #4
0
 protected void InsertPhrase(RichTextBox rtb, string phraseCategory)
 {
     string phrase = PickListControl.ShowPickList(User, PickListType.Phrase, phraseCategory, TraitCategoryType.Taxon);
     if (phrase != null) {
         var tr = new TextRange(rtb.Selection.Start, rtb.Selection.End);
         tr.Text = phrase;
         rtb.Focus();
     }
 }
Example #5
0
 private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
 {
     //This fixes the bug when applying text transformations the text would lose it's highlight. That was because the RichTextBox was losing focus,
     //so we just give it focus again and it seems to do the trick of re-highlighting it.
     if (!_richTextBox.IsFocused && !_richTextBox.Selection.IsEmpty)
     {
         _richTextBox.Focus();
     }
 }
Example #6
0
        public FormatRichText()
        {
            Title = "Format Rich Text";

            DockPanel dock = new DockPanel();
            Content = dock;

            ToolBarTray tray = new ToolBarTray();
            dock.Children.Add(tray);
            DockPanel.SetDock(tray, Dock.Top);

            txtbox = new RichTextBox();
            txtbox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

            AddFileToolBar(tray, 0, 0);
            AddEditToolBar(tray, 1, 0);
            AddCharToolBar(tray, 2, 0);
            AddParaToolBar(tray, 2, 1);
            AddStatusBar(dock);

            dock.Children.Add(txtbox);
            txtbox.Focus();
        }
Example #7
0
 public static void SelectText(RichTextBox myRichTextBox, string input)
 {
     TextPointer textPointer = (TextPointer) null;
       string str = input;
       if (string.IsNullOrEmpty(str))
     return;
       for (TextPointer position1 = myRichTextBox.Document.ContentStart; position1 != null && position1.CompareTo(myRichTextBox.Document.ContentEnd) < 0; position1 = position1.GetNextInsertionPosition(LogicalDirection.Forward) ?? myRichTextBox.Document.ContentStart)
       {
     TextPointer position2 = position1;
     for (int index = 0; position2 != null && index < str.Length; ++index)
       position2 = position2.GetNextInsertionPosition(LogicalDirection.Forward);
     if (position2 != null)
     {
       TextRange textRange = new TextRange(position1, position2);
       if (textRange.Text == str)
       {
     myRichTextBox.Focus();
     myRichTextBox.Selection.Select(textRange.Start, textRange.End);
     textPointer = position2.GetNextInsertionPosition(LogicalDirection.Forward);
     break;
       }
     }
       }
 }
        public CraftTheToolbar()
        {
            Title = "Craft the Toolbar";

            RoutedUICommand[] comm =
                {
                    ApplicationCommands.New, ApplicationCommands.Open,
                    ApplicationCommands.Save, ApplicationCommands.Print,
                    ApplicationCommands.Cut, ApplicationCommands.Copy,
                    ApplicationCommands.Paste, ApplicationCommands.Delete
                };

            string[] strImages =
                {
                    "NewDocumentHS.png", "openHS.png", "saveHS.png",
                    "PrintHS.png", "CutHS.png", "CopyHS.png",
                    "PasteHS.png", "DeleteHS.png"
                };

            // Create DockPanel as content of window.
            DockPanel dock = new DockPanel();
            dock.LastChildFill = false;
            Content = dock;

            // Create Toolbar docked at top of window.
            ToolBar toolbar = new ToolBar();
            dock.Children.Add(toolbar);
            DockPanel.SetDock(toolbar, Dock.Top);

            RichTextBox txtbox = new RichTextBox();
            dock.Children.Add(txtbox);

            txtbox.Focus();

            // Create the Toolbar buttons.
            for (int i = 0; i < 8; i++)
            {
                if (i == 4)
                    toolbar.Items.Add(new Separator());

                // Create the Button.
                Button btn = new Button();
                btn.Command = comm[i];
                toolbar.Items.Add(btn);

                // Create an Image as content of the Button.
                Image img = new Image();
                img.Source = new BitmapImage(
                    new Uri("pack://application:,,/Images/" + strImages[i]));
                img.Stretch = Stretch.None;
                btn.Content = img;

                //StackPanel stack = new StackPanel();
                //stack.Orientation = Orientation.Horizontal;
                //btn.Content = stack;

                //TextBlock txtblk = new TextBlock();
                //txtblk.Text = comm[i].Text;

                //stack.Children.Add(img);
                //stack.Children.Add(txtblk);

                // Create a ToolTip based on the UICommand text.
                ToolTip tip = new ToolTip();
                tip.Content = comm[i].Text;
                btn.ToolTip = tip;

                // �̺�Ʈ �ڵ鷯�� RoutedUICommand ��ü�� �����ϱ� ���� Ŀ�ǵ� ���ε� �÷��ǿ� Ŀ��Ʈ ��ü�� �߰����ش�.
                CommandBindings.Add(
                    new CommandBinding(comm[i], ToolBarButtonOnClick));
            }
        }
Example #9
0
File: Utility.cs Project: JuRogn/OA
        /// <summary>
        /// 富文本框赋值-向寒咏
        /// </summary>
        /// <param name="box">富文本框</param>
        /// <param name="RichBoxData">保存的值-Byte[]类型</param>
        public static void SetRichTextBoxData(RichTextBox box, byte[] RichBoxData)
        {
            box.Blocks.Clear();
            if (RichBoxData == null || RichBoxData.Length == 0)
            {
                return;
            }
            byte[] start = new byte[1024];   //图片定位数组
            if (RichBoxData.Length > 1024)
                Array.Copy(RichBoxData, 0, start, 0, 1024);

            long starleng = 0;     //开始截取的位置
            long endleng = 0;     //结束截取的位置
            long endy = 0;         //获取上次结束的位置
            bool isendtext = false;  //不存在图片
            int[] lengst = Byte64toLong(start);  //转化定位数组
            if (RichBoxData.Length > 1024)
            {
                for (int x = 1024; x < RichBoxData.Length; x++)   //内容进行循环   位置从开始匹配
                {
                    if (lengst != null)
                    {
                        endy = endleng;
                        for (int a = 0; a < lengst.Length; a++)   //定位数组进行循环
                        {
                            if (a % 2 == 0)
                            {
                                starleng = long.Parse(lengst[a].ToString());  //获取开始出现图片位置
                                continue;
                            }
                            else
                            {
                                endleng = long.Parse(lengst[a].ToString());   //获取结束图片位置

                                if (endleng == 0)
                                {
                                    isendtext = true;
                                }
                            }
                            if (starleng == x - 1024 && isendtext == false)                      //当前位置是图片位置 进行处理
                            {
                                byte[] imageByte = new byte[endleng - starleng];

                                Array.Copy(RichBoxData, (int)starleng + 1024, imageByte, 0, (int)(endleng - starleng));
                                SetImageBindBox(box, imageByte);  //控件绑定
                                x = (int)endleng + 1024;
                                if (x == RichBoxData.Length - 2)
                                {
                                    x++;
                                }

                            }  // 处理文字信息
                            else
                            {
                                byte[] textByte = null;
                                if (starleng == 0 && isendtext == true)          //所有都是文字处理方式
                                {
                                    textByte = new byte[RichBoxData.Length - x];
                                    if (RichBoxData.Length == x)
                                    {
                                        break;
                                    }
                                    Array.Copy(RichBoxData, x, textByte, 0, (int)(RichBoxData.Length - x));
                                }

                                if (starleng > x - 1024)    // 当前位置小于开始图片位置
                                {
                                    textByte = new byte[starleng - x + 1024];
                                    Array.Copy(RichBoxData, x, textByte, 0, (int)(starleng - x + 1024));
                                }

                                SetTextBindBox(box, textByte, 1);
                                //using (MemoryStream stream = new MemoryStream(textByte))
                                //{
                                //    using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
                                //    {
                                //        string Xaml = reader.ReadToEnd();
                                //        Run myRun = new Run();
                                //        myRun.Text = Xaml;
                                //        box.Selection.Insert(myRun);

                                //    }

                                //}
                                if (starleng == 0)          //所有都是文字处理方式  没有图片时跳出循环
                                {
                                    x = RichBoxData.Length - 1;
                                    break;
                                }
                                if (starleng > x - 1024)    //当当前位置
                                {
                                    x = 1024 + (int)starleng;
                                    byte[] imageByte = new byte[endleng - starleng];
                                    Array.Copy(RichBoxData, x, imageByte, 0, (int)(endleng - starleng));
                                    SetImageBindBox(box, imageByte);  //控件绑定
                                    x = (int)endleng + 1024;
                                    if (x == RichBoxData.Length - 2)
                                    {
                                        x++;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        #region 判断没有图片的时候
                        if (starleng == 0 && endleng == 0)
                        {
                            byte[] strByte = new byte[RichBoxData.Length - 1024];
                            Array.Copy(RichBoxData, 1024, strByte, 0, RichBoxData.Length - 1024);
                            SetTextBindBox(box, strByte, 1);

                            break;
                        }
                        #endregion
                    }

                }
            }
            else
            {
                //SetTextBindBox(box, RichBoxData,1);
                using (MemoryStream stream = new MemoryStream(RichBoxData))
                {
                    using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
                    {
                        string Xaml = reader.ReadToEnd();
                        Run myRun = new Run();
                        myRun.Text = Xaml;
                        box.Selection.Insert(myRun);

                    }

                }
            }
            //box.SelectAll();
            //box.Selection.ApplyPropertyValue(Run.FontSizeProperty, "12");
            TextPointer startPointer = box.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);
            TextPointer MyTP1 = startPointer.GetPositionAtOffset(0, LogicalDirection.Forward);
            box.Selection.Select(startPointer, MyTP1);
            box.Focus();

        }
Example #10
0
File: Utility.cs Project: JuRogn/OA
 private static void ReturnFocus(RichTextBox box)
 {
     if (box != null)
         box.Focus();
 }
Example #11
0
        private void EditRecord_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = e.Source as MenuItem;
            if (menuItem == null)
                return;

            ContextMenu contextMenu = menuItem.Parent as ContextMenu;
            if (contextMenu == null)
                return;

            DockPanel activePanel = contextMenu.PlacementTarget as DockPanel;
            if (activePanel == null)
                return;

            int id = 0;
            if (int.TryParse(activePanel.Tag.ToString(), out id))
            {
                FlowDocumentScrollViewer textViewer = activePanel.Children.OfType<FlowDocumentScrollViewer>().FirstOrDefault();
                if (textViewer == null)
                    return;

                //copy document from selected record to newPost RichTextBox
                RichTextBox updatedPost = new RichTextBox();
                updatedPost.Tag = id.ToString();
                updatedPost.LostFocus += UpdatedPost_LostFocus;

                TextRange tr = new TextRange(textViewer.Document.ContentStart, textViewer.Document.ContentEnd);
                MemoryStream ms = new MemoryStream();
                tr.Save(ms, DataFormats.Rtf);

                TextRange range2 = new TextRange(updatedPost.Document.ContentEnd, updatedPost.Document.ContentEnd);
                range2.Load(ms, DataFormats.Rtf);

                activePanel.Children.Remove(textViewer);
                activePanel.Children.Add(updatedPost);
                updatedPost.Focus();
            }
        }
 private void ReturnFocus(RichTextBox textBox)
 {
     if (textBox != null)
         textBox.Focus();
 }
 private void SetRichTextBoxValue(RichTextBox varControl, string text)
 {
     if (varControl.Dispatcher.CheckAccess())
     {
         varControl.Document = new FlowDocument(new Paragraph(new Run(text)));
         varControl.Focus();
     }
     else
     {
         varControl.Dispatcher.Invoke(new Action<RichTextBox, string>((c, t) => SetRichTextBoxValue(varControl, text)), new object[] { varControl, text });
     }
 }