By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

RichTextControl looses formatting

P: n/a
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 = "";
}
Oct 17 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a

I could not see anything obviously wrong in the code you posted (though
you do not show the initialization of the delegate m_ChangeFont, but I
am assuming there is something like:

public delegate void DelgChangeFontD();
private DelgChangeFontD m_ChangeFont;
m_ChangeFont = new DelgChangeFontD(ChangeFont);

somewhere in your app).

I built a quick project and dumped your code in. It worked fine. I
was able to make multiple selections and change the font/size/color
without losing any previous changes.
--
breitak67
Oct 18 '08 #2

P: n/a
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.../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 (v-****@online.microsoft.com, 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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 20 '08 #3

P: n/a
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]""" wrote:
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.../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 (v-****@online.microsoft.com, 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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 20 '08 #4

P: n/a
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{\fontt bl{\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]""" wrote:
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.../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 (v-****@online.microsoft.com, 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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 20 '08 #5

P: n/a

I set the contents of the rich text box by placing the assignment
statement in the form_load event handler. I was able to make multiple
changes in font, color, and size without losing any previous changes.
--
breitak67
Oct 21 '08 #6

P: n/a
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 v-****@microsoft.com. So, I will try to find what
the problem is there.
Best regards,
Ji Zhou (v-****@online.microsoft.com, 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:
ms****@microsoft.com.

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

Oct 21 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.