473,795 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Change FontD, null);
return;
}

bool wasProtected = false;

string s = richTextBox1.Se lectedText;
int selectionStart = richTextBox1.Se lectionStart;
int selectionLength = richTextBox1.Se lectionLength;

FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.Se lectionColor;
Font font = richTextBox1.Se lectionFont;
fd.Font = font;

if (fd.ShowDialog( ) == DialogResult.OK )
{
richTextBox1.Fo cus();
//richTextBox1.Se lectionStart = selectionStart;
//richTextBox1.Se lectionLength = selectionLength ;
richTextBox1.Se lect(selectionS tart, selectionLength ); //
regain selection
s = richTextBox1.Se lectedText;

if (richTextBox1.S electionProtect ed)
{
wasProtected = true;
richTextBox1.Se lectionProtecte d = false; // so the font
canchange
}
richTextBox1.Se lectionFont = fd.Font;
richTextBox1.Se lectionColor = fd.Color;
richTextBox1.Se lectionProtecte d = wasProtected;

s = richTextBox1.Se lectedText;
System.Diagnost ics.Debug.Print ("SelectedTe xt after font '" +
s + "'");
}

richTextBox1.Se lect(0,0);
m_TextToInsert = "";
}
Oct 17 '08 #1
6 1609

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
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 DelegateChangeF ont();
private DelegateChangeF ont m_ChangeFontD;

private void Form1_Load(obje ct sender, EventArgs e)
{
this.m_ChangeFo ntD = new DelegateChangeF ont(ChangeFont) ;
}

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

private void button2_Click(o bject sender, EventArgs e)
{
Thread t = new Thread(ChangeFo nt);
t.Start();
}

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

bool wasProtected = false;

string s = richTextBox1.Se lectedText;
int selectionStart = richTextBox1.Se lectionStart;
int selectionLength = richTextBox1.Se lectionLength;

FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.Se lectionColor;
Font font = richTextBox1.Se lectionFont;
fd.Font = font;

if (fd.ShowDialog( ) == DialogResult.OK )
{
richTextBox1.Fo cus();
//richTextBox1.Se lectionStart = selectionStart;
//richTextBox1.Se lectionLength = selectionLength ;
richTextBox1.Se lect(selectionS tart, selectionLength );
//regain selection
s = richTextBox1.Se lectedText;

if (richTextBox1.S electionProtect ed)
{
wasProtected = true;
richTextBox1.Se lectionProtecte d = false; // so the font can change
}
richTextBox1.Se lectionFont = fd.Font;
richTextBox1.Se lectionColor = fd.Color;
richTextBox1.Se lectionProtecte d = wasProtected;

s = richTextBox1.Se lectedText;
System.Diagnost ics.Debug.Print ("SelectedTe xt after font '" + s + "'");
}

richTextBox1.Se lect(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.mic rosoft.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****@microsof t.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
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 DelegateChangeF ont();
private DelegateChangeF ont m_ChangeFontD;

private void Form1_Load(obje ct sender, EventArgs e)
{
this.m_ChangeFo ntD = new DelegateChangeF ont(ChangeFont) ;
}

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

private void button2_Click(o bject sender, EventArgs e)
{
Thread t = new Thread(ChangeFo nt);
t.Start();
}

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

bool wasProtected = false;

string s = richTextBox1.Se lectedText;
int selectionStart = richTextBox1.Se lectionStart;
int selectionLength = richTextBox1.Se lectionLength;

FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.Se lectionColor;
Font font = richTextBox1.Se lectionFont;
fd.Font = font;

if (fd.ShowDialog( ) == DialogResult.OK )
{
richTextBox1.Fo cus();
//richTextBox1.Se lectionStart = selectionStart;
//richTextBox1.Se lectionLength = selectionLength ;
richTextBox1.Se lect(selectionS tart, selectionLength );
//regain selection
s = richTextBox1.Se lectedText;

if (richTextBox1.S electionProtect ed)
{
wasProtected = true;
richTextBox1.Se lectionProtecte d = false; // so the font can change
}
richTextBox1.Se lectionFont = fd.Font;
richTextBox1.Se lectionColor = fd.Color;
richTextBox1.Se lectionProtecte d = wasProtected;

s = richTextBox1.Se lectedText;
System.Diagnost ics.Debug.Print ("SelectedTe xt after font '" + s + "'");
}

richTextBox1.Se lect(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.mic rosoft.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****@microsof t.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
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.Rt f =
@"{\rtf1\ansi\a nsicpg1252\deff 0\deflang1033{\ fonttbl{\f0\fni l\fcharset0
Microsoft Sans Serif;}}
{\colortbl
;\red128\green0 \blue128;\red25 5\green0\blue0; \red0\green128\ blue128;\red0\g reen0\blue128;}
\viewkind4\uc1\ pard\cf1\f0\fs1 7 Test \cf0 text %%IndexField1%% set a \cf2
form \cf3\b\i\protec t\fs18 %%str_OperatorN ame%%\cf0\b0\i0 \protect0\fs17 \cf4
activated\cf0\p ar
}";

