richTextBox
Written by: maffelu , 2009-05-16 18:41:27
Font oldFont;
Font newFont;
void MainFormLoad(object sender, EventArgs e)
{
//Load the font-families
cmbFontFamily.Items.Add("Arial");
cmbFontFamily.Items.Add("Sans Serif");
cmbFontFamily.Items.Add("Tahoma");
cmbFontFamily.Items.Add("Times New Roman");
//Load the font-sizes
cmbFontSize.Items.Add("8");
cmbFontSize.Items.Add("10");
cmbFontSize.Items.Add("12");
cmbFontSize.Items.Add("14");
//Load the font-sizes
cmbFontColor.Items.Add(Color.Black.Name);
cmbFontColor.Items.Add(Color.Aqua.Name);
cmbFontColor.Items.Add(Color.Green.Name);
cmbFontColor.Items.Add(Color.Red.Name);
}
private void changeFont(FontStyle fs)
{
//Get the current font
oldFont = myRichTextBox.SelectionFont;
//Create a new font
//Check if the current FontStyle property equals sent in FontStyle object or not
if(oldFont.Style.Equals((object)fs))
newFont = new Font(oldFont, oldFont.Style & ~fs); //Without the current style
else
newFont = new Font(oldFont, oldFont.Style | fs); //With the current style
//Set the new font
myRichTextBox.SelectionFont = newFont;
myRichTextBox.Focus();
}
void BtnBoldClick(object sender, EventArgs e)
{
changeFont(FontStyle.Bold);
}
//Convert the text to a color object
ColorConverter conv = new ColorConverter();
Color color = (Color)conv.ConvertFromString(cmbFontColor.SelectedItem.ToString());
//Set the marked text to the correct color
myRichTextBox.SelectionColor = color;
/* * Created by SharpDevelop. * User: maffelu * Date: 2009-05-16 * Time: 16:18 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace myRichTextBox
{
public partial class MainForm : Form
{
Font oldFont;
Font newFont;
public MainForm()
{
InitializeComponent();
}
void MainFormLoad(object sender, EventArgs e)
{
//Load the font-families
cmbFontFamily.Items.Add("Arial");
cmbFontFamily.Items.Add("Sans Serif");
cmbFontFamily.Items.Add("Tahoma");
cmbFontFamily.Items.Add("Times New Roman");
//Load the font-sizes
cmbFontSize.Items.Add("8");
cmbFontSize.Items.Add("10");
cmbFontSize.Items.Add("12");
cmbFontSize.Items.Add("14");
//Load the font-sizes
cmbFontColor.Items.Add(Color.Black.Name);
cmbFontColor.Items.Add(Color.Aqua.Name);
cmbFontColor.Items.Add(Color.Green.Name);
cmbFontColor.Items.Add(Color.Red.Name);
}
void BtnBoldClick(object sender, EventArgs e)
{
changeFont(FontStyle.Bold);
}
void BtnItalicClick(object sender, EventArgs e)
{
changeFont(FontStyle.Italic);
}
void BtnUnderlinedClick(object sender, EventArgs e)
{
changeFont(FontStyle.Underline);
}
private void changeFont(FontStyle fs)
{
//Get the current font
oldFont = myRichTextBox.SelectionFont;
//Create a new font
//Check if the current FontStyle property equals sent in FontStyle object or not
if(oldFont.Style.Equals((object)fs))
newFont = new Font(oldFont, oldFont.Style & ~fs); //Without the current style
else
newFont = new Font(oldFont, oldFont.Style | fs); //With the current style
//Set the new font
myRichTextBox.SelectionFont = newFont;
myRichTextBox.Focus();
}
void CmbFontFamilySelectedIndexChanged(object sender, EventArgs e)
{
//Get the current font
oldFont = myRichTextBox.SelectionFont;
//Create a new font
newFont = new Font(cmbFontFamily.SelectedItem.ToString(), oldFont.Size, oldFont.Style);
//Set the new font
myRichTextBox.SelectionFont = newFont;
myRichTextBox.Focus();
}
void CmbFontColorSelectedIndexChanged(object sender, EventArgs e)
{
//Convert the text to a color object
ColorConverter conv = new ColorConverter();
Color color = (Color)conv.ConvertFromString(cmbFontColor.SelectedItem.ToString());
//Set the marked text to the correct color
myRichTextBox.SelectionColor = color;
}
void CmbFontSizeSelectedIndexChanged(object sender, EventArgs e)
{
oldFont = myRichTextBox.SelectionFont;
newFont = new Font(oldFont.FontFamily, float.Parse(cmbFontSize.SelectedItem.ToString()), oldFont.Style);
myRichTextBox.SelectionFont = newFont;
myRichTextBox.Focus();
}
}
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.