Discussion:
Delete text in a RichTextBox
(too old to reply)
Jeronimo Bertran
2005-05-27 00:48:30 UTC
Permalink
How can I delete some text in a RichTextBox (without cutting it into the
clipboard)?

I am trying to delete a certain number of characters from the top of the
control...

I tried the following:

SelectionStart = 0;
SelectionLength = offset;
SelectedText = "*";

And the selection is effectively replaced by a single *. However if I
change the las line to:

SelectedText = "";

The application simply beeps and does not remove the selection.

Thanks

Jeronimo Bertran
Jeffrey Tan[MSFT]
2005-05-27 02:10:23 UTC
Permalink
Hi Jeronimo,

Thanks for your post!!

For this issue, I have writen a little sample project, which used the code
snippet below for the deleting in Button1_Click event:

this.richTextBox1.SelectionStart=0;
this.richTextBox1.SelectionLength=4;
this.richTextBox1.SelectedText="";

However, it works well on my side, that is: I can successfully delete the
text in the richtextbox. So I suspect there maybe some special situation in
your project.

Can you create a sample reproduce application and attach it in the reply?
Then we can refer to the reproduce project and understand your problem much
better. Thanks
=====================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Jeronimo Bertran
2005-05-27 16:37:53 UTC
Permalink
Jeffrey,

I checked it further and the problem hast to do with the richtextbox
being set as ReadOnly. I am able to replace the selection with OTHER
text but not with the empty string??? here is the class I tested with
...

public class MyRichText : RichTextBox
{
private int maxLines = 10;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;


public MyRichText()
{
// This call is required by the Windows.Forms Form
Designer.
InitializeComponent();
//
// TODO: Add constructor logic here
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// MyRichText
//
this.ReadOnly = true;
this.WordWrap = false;

}
#endregion


protected void RemoveLines()
{
int linesToRemove = Lines.Length - maxLines;
if (linesToRemove <= 0)
return;

try
{
int offset = 0;
for (int line = 0; line < linesToRemove; line
++)
offset += Lines[line].Length + 1;

Select(0, offset);
SelectedText = "";
}
catch (Exception e)
{
Debug.WriteLine(e.Message, "RemoveLines");
}
}

public void AddLine(string text, int indent, Color
textColor, bool stamped)
{
if (Lines.Length >= maxLines)
RemoveLines();

// Clear selection and send caret to the end
Select(TextLength, 0);
SelectionIndent = indent;

SelectionColor = textColor;
String prefix = "";
if (stamped)
prefix = DateTime.Now.ToString() + " : ";

SelectedText = prefix + text + "\n";
}
}
Jeffrey Tan[MSFT]
2005-05-28 06:22:06 UTC
Permalink
Hi Jeronimo,

Thanks for your feedback.

After setting RichTextBox's ReadOnly property to true, I can reproduce out
your problem with the code snippet below:
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength =4;
this.richTextBox1.SelectedText ="";

Then I used Reflector to view the source code of RichTextBox.SelectedText
property's set accessor, which is listed below:
public virtual void set_SelectedText(string value)
{
if (base.IsHandleCreated)
{
base.SendMessage(0xc5, 0x7fff, 0);
base.SendMessage(0xc2, -1, (value == null) ? "" : value);
base.SendMessage(0xb9, 0, 0);
base.SendMessage(0xc5, this.maxLength, 0);
}
else
{
string text1 = this.Text.Substring(0, this.SelectionStart);
string text2 = "";
if ((this.SelectionStart + this.SelectionLength) <
this.Text.Length)
{
this.Text.Substring(this.SelectionStart +
this.SelectionLength);
}
base.Text = text1 + value + text2;
}
this.ClearUndo();
}
In the code, 0xc5 is EM_LIMITTEXT, 0xc2 is EM_REPLACESEL, and 0xb9 is
EM_SETMODIFY.(You can search these consts messages in WinUser.h header file)

However, I still did not see any problem with this code snippet. What is
even strange is that I rewrite this function with customized P/invoke like
this:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
string lParam);

private int EM_REPLACESEL=0xc2;
private void button1_Click(object sender, System.EventArgs e)
{
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength =4;
string test=string.Empty;

MessageBox.Show(this.richTextBox1.IsHandleCreated.ToString());
SendMessage(this.richTextBox1.Handle, 0xc5, 0x7fff, null);
SendMessage(this.richTextBox1.Handle, EM_REPLACESEL, -1, (test == null) ?
"" : test);
SendMessage(this.richTextBox1.Handle,0xb9, 0, null);
SendMessage(this.richTextBox1.Handle,0xc5, this.richTextBox1.MaxLength,
null);
this.richTextBox1.ClearUndo();
}
All works well.

