Home c# TextBox with the ability to embed references C # Forms

TextBox with the ability to embed references C # Forms

Author

Date

Category

Trying to make it possible to add to the text box to one line link to any part of the conductor at which you can go on click. At the same time, the second line must be without reference, i.e. Static text. I tried a lot of things, but it either puts all the text as a link, or does not work at all. Even found a custom boxing class. Here is what I want to implement:

d: \ folder \ folder2 // You can go on click on the link to the text box
FOLDER OF SOME GAME // Static text

That’s what I found:

Public Class CustomTextBox: RichTextBox
  {
    #REGION Interop-Defines
    [StructlaYout (layoutkind.sequential)]
    Private Struct Charformat2_STruct.
    {
      Public Uint32 CBSIZE;
      Public Uint32 DWMASK;
      Public Uint32 dweffects;
      Public int32 Yheight;
      Public int32 yoffset;
      Public int32 CRTEXTCOLOR;
      Public Byte BCharset;
      Public Byte BPITCHANDFAMILY;
      [Marshalas (UNMANAGEDTYPE.BYVALARRAY, SIZECONST = 32)]
      Public Char [] SzFaceName;
      Public Uint16 WWEIGHT;
      Public Uint16 SSPACING;
      Public int crabcolor; // Color.ToArgB () - & gt; int.
      Public int lcid;
      Public int dwreserved;
      Public int16 sstyle;
      Public int16 wkerning;
      Public Byte Bunderlintype;
      Public Byte Banimation;
      Public Byte Brevauthor;
      Public Byte Breserved1;
    }
    [DLLIMPORT ("User32.dll", charset = charset.auto)]
    Private Static Extern Intptr SendMessage (Intptr Hwnd, Int MSG, Intptr WPARAM, Intptr LParam);
    Private Const int wm_user = 0x0400;
    Private Const int em_getcharformat = wm_user + 58;
    Private Const int em_setcharformat = wm_user + 68;
    Private Const int SCF_SELECTION = 0x0001;
    Private Const int SCF_WORD = 0x0002;
    Private Const int SCF_ALL = 0x0004;
    #REGION CHARFORMAT2 Flags.
    Private Const Uint32 CFE_BOLD = 0x0001;
    Private Const Uint32 CFE_Italic = 0x0002;
    Private Const Uint32 CFE_UNDERLINE = 0x0004;
    Private Const Uint32 CFE_Strikeout = 0x0008;
    Private Const Uint32 CFE_Protected = 0x0010;
    Private Const Uint32 CFE_LINK = 0x0020;
    Private Const uint32 CFE_Autocolor = 0x40000000;
    Private Const Uint32 CFE_SUBScript = 0x00010000; / * SuperScript and Subscript Are * /
    Private Const Uint32 CFE_SUPERScript = 0x00020000; / * Mutually Exclusive * /
    Private Const int cfm_smallcaps = 0x0040; / * (*) * /
    Private Const int cfm_allcaps = 0x0080; / * Displayed by 3.0 * /
    Private Const int cfm_hidden = 0x0100; / * Hidden by 3.0 * /
    Private Const int CFM_OUTLINE = 0x0200; / * (*) * /
    Private Const int cfm_shadow = 0x0400; / * (*) * /
    Private Const int cfm_emboss = 0x0800; / * (*) * /
    Private Const int cfm_imprint = 0x1000; / * (*) * /
    Private Const int cfm_disabled = 0x2000;
    Private Const int cfm_revised = 0x4000;
    Private Const int cfm_backcolor = 0x04000000;
    Private Const int cfm_lcid = 0x02000000;
    Private Const int CFM_UNDERLINETYPE = 0x00800000; / * Many Displayed by 3.0 * /
    Private Const int cfm_weight = 0x00400000;
    Private Const int cfm_spacing = 0x00200000; / * Displayed by 3.0 * /
    Private Const int cfm_kerning = 0x00100000; / * (*) * /
    Private Const int cfm_style = 0x00080000; / * (*) * /
    Private Const int cfm_animation = 0x00040000; / * (*) * /
    Private Const int cfm_revauthor = 0x00008000;
    Private Const Uint32 CFM_BOLD = 0x00000001;
    Private Const uint32 cfm_italic = 0x00000002;
    Private Const Uint32 CFM_UNDERLINE = 0x00000004;
    Private Const uint32 cfm_strikeout = 0x00000008; 
Private Const Uint32 CFM_Protected = 0x00000010;
    Private Const Uint32 CFM_LINK = 0x00000020;
    Private Const Uint32 CFM_SIZE = 0x80000000;
    Private Const uint32 cfm_color = 0x40000000;
    Private Const Uint32 CFM_FACE = 0x20000000;
    Private Const Uint32 CFM_OFFSET = 0x10000000;
    Private Const uint32 cfm_charset = 0x08000000;
    Private Const Uint32 CFM_SUBSCRIPT = CFE_SUBScript | CFE_SUPERScript;
    Private Const Uint32 CFM_SUPERScript = CFM_SUBSCRIP;
    Private Const Byte CFU_UNDERLINENONE = 0x00000000;
    Private Const Byte CFU_UNDERLINE = 0x00000001;
    Private Const byte CFU_UNDERLINEWORD = 0x00000002; / * (*) Displayed AS Ordinary Underline * /
    Private Const Byte CFU_UNDERLinedOBLE = 0x00000003; / * (*) Displayed AS Ordinary Underline * /
    Private Const Byte CFU_UNDERLinedotted = 0x00000004;
    Private Const Byte CFU_UNDERLINEDASH = 0x00000005;
    Private Const Byte CFU_UNDERLINEDASHDOT = 0x00000006;
    Private Const Byte CFU_UNDERLINEDASHDOTDOT = 0x00000007;
    Private Const Byte CFU_UNDERLINEWAVE = 0x00000008;
    Private Const Byte CFU_UNDERLINETHICK = 0x00000009;
    Private Const Byte CFU_UNDERLINEHAIRLINE = 0x0000000A; / * (*) Displayed AS Ordinary Underline * /
    #Endregion.
    #Endregion.
    Public CustomTextBox ()
    {
      // Otherwise, Non-Standard Links Get Lost When User Starts Typing
      // Next To A Non-Standard Link
      this.detecturls = false;
    }
    [DefaultValue (False)]
    Public New Bool Detecturls
    {
      get {return base.detecturls; }
      set {base.detecturls = value; }
    }
    /// & LT; Summary & GT;
    /// INSERT A GIVEN TEXTEXTBOX AT THE CURRENT INSERT POSITION.
    /// & LT; / Summary & GT;
    /// & lt; param name = "text" & gt; text to be inserted & lt; / param & gt;
    Public Void InsertLink (String Text)
    {
      INSERTLINK (Text, This.SelectionStart);
    }
    /// & LT; Summary & GT;
    /// INSERT A Given Text At A Given Position As A Link.
    /// & LT; / Summary & GT;
    /// & lt; param name = "text" & gt; text to be inserted & lt; / param & gt;
    /// & LT; Param Name = "Position" & gt; Insert Position & LT; / Param & gt;
    Public Void InsertLink (String Text, Int Position)
    {
      If (Position & lt; 0 || Position & gt; this.text.length)
        Throw New ArgumentOutofrangeException ("Position");
      This.selectionStart = Position;
      this.selectedText = Text;
      This.Selecth (Position, Text.Length);
      this.setselectionLink (TRUE);
      This.selecth (Position + Text.Length, 0);
    }
    /// & LT; Summary & GT;
    /// INSERT A GIVEN TEXT AT AT THE CURRENT INPUT POSITION AS A LINK.
    /// The Link Text Is Followed by A Hash (#) And The Given Hyperlink Text, Both Of
    /// Them Invisible.
    /// WHEN CLICKED ON, THE WHOLE LINK TEXT AND HYPERLINK STRING ARE GIVEN IN THE
    /// linkclickedeEventArgs.
    /// & LT; / Summary & GT;
    /// & lt; param name = "text" & gt; text to be inserted & lt; / param & gt;
    /// & LT; Param Name = "Hyperlink" & gt; Invisible Hyperlink String to Be Inserted & Lt; / Param & GT;
    Public Void InsertLink (String Text, String Hyperlink)
    {
      INSERTLINK (Text, Hyperlink, This.SelectionStart);
    }
    /// & LT; Summary & GT;
    /// INSERT A Given Text At A Given Position As A Link. The Link Text Is Followed By
    /// A HASH (#) And The Given Hyperlink Text, Both of Them Invisible.
    /// WHEN CLICKED ON, THE WHOLE LINK TEXT AND HYPERLINK STRING ARE GIVEN IN THE
    /// linkclickedeEventArgs.
    /// & LT; / Summary & GT;
    /// & lt; param name = "text" & gt; text to be inserted & lt; / param & gt;
    /// & LT; Param Name = "Hyperlink" & gt; Invisible Hyperlink String to Be Inserted & Lt; / Param & GT;
    /// & LT; Param Name = "Position" & gt; Insert Position & LT; / Param & gt;
    Public Void InsertLink (String Text, String Hyperlink, Int Position)
    {
      If (Position & lt; 0 || Position & gt; this.text.length) 
Throw New ArgumentOutofrangeException ("Position");
      This.selectionStart = Position;
      this.SelectedRtf = @ "{\ rtf1 \ ansi" + text + @ "\ v #" + hyperlink + @ "\ v0}";
      this.Select (position, text.Length + hyperlink.Length + 1);
      this.setselectionLink (TRUE);
      this.Select (position + text.Length + hyperlink.Length + 1, 0);
    }
    /// & LT; Summary & GT;
    /// Set the current selection's link style
    /// & LT; / Summary & GT;
    /// & lt; param name = "link" & gt; true: set link style, false: clear link style & lt; / param & gt;
    public void SetSelectionLink (bool link)
    {
      SetSelectionStyle (CFM_LINK, link CFE_LINK: 0?);
    }
    /// & LT; Summary & GT;
    /// Get the link style for the current selection
    /// & LT; / Summary & GT;
    /// & lt; returns & gt; 0: link style not set, 1: link style set, -1: mixed & lt; / returns & gt;
    public int GetSelectionLink ()
    {
      return GetSelectionStyle (CFM_LINK, CFE_LINK);
    }
    private void SetSelectionStyle (UInt32 mask, UInt32 effect)
    {
      CHARFORMAT2_STRUCT cf = new CHARFORMAT2_STRUCT ();
      cf.cbSize = (UInt32) Marshal.SizeOf (cf);
      cf.dwMask = mask;
      cf.dwEffects = effect;
      IntPtr wpar = new IntPtr (SCF_SELECTION);
      IntPtr lpar = Marshal.AllocCoTaskMem (Marshal.SizeOf (cf));
      Marshal.StructureToPtr (cf, lpar, false);
      IntPtr res = SendMessage (Handle, EM_SETCHARFORMAT, wpar, lpar);
      Marshal.FreeCoTaskMem (lpar);
    }
    private int GetSelectionStyle (UInt32 mask, UInt32 effect)
    {
      CHARFORMAT2_STRUCT cf = new CHARFORMAT2_STRUCT ();
      cf.cbSize = (UInt32) Marshal.SizeOf (cf);
      cf.szFaceName = new char [32];
      IntPtr wpar = new IntPtr (SCF_SELECTION);
      IntPtr lpar = Marshal.AllocCoTaskMem (Marshal.SizeOf (cf));
      Marshal.StructureToPtr (cf, lpar, false);
      IntPtr res = SendMessage (Handle, EM_GETCHARFORMAT, wpar, lpar);
      cf = (CHARFORMAT2_STRUCT) Marshal.PtrToStructure (lpar, typeof (CHARFORMAT2_STRUCT));
      int state;
      // dwMask holds the information which properties are consistent throughout the selection:
      if ((cf.dwMask & amp; mask) == mask)
      {
        if ((cf.dwEffects & amp; effect) == effect)
          state = 1;
        ELSE.
          state = 0;
      }
      ELSE.
      {
        state = -1;
      }
      Marshal.FreeCoTaskMem (lpar);
      return state;
    }
  }

Answer 1, Authority 100%

Put on the form RichTextBox , and adjust its size as you need. It is possible to combine both links and text. And for the future, this item features all sorts of formatting text. Event richTextBox1_LinkClicked handles clicks on the links.

public Form1 ()
  {
    Initializecomponent ();
    richTextBox1.Text = "file: // D: / folder / folder2 \ n folder of some game";
  }
  private void richTextBox1_LinkClicked (object sender, LinkClickedEventArgs e)
  {
    Process.Start (new Uri (e.LinkText) .AbsolutePath);
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions