4 Replies - 18266 Views - Last Post: 11 June 2009 - 01:24 AM Rate Topic: -----

#1 facetoface101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 28-February 07

Rich Text Box, Change Multiple Selected Fonts

Posted 20 December 2008 - 04:57 AM

Hi, ive got a quick question. Im developing an app much like MS WordPad, however ive noticed that when trying to change multiple selected Fonts or font sizes there is no way to obtain what the original font or size was as the RichTextBox.SelectedFont would return null with multiple fonts. So at the moment im have to iterate through each character of the selected text and change them one by one...This is extremely slow!! even on a fast pc!

So i was wondering if there is any better way of doing it?

The code im using at the moment is below:

 private void fontCombo1_SelectedIndexChanged(object sender, EventArgs e)
		{

			Font Current = richTextBox1.SelectionFont; // Record current Font

			richTextBox1.SuspendLayout();
			Font lastFont = null;
			int selStart = richTextBox1.Selectionstart;
			int selEnd = richTextBox1.Selectionstart + richTextBox1.SelectionLength;
			int currentStart = selStart;

			for (int i = selStart; i < selEnd; i++)
			{
				richTextBox1.Select(i, 1); // Select each character of the selected string
				lastFont = richTextBox1.SelectionFont; // record the font properties of each character

				if (fontCombo1.Text == "Monotype Corsiva" || fontCombo1.Text.ToString() == "Vivaldi")
				{
					Font newFont = new Font(this.fontCombo1.Text, lastFont.Size, FontStyle.Italic);
					richTextBox1.SelectionFont = newFont;
				}
				else
				{
					Font newFont = new Font(this.fontCombo1.Text, lastFont.Size, lastFont.Style);
					richTextBox1.SelectionFont = newFont;
				}

			}

			if (richTextBox1.SelectedText == "") // Check to see if were changing selected Fonts
			{
				if (fontCombo1.Text == "Monotype Corsiva" || fontCombo1.Text.ToString() == "Vivaldi")
				{
					Font newFont = new Font(this.fontCombo1.Text, Current.Size, FontStyle.Italic);
					richTextBox1.SelectionFont = newFont;
				}
				else
					richTextBox1.SelectionFont = new Font(this.fontCombo1.Text, Current.Size, Current.Style);

			}

			richTextBox1.ResumeLayout();
			// Show changes
			richTextBox1.Invalidate();
			richTextBox1.Focus();

		}


Any Help appreciated!!

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Rich Text Box, Change Multiple Selected Fonts

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Rich Text Box, Change Multiple Selected Fonts

Posted 21 December 2008 - 03:13 AM

Have a look at Font ComboBox You may need to go to the underlying API to do what you want.
Was This Post Helpful? 0
  • +
  • -

#3 facetoface101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 28-February 07

Re: Rich Text Box, Change Multiple Selected Fonts

Posted 28 December 2008 - 03:06 AM

Sorry for the late reply, christmas 'n' all. Thanks for your reply, i had a look at it and it still doesnt completly solve my issue and also has some other issues with this approach. So i think ill stick with my solution at the moment, if i find a better solution in time ill let you know...im investigating the use of TOM at the moment...looks like it may be a step in the right direction.

If you dont mind i have another issue thats been puzzling me, although i think it may be a bug in the .net framework.

Im trying to implement a DateTimePicker control into a menu strip sublevel using the ToolStripControlHost class, this works fine and it displays however when i activate the control and try to scroll to another month etc... it vanishes! intrestingly it works perfectly on the top level of menu's or toolstrips. Any ideas why this is happening?

(Im adding this at run time)

(This code causes the problem)

private void Form1_Load(object sender, EventArgs e)

{

	DateTimePicker HostedDateTimePicker = new DateTimePicker();

	ToolStripControlHost  _ToolStripControlHost = new ToolStripControlHost(HostedDateTimePicker);
	
	HostedDateTimePicker.Width = 140;

	menuStrip1.SuspendLayout();

	dateToolStripMenuItem.DropDownItems.Insert(0, _ToolStripControlHost);

	menuStrip1.ResumeLayout();

}



Thanks
Was This Post Helpful? 1

#4 tranthiecchan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-November 08

Re: Rich Text Box, Change Multiple Selected Fonts

Posted 08 June 2009 - 02:05 AM

View Postfacetoface101, on 20 Dec, 2008 - 03:57 AM, said:

Hi, ive got a quick question. Im developing an app much like MS WordPad, however ive noticed that when trying to change multiple selected Fonts or font sizes there is no way to obtain what the original font or size was as the RichTextBox.SelectedFont would return null with multiple fonts. So at the moment im have to iterate through each character of the selected text and change them one by one...This is extremely slow!! even on a fast pc!

So i was wondering if there is any better way of doing it?

The code im using at the moment is below:

 private void fontCombo1_SelectedIndexChanged(object sender, EventArgs e)
		{

			Font Current = richTextBox1.SelectionFont; // Record current Font

			richTextBox1.SuspendLayout();
			Font lastFont = null;
			int selStart = richTextBox1.Selectionstart;
			int selEnd = richTextBox1.Selectionstart + richTextBox1.SelectionLength;
			int currentStart = selStart;

			for (int i = selStart; i < selEnd; i++)
			{
				richTextBox1.Select(i, 1); // Select each character of the selected string
				lastFont = richTextBox1.SelectionFont; // record the font properties of each character

				if (fontCombo1.Text == "Monotype Corsiva" || fontCombo1.Text.ToString() == "Vivaldi")
				{
					Font newFont = new Font(this.fontCombo1.Text, lastFont.Size, FontStyle.Italic);
					richTextBox1.SelectionFont = newFont;
				}
				else
				{
					Font newFont = new Font(this.fontCombo1.Text, lastFont.Size, lastFont.Style);
					richTextBox1.SelectionFont = newFont;
				}

			}

			if (richTextBox1.SelectedText == "") // Check to see if were changing selected Fonts
			{
				if (fontCombo1.Text == "Monotype Corsiva" || fontCombo1.Text.ToString() == "Vivaldi")
				{
					Font newFont = new Font(this.fontCombo1.Text, Current.Size, FontStyle.Italic);
					richTextBox1.SelectionFont = newFont;
				}
				else
					richTextBox1.SelectionFont = new Font(this.fontCombo1.Text, Current.Size, Current.Style);

			}

			richTextBox1.ResumeLayout();
			// Show changes
			richTextBox1.Invalidate();
			richTextBox1.Focus();

		}


Any Help appreciated!!

Thanks



This is my source code very simple wordpad.
I give everybody
Good luck
Link and source code:
http://www.mediafire...php?zdmczdwmtji
http://www.mediafire...php?jiyqnmmdjm0
Bye
Was This Post Helpful? 0
  • +
  • -

#5 tranthiecchan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-November 08

Re: Rich Text Box, Change Multiple Selected Fonts

Posted 11 June 2009 - 01:24 AM

password files rar is 111111
sorry, i am afraid of virus....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1