CodeRunner
CodeRunner

Reputation: 391

Make links readonly in richtextbox c#

I have a richtextbox in c# and I want to make the links that appear as readonly. Right now I can move my cursor into it and edit it. Is there any way to make it readonly?

Upvotes: 0

Views: 1542

Answers (4)

mutanic
mutanic

Reputation: 2488

I would like to share my solution...I did try everything I found on the internet but seems I can't get exactly 100% like I want(to make a richtexbox as readonly). Then I start looking for an alternative which finally I get one to do exactly like I want.

Sometime we need to show the value with a style on it, thats why we choose richtextbox at the 1st time, then it become an issue when we unable to make it as ReadOnly. The different is I am not using the richtextbox anymore but I change it to label. Depending on how your program work, you might need to have 2 control (richtextbox & label) to hold the same value which will be switched(visible true/false) base on your requirement.

See my example here to get a ReadOnly richtextbox look alike control :

<div id="History">
    <asp:Label ID="lblLACA27" runat="server" CssClass="ctlLabel"></asp:Label>
</div>

And a piece of CSS code :

    #History
    {
        height: 100px;
        float: left;
        overflow: auto;
        overflow-x: hidden;
    }

The DIV tag which hold the LABEL will act like multiline textbox/richtextbox with scrollbar visible on it. Thats it & lets continue programming. Hope this will help someone later.

Upvotes: 0

Picrofo Software
Picrofo Software

Reputation: 5571

You can set this property of a RichTextBox to make the whole text read-only

ReadOnly = true

If you would like to protect the links only but leave other text editable, please try to insert the following whether under Form1_Load or under any method you may create

You'll need to add RichTextBox.Find(string str); from the object browser

    MatchCollection mc = Regex.Matches(richTextBox1.Text, @"(www[^ \s]+|http[^ \s]+)([\s]|$)", RegexOptions.IgnoreCase); // Create a new MatchCollection and match from richTextBox1.Text

    for (int collection = 0; collection < mc.Count; collection++) // increase collection for every string in mc
    {
        if (richTextBox1.Find(mc[collection].Value, RichTextBoxFinds.None) > -1) // Find the mc value
        {
            richTextBox1.SelectionProtected = true; // Protect the value
        }
    }

So the form would look like this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            MatchCollection mc = Regex.Matches(richTextBox1.Text, @"(www[^ \s]+|http[^ \s]+)([\s]|$)", RegexOptions.IgnoreCase);

            for (int collection = 0; collection < mc.Count; collection++)
            {
                if (richTextBox1.Find(mc[collection].Value, RichTextBoxFinds.None) > -1)
                {
                    richTextBox1.SelectionProtected = true;
                }
            }

        }
    }
}

Thanks,

Have a great day :)

Upvotes: 1

AntonR
AntonR

Reputation: 51

You should capture the change event, in such a way that you reset every change a user would like to make to the link and set it back to the original link. Save the positions of the links and update the positions if the user deletes or adds a character.

Upvotes: 0

Thousand
Thousand

Reputation: 6638

You can change it in your code like this:

richTextBox1.ReadOnly = true;

Or you could go to your design view, check the properties for your richtextbox and set the ReadOnly attribute to true.

Upvotes: 1

Related Questions