Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - RichTextControl looses formatting

Reply
 
Old 10-17-2008   #1 (permalink)
QSIDeveloper


 
 

RichTextControl looses formatting

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 = "";
}

My System SpecsSystem Spec
Old 10-20-2008   #2 (permalink)
\Ji Zhou [MSFT]\


 
 

RE: RichTextControl looses formatting

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-jzho@xxxxxx, 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:
msdnmg@xxxxxx.

==================================================
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.

My System SpecsSystem Spec
Old 10-20-2008   #3 (permalink)
QSIDeveloper


 
 

RE: RichTextControl looses formatting

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:
Quote:

> 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-jzho@xxxxxx, 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:
> msdnmg@xxxxxx.
>
> ==================================================
> 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.
>
>
My System SpecsSystem Spec
Old 10-20-2008   #4 (permalink)
QSIDeveloper


 
 

RE: RichTextControl looses formatting

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]""" wrote:
Quote:

> 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-jzho@xxxxxx, 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:
> msdnmg@xxxxxx.
>
> ==================================================
> 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.
>
>
My System SpecsSystem Spec
Old 10-20-2008   #5 (permalink)


Vista Business x64
 
 

Re: RichTextControl looses formatting

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.
My System SpecsSystem Spec
Old 10-21-2008   #6 (permalink)
\Ji Zhou [MSFT]\


 
 

RE: RichTextControl looses formatting

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-jzho@xxxxxx. So, I will try to find what
the problem is there.


Best regards,
Ji Zhou (v-jzho@xxxxxx, 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:
msdnmg@xxxxxx.

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

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
WiFi looses connection Drivers
WM looses psw and won't hold it for NNTP. Vista mail
Import from Outlook XP looses message formatting Vista mail
PC looses it's network connection Vista networking & sharing
Imap looses connection Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46