RichTextControl looses formatting

  • Thread starter Thread starter QSIDeveloper
  • Start date Start date
Q

QSIDeveloper

I am using a RichTextControl (C# VS2005 .NET 2.0) and allowing users to
change the font and color of what ever they select. The problem I am having
is that when I select a second set of character and change the font and or
color the Font and color from the previous change is lost.

This is the code that handles the change
private void ChangeFont()
{
if (InvokeRequired)
{
Invoke(m_ChangeFontD, null);
return;
}

bool wasProtected = false;

string s = richTextBox1.SelectedText;
int selectionStart = richTextBox1.SelectionStart;
int selectionLength = richTextBox1.SelectionLength;

FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.SelectionColor;
Font font = richTextBox1.SelectionFont;
fd.Font = font;

if (fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Focus();
//richTextBox1.SelectionStart = selectionStart;
//richTextBox1.SelectionLength = selectionLength;
richTextBox1.Select(selectionStart, selectionLength); //
regain selection
s = richTextBox1.SelectedText;

if (richTextBox1.SelectionProtected)
{
wasProtected = true;
richTextBox1.SelectionProtected = false; // so the font
canchange
}


richTextBox1.SelectionFont = fd.Font;
richTextBox1.SelectionColor = fd.Color;
richTextBox1.SelectionProtected = wasProtected;

s = richTextBox1.SelectedText;
System.Diagnostics.Debug.Print("SelectedText after font '" +
s + "'");
}

richTextBox1.Select(0,0);
m_TextToInsert = "";
}
 
Hello John,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

Based on my understanding, the issue we are now facing is, when we set the
second set of characters' font and color in the RichTextControl, the
previous set font and color are lost, right? Please feel free to correct me
if I have misunderstood your issue.

Your codes look very well, and I have also tested them on my side, but the
result is same as breitak's. I cannot reproduce the issue. No matter how
many times I set the selected text's font and color, the previous set font
and color remains ok. I have uploaded my project in the following link, and
would you like to download it and test it will work on your side?

http://cid-c2e0d62e8a095a30.skydrive.live.com/self.aspx/Public/RichTextContr
olFontIssue.zip

For your convenience, I also post my core codes here,

private String m_TextToInsert = "Default";
private delegate void DelegateChangeFont();
private DelegateChangeFont m_ChangeFontD;

private void Form1_Load(object sender, EventArgs e)
{
this.m_ChangeFontD = new DelegateChangeFont(ChangeFont);
}

private void button1_Click(object sender, EventArgs e)
{
m_ChangeFontD();
}

private void button2_Click(object sender, EventArgs e)
{
Thread t = new Thread(ChangeFont);
t.Start();
}

private void ChangeFont()
{
if (InvokeRequired)
{
Invoke(m_ChangeFontD, null);
return;
}

bool wasProtected = false;

string s = richTextBox1.SelectedText;
int selectionStart = richTextBox1.SelectionStart;
int selectionLength = richTextBox1.SelectionLength;

FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.SelectionColor;
Font font = richTextBox1.SelectionFont;
fd.Font = font;

if (fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Focus();
//richTextBox1.SelectionStart = selectionStart;
//richTextBox1.SelectionLength = selectionLength;
richTextBox1.Select(selectionStart, selectionLength);
//regain selection
s = richTextBox1.SelectedText;

if (richTextBox1.SelectionProtected)
{
wasProtected = true;
richTextBox1.SelectionProtected = false; // so the font can change
}


richTextBox1.SelectionFont = fd.Font;
richTextBox1.SelectionColor = fd.Color;
richTextBox1.SelectionProtected = wasProtected;

s = richTextBox1.SelectedText;
System.Diagnostics.Debug.Print("SelectedText after font '" + s + "'");
}

richTextBox1.Select(0, 0);
m_TextToInsert = "";
}

It works for both scenarios that I click the button1 and the button2.
Button1 click event handle will not into the Invoke method, while button2
click event handle does because it raise a new thread. Please let me know
if there is any difference from your side. Then, I can try my best to do
future research.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello

Thank you (both) for your reply

The problem appears to be related to saving and reloading the contents of
the RichTextBox. If I save the contents of the rich text box using .SaveFile
after changing colors of some of the words. Then I reload the text with
..LoadFile. Next insert some text in the middle of the loaded text by typing
somthing. Now change the color of say, the first word, you lose the inserted
text.

How can I send you the modified example?


""Ji Zhou [MSFT]"" said:
Hello John,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

Based on my understanding, the issue we are now facing is, when we set the
second set of characters' font and color in the RichTextControl, the
previous set font and color are lost, right? Please feel free to correct me
if I have misunderstood your issue.

Your codes look very well, and I have also tested them on my side, but the
result is same as breitak's. I cannot reproduce the issue. No matter how
many times I set the selected text's font and color, the previous set font
and color remains ok. I have uploaded my project in the following link, and
would you like to download it and test it will work on your side?

http://cid-c2e0d62e8a095a30.skydrive.live.com/self.aspx/Public/RichTextContr
olFontIssue.zip

For your convenience, I also post my core codes here,

private String m_TextToInsert = "Default";
private delegate void DelegateChangeFont();
private DelegateChangeFont m_ChangeFontD;

private void Form1_Load(object sender, EventArgs e)
{
this.m_ChangeFontD = new DelegateChangeFont(ChangeFont);
}

private void button1_Click(object sender, EventArgs e)
{
m_ChangeFontD();
}

private void button2_Click(object sender, EventArgs e)
{
Thread t = new Thread(ChangeFont);
t.Start();
}

private void ChangeFont()
{
if (InvokeRequired)
{
Invoke(m_ChangeFontD, null);
return;
}

bool wasProtected = false;

string s = richTextBox1.SelectedText;
int selectionStart = richTextBox1.SelectionStart;
int selectionLength = richTextBox1.SelectionLength;

FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.SelectionColor;
Font font = richTextBox1.SelectionFont;
fd.Font = font;

if (fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Focus();
//richTextBox1.SelectionStart = selectionStart;
//richTextBox1.SelectionLength = selectionLength;
richTextBox1.Select(selectionStart, selectionLength);
//regain selection
s = richTextBox1.SelectedText;

if (richTextBox1.SelectionProtected)
{
wasProtected = true;
richTextBox1.SelectionProtected = false; // so the font can change
}


richTextBox1.SelectionFont = fd.Font;
richTextBox1.SelectionColor = fd.Color;
richTextBox1.SelectionProtected = wasProtected;

s = richTextBox1.SelectedText;
System.Diagnostics.Debug.Print("SelectedText after font '" + s + "'");
}

richTextBox1.Select(0, 0);
m_TextToInsert = "";
}

It works for both scenarios that I click the button1 and the button2.
Button1 click event handle will not into the Invoke method, while button2
click event handle does because it raise a new thread. Please let me know
if there is any difference from your side. Then, I can try my best to do
future research.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
More information.
If you load the RTF directly with teh text below. Then try to make changes.
When you make a secong change the first is lost.


richTextBox1.Rtf =
@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
Microsoft Sans Serif;}}
{\colortbl
;\red128\green0\blue128;\red255\green0\blue0;\red0\green128\blue128;\red0\green0\blue128;}
\viewkind4\uc1\pard\cf1\f0\fs17 Test \cf0 text %%IndexField1%% set a \cf2
form \cf3\b\i\protect\fs18 %%str_OperatorName%%\cf0\b0\i0\protect0\fs17 \cf4
activated\cf0\par
}";



