Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / May 2008

Tip: Looking for answers? Try searching our database.

RTF content in contextmenustrip

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Aneesh Pulukkul [http://dotnet-revolutions.blogspot.com] - 05 May 2008 11:13 GMT
I have a requirement- need to display RTF content in context menu. I
am able to host a rich textbox in a contextmenu strip using
ToolStripControlHost class. But assigning some RTF content to the
containing rich textbox control does not display in actual RTF
format- it just dislpays as plain  text. Here's the code am using:

//Custom richtextbox to handle bg color in readonly mode
public class MyRichTextBox : RichTextBox
   {
       public MyRichTextBox()
       {
       }

       public new bool ReadOnly
       {
           get
           {
               return base.ReadOnly;
           }
           set
           {
               if (value == true)
               {
                   this.BackColor = Color.White;
               }

               base.ReadOnly = value;
           }
       }

   }

----------------------------------------------------------------
public Form1()
       {
           InitializeComponent();

           m_cmenu.Items.Add(new ToolStripMenuItem("Edit"));
           m_cmenu.Items.Add(new ToolStripSeparator());

           MyRichTextBox m_richTextBox = new MyRichTextBox();
           m_richTextBox.BorderStyle =
System.Windows.Forms.BorderStyle.None;
           m_richTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
           m_richTextBox.ReadOnly = true;
           m_richTextBox.ScrollBars =
System.Windows.Forms.RichTextBoxScrollBars.None;

           ToolHost textItem = new ToolHost(m_richTextBox);
           //Assign sample RTF content.
           m_richTextBox.Rtf = @"{\rtf1\fbidis\ansi
\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0
Arial;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\ltrpar\sa200\sl276\slmult1\f0\fs28 This is\fs22
\i some\i0  \ul Rich\ulnone  \b text\b0\par
\pard\ltrpar\f1\fs17\par }";

           m_cmenu.Items.Add(textItem);
           this.ContextMenuStrip = m_cmenu;//Assign contextmenu for
Form

       }
Morten Wennevik [C# MVP] - 06 May 2008 06:48 GMT
Hi,

There is definitely something going on with the ToolStripControlHost that is
not so logical, but I was able to draw colored rtf text using the following
method.

Set the text/rtf property before initializing the ToolHost object and pass
the rtf along with the constructor.  In an appropriate place inside the
ToolHost set the rtf again.  OnVisibleChanged appears to be working for this

           string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033
{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs17 This \cf1 is\cf0 some Rich
text\par
\pard\par}";

           ToolHost textItem = new ToolHost(m_richTextBox, rtf);

           m_richTextBox.Rtf = rtf;

...

   public class ToolHost : ToolStripControlHost
   {
       private MyRichTextBox mrtb;
       private string rtf;
       public ToolHost(MyRichTextBox c, string rtf)
           : base(c)
       {
           mrtb = c;
           this.rtf = rtf;
       }

       protected override void OnVisibleChanged(EventArgs e)
       {
           base.OnVisibleChanged(e);
           mrtb.Rtf = rtf;
       }
   }
Signature

Happy Coding!
Morten Wennevik [C# MVP]

"Aneesh Pulukkul [http://dotnet-revolutio" wrote:

> I have a requirement- need to display RTF content in context menu. I
> am able to host a rich textbox in a contextmenu strip using
[quoted text clipped - 58 lines]
>
>         }
Aneesh Pulukkul [http://dotnet-revolutions.blogspot.com] - 06 May 2008 10:45 GMT
On May 6, 10:48 am, Morten Wennevik [C# MVP]
<MortenWenne...@hotmail.com> wrote:
> Hi,
>
[quoted text clipped - 105 lines]
>
> - Show quoted text -

Thanks a lot Morten!!. That solved the issue with display.

Now am trying to fix the width - autosize for text box/toolstripitem
does not work. Also setting ReadOnly does not disable the cursor and
text selection. Would appreciate if you could share your thoughts on
this.
Aneesh Pulukkul [http://dotnet-revolutions.blogspot.com] - 06 May 2008 18:16 GMT
On May 6, 2:45 pm, "Aneesh Pulukkul [http://dotnet-
revolutions.blogspot.com]" <anees...@gmail.com> wrote:
> On May 6, 10:48 am, Morten Wennevik [C# MVP]
>
[quoted text clipped - 117 lines]
>
> - Show quoted text -

Am selecting the value of clicked item and closing the menu -  so no
need to disable cursor and text selection. But, the autosize issue is
still there. Tried to set the Wordwrap property to false, but it
casuses clipping of text. Don' know how to alter the width of the
richtextbox in this case. Assigning explicit width didn't solve the
problem.
Aneesh Pulukkul [http://dotnet-revolutions.blogspot.com] - 17 May 2008 21:19 GMT
On May 6, 10:16 pm, "Aneesh Pulukkul [http://dotnet-
revolutions.blogspot.com]" <anees...@gmail.com> wrote:
> On May 6, 2:45 pm, "Aneesh Pulukkul [http://dotnet-
>
[quoted text clipped - 129 lines]
>
> - Show quoted text -

Got a trick from my colleague John - disable AutoSize for
ToolStripControlHost and use calculated size to accomodate the RTF
content.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.