"""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 DelegateChangeF ont();
private DelegateChangeF ont m_ChangeFontD;

private void Form1_Load(obje ct sender, EventArgs e)
{
this.m_ChangeFo ntD = new DelegateChangeF ont(ChangeFont) ;
}

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

private void button2_Click(o bject sender, EventArgs e)
{
Thread t = new Thread(ChangeFo nt);
t.Start();
}

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

bool wasProtected = false;

string s = richTextBox1.Se lectedText;
int selectionStart = richTextBox1.Se lectionStart;
int selectionLength = richTextBox1.Se lectionLength;

FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.Se lectionColor;
Font font = richTextBox1.Se lectionFont;
fd.Font = font;

if (fd.ShowDialog( ) == DialogResult.OK )
{
richTextBox1.Fo cus();
//richTextBox1.Se lectionStart = selectionStart;
//richTextBox1.Se lectionLength = selectionLength ;
richTextBox1.Se lect(selectionS tart, selectionLength );
//regain selection
s = richTextBox1.Se lectedText;

if (richTextBox1.S electionProtect ed)
{
wasProtected = true;
richTextBox1.Se lectionProtecte d = false; // so the font can change
}
richTextBox1.Se lectionFont = fd.Font;
richTextBox1.Se lectionColor = fd.Color;
richTextBox1.Se lectionProtecte d = wasProtected;

s = richTextBox1.Se lectedText;
System.Diagnost ics.Debug.Print ("SelectedTe xt after font '" + s + "'");
}

richTextBox1.Se lect(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.mic rosoft.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****@microsof t.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

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
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.mic rosoft.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****@microsof t.com.

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

Oct 21 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5831
by: Jørgen Hansen | last post by:
Hi I have a problem with a Listbox in Tkinter. When I tab through several widgets with the tab-key, the listbox looses its selection, even though it has been selected with .selection_set. The example below demonstrates this. Can anyone provide me with some help on this? Regards Jorgen
2
430
by: oneworld | last post by:
I have an application that simply reads the text in a RichTextBox control, and stores the text into the database. I want to use the plain RichTextBox control and not a database rtf control. When I read the Rtf property it returns the text combined with rft codes, e.g.: "{\\rtf1 ....... \r\0" I then store this text in my Access database via an OdbcCommand. When I read the text back from the database, the returned text is @-quoted as...
3
1625
by: MarsVoyager | last post by:
Hi, I want to change the value of a submit button when the onChange event is fired by a Radio Input. Problem is, it seems the value is modified in the gui only when the Radio Input looses focus, not when its value is changed by the user. How can I make the value on the submit button change immediately upon action on the radio input and not when focus changes ? Thanks...
0
1142
by: mizd | last post by:
Hi, does anybody have a hint how to solve this. I have a datagrid with a template column (all in code behind technique). the datagrid has paging with ten items. when I select the paging arrow to switch to the next page everything works fine except that the htmlinputfile items looses their last input.
2
1699
by: Vasilis X | last post by:
Hello. I am using vb.net. On a form i have a picture box and i draw points, lines, arcs etc in it. When i minimize the form and then maximize it again or if the form looses the focus for any reason all graphics are gone and i have to redraw them. Is there a way to make my application to display the graphics all the time?
7
2554
by: SevDer | last post by:
Hi We have a static hashtable that is located in another tier in our n-tiered web application. And we are storing big but not huge objects in this hashtable and the key to the objects is through Session.SessionID. public class DataStore : System.Web.UI.Page { public static Hashtable ht = new Hashtable();
4
10271
by: dev648237923 | last post by:
Upon logon I create a ticket and put some userdata in it: FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 3, //Version tUsername.Text, //Username DateTime.Now, //Issue time DateTime.Now.AddHours(1), //Expires every hour false, //Don't persist cookie "test data" //User_SecureID ); //Hash the cookie for transport
1
1271
by: SAL | last post by:
Hello, I am sending an e-mail message from my ASP.NET 2.0 application. The body of the message is coming from a database and formatting has already been applied to the text. My problem is that if I set: IsBodyHtml = True The body looses its formatting. I'm including an hyperlink in the body, which I'd like to be clickable, but I don't care if the rest of the message is html. Is there a way to make both things happen (have the text hold...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.