""Ji Zhou [MSFT]"" said:
Hello John,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

Based on my understanding, the issue we are now facing is, when we set the
second set of characters' font and color in the RichTextControl, the
previous set font and color are lost, right? Please feel free to correct me
if I have misunderstood your issue.

Your codes look very well, and I have also tested them on my side, but the
result is same as breitak's. I cannot reproduce the issue. No matter how
many times I set the selected text's font and color, the previous set font
and color remains ok. I have uploaded my project in the following link, and
would you like to download it and test it will work on your side?

http://cid-c2e0d62e8a095a30.skydrive.live.com/self.aspx/Public/RichTextContr
olFontIssue.zip

For your convenience, I also post my core codes here,

private String m_TextToInsert = "Default";
private delegate void DelegateChangeFont();
private DelegateChangeFont m_ChangeFontD;

private void Form1_Load(object sender, EventArgs e)
{
this.m_ChangeFontD = new DelegateChangeFont(ChangeFont);
}

private void button1_Click(object sender, EventArgs e)
{
m_ChangeFontD();
}

private void button2_Click(object sender, EventArgs e)
{
Thread t = new Thread(ChangeFont);
t.Start();
}

private void ChangeFont()
{
if (InvokeRequired)
{
Invoke(m_ChangeFontD, null);
return;
}

bool wasProtected = false;

string s = richTextBox1.SelectedText;
int selectionStart = richTextBox1.SelectionStart;
int selectionLength = richTextBox1.SelectionLength;

FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.SelectionColor;
Font font = richTextBox1.SelectionFont;
fd.Font = font;

if (fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Focus();
//richTextBox1.SelectionStart = selectionStart;
//richTextBox1.SelectionLength = selectionLength;
richTextBox1.Select(selectionStart, selectionLength);
//regain selection
s = richTextBox1.SelectedText;

if (richTextBox1.SelectionProtected)
{
wasProtected = true;
richTextBox1.SelectionProtected = false; // so the font can change
}


richTextBox1.SelectionFont = fd.Font;
richTextBox1.SelectionColor = fd.Color;
richTextBox1.SelectionProtected = wasProtected;

s = richTextBox1.SelectedText;
System.Diagnostics.Debug.Print("SelectedText after font '" + s + "'");
}

richTextBox1.Select(0, 0);
m_TextToInsert = "";
}

It works for both scenarios that I click the button1 and the button2.
Button1 click event handle will not into the Invoke method, while button2
click event handle does because it raise a new thread. Please let me know
if there is any difference from your side. Then, I can try my best to do
future research.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I tried to load your RTF string into the richTextControl in the Form
loading process, but still cannot reproduce the issue. You can send me the
modified project to me at (e-mail address removed). So, I will try to find what
the problem is there.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top