I am not sure the cause of this issue. However, currently, I think we can
workaround this issue with my code snippet above. Hope this helps.
===============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Jeronimo Bertran
2005-05-28 20:16:48 UTC
Permalink
Thanks Jeffrey,

That solved the problem... I also tried Patrick's solution and it also
works.

Now that you mention the Reflector... how do you protect classes from being
disassembled??


Jeronimo
Jeffrey Tan[MSFT]
2005-05-31 01:46:22 UTC
Permalink
Hi Jeronimo,

I am glad our replies make sense to you.

Yes, because of the metadata of .Net assembly, it is really easy to do
reverse engineering on .Net assembly. Currently, we usually used the tool
"dotfuscator" to protect the .Net assembly from being disassembled, this
tool is included in .Net VS.net, you can refer it in the start menu in
VS.net 2003.(Note: it is a third-party tool, which is not supported by
microsoft)

For more information of using dotfuscator, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dotfuscator
/dotf3e5x.asp

Additionally, the battle between code protection and reverse engineering is
going on for a long time, there is no perfect way to completely protect our
software unless preventing the people from using it. So dotfuscator only
gives our .Net assembly a certain level protection, but can not prevent the
"real" hack.

Anyway, hope my reply can help you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
C# Learner
2005-05-31 03:05:06 UTC
Permalink
Post by Jeronimo Bertran
Now that you mention the Reflector... how do you protect classes from being
disassembled??
For more information on that topic, check out Jon Skeet's article on it:
<http://www.yoda.arachsys.com/csharp/obfuscation.html>.
Jeffrey Tan[MSFT]
2005-06-03 07:53:43 UTC
Permalink
Hi Jeronimo,

Does my reply make sense to you? If you still have any concern, please feel
free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Patrick Schlangen
2005-05-28 07:46:04 UTC
Permalink
Hi,

perhaps a quite dirty solution, but perhaps you can set the ReadOnly
property to false, delete the code and set it back to true.
Post by Jeronimo Bertran
Jeffrey,
I checked it further and the problem hast to do with the richtextbox
being set as ReadOnly. I am able to replace the selection with OTHER
text but not with the empty string??? here is the class I tested with
...
public class MyRichText : RichTextBox
{
private int maxLines = 10;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MyRichText()
{
// This call is required by the Windows.Forms Form
Designer.
InitializeComponent();
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// MyRichText
//
this.ReadOnly = true;
this.WordWrap = false;
}
#endregion
protected void RemoveLines()
{
int linesToRemove = Lines.Length - maxLines;
if (linesToRemove <= 0)
return;
try
{
int offset = 0;
for (int line = 0; line < linesToRemove; line
++)
offset += Lines[line].Length + 1;
Select(0, offset);
SelectedText = "";
}
catch (Exception e)
{
Debug.WriteLine(e.Message, "RemoveLines");
}
}
public void AddLine(string text, int indent, Color
textColor, bool stamped)
{
if (Lines.Length >= maxLines)
RemoveLines();
// Clear selection and send caret to the end
Select(TextLength, 0);
SelectionIndent = indent;
SelectionColor = textColor;
String prefix = "";
if (stamped)
prefix = DateTime.Now.ToString() + " : ";
SelectedText = prefix + text + "\n";
}
}
Herfried K. Wagner [MVP]
2005-05-27 08:32:28 UTC
Permalink
Post by Jeronimo Bertran
How can I delete some text in a RichTextBox (without cutting it into the
clipboard)?
I am trying to delete a certain number of characters from the top of the
control...
SelectionStart = 0;
SelectionLength = offset;
SelectedText = "*";
And the selection is effectively replaced by a single *. However if I
SelectedText = "";
The application simply beeps and does not remove the selection.
Mhm... The code works fine for me. How are you filling the richtextbox
control?
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
s_mat
2007-07-27 22:02:09 UTC
Permalink
thanks a lot for the workaround for setting selectedtext to "

From http://www.developmentnow.com/g/30_2005_5_0_0_528622/Delete-text-in-a-RichTextBox.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com

Loading...