📄 richtextboxextended.cs
字号:
/// </summary>
[Description("Determines if auto word selection is enabled"),
Category("Behavior")]
public bool AutoWordSelection
{
get { return rtb1.AutoWordSelection; }
set { rtb1.AutoWordSelection = value; }
}
/// <summary>
/// Determines if this control can be edited
/// </summary>
[Description("Determines if this control can be edited"),
Category("Behavior")]
public bool ReadOnly
{
get { return rtb1.ReadOnly; }
set
{
tb1.Visible = !value;
rtb1.ReadOnly = value;
}
}
private bool _showToolBarText;
/// <summary>
/// Determines if the buttons on the toolbar will show there text or not
/// </summary>
[Description("Determines if the buttons on the toolbar will show there text or not"),
Category("Behavior")]
public bool ShowToolBarText
{
get { return _showToolBarText; }
set
{
_showToolBarText = value;
if(_showToolBarText)
{
tbbSave.Text = "Save";
tbbOpen.Text = "Open";
tbbBold.Text = "Bold";
tbbFont.Text = "Font";
tbbFontSize.Text = "Font Size";
tbbColor.Text = "Font Color";
tbbItalic.Text = "Italic";
tbbStrikeout.Text = "Strikeout";
tbbUnderline.Text = "Underline";
tbbLeft.Text = "Left";
tbbCenter.Text = "Center";
tbbRight.Text = "Right";
tbbUndo.Text = "Undo";
tbbRedo.Text = "Redo";
tbbCut.Text = "Cut";
tbbCopy.Text = "Copy";
tbbPaste.Text = "Paste";
tbbStamp.Text = "Stamp";
}
else
{
tbbSave.Text = "";
tbbOpen.Text = "";
tbbBold.Text = "";
tbbFont.Text = "";
tbbFontSize.Text = "";
tbbColor.Text = "";
tbbItalic.Text = "";
tbbStrikeout.Text = "";
tbbUnderline.Text = "";
tbbLeft.Text = "";
tbbCenter.Text = "";
tbbRight.Text = "";
tbbUndo.Text = "";
tbbRedo.Text = "";
tbbCut.Text = "";
tbbCopy.Text = "";
tbbPaste.Text = "";
tbbStamp.Text = "";
}
this.Invalidate();
this.Update();
}
}
#endregion
#region Change font
/// <summary>
/// Change the richtextbox font for the current selection
/// </summary>
public void ChangeFont(string fontFamily)
{
//This method should handle cases that occur when multiple fonts/styles are selected
// Parameters:-
// fontFamily - the font to be applied, eg "Courier New"
// Reason: The reason this method and the others exist is because
// setting these items via the selection font doen't work because
// a null selection font is returned for a selection with more
// than one font!
int rtb1start = rtb1.SelectionStart;
int len = rtb1.SelectionLength;
int rtbTempStart = 0;
// If len <= 1 and there is a selection font, amend and return
if (len <= 1 && rtb1.SelectionFont != null)
{
rtb1.SelectionFont =
new Font(fontFamily, rtb1.SelectionFont.Size, rtb1.SelectionFont.Style);
return;
}
// Step through the selected text one char at a time
rtbTemp.Rtf = rtb1.SelectedRtf;
for(int i = 0; i < len; ++i)
{
rtbTemp.Select(rtbTempStart + i, 1);
rtbTemp.SelectionFont = new Font(fontFamily, rtbTemp.SelectionFont.Size, rtbTemp.SelectionFont.Style);
}
// Replace & reselect
rtbTemp.Select(rtbTempStart,len);
rtb1.SelectedRtf = rtbTemp.SelectedRtf;
rtb1.Select(rtb1start,len);
return;
}
#endregion
#region Change font style
/// <summary>
/// Change the richtextbox style for the current selection
/// </summary>
public void ChangeFontStyle(FontStyle style, bool add)
{
//This method should handle cases that occur when multiple fonts/styles are selected
// Parameters:-
// style - eg FontStyle.Bold
// add - IF true then add else remove
// throw error if style isn't: bold, italic, strikeout or underline
if ( style != FontStyle.Bold
&& style != FontStyle.Italic
&& style != FontStyle.Strikeout
&& style != FontStyle.Underline)
throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyle");
int rtb1start = rtb1.SelectionStart;
int len = rtb1.SelectionLength;
int rtbTempStart = 0;
//if len <= 1 and there is a selection font then just handle and return
if(len <= 1 && rtb1.SelectionFont != null)
{
//add or remove style
if (add)
rtb1.SelectionFont = new Font(rtb1.SelectionFont, rtb1.SelectionFont.Style | style);
else
rtb1.SelectionFont = new Font(rtb1.SelectionFont, rtb1.SelectionFont.Style & ~style);
return;
}
// Step through the selected text one char at a time
rtbTemp.Rtf = rtb1.SelectedRtf;
for(int i = 0; i < len; ++i)
{
rtbTemp.Select(rtbTempStart + i, 1);
//add or remove style
if (add)
rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont, rtbTemp.SelectionFont.Style | style);
else
rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont, rtbTemp.SelectionFont.Style & ~style);
}
// Replace & reselect
rtbTemp.Select(rtbTempStart,len);
rtb1.SelectedRtf = rtbTemp.SelectedRtf;
rtb1.Select(rtb1start,len);
return;
}
#endregion
#region Change font size
/// <summary>
/// Change the richtextbox font size for the current selection
/// </summary>
public void ChangeFontSize(float fontSize)
{
//This method should handle cases that occur when multiple fonts/styles are selected
// Parameters:-
// fontSize - the fontsize to be applied, eg 33.5
if (fontSize <= 0.0)
throw new System.InvalidProgramException("Invalid font size parameter to ChangeFontSize");
int rtb1start = rtb1.SelectionStart;
int len = rtb1.SelectionLength;
int rtbTempStart = 0;
// If len <= 1 and there is a selection font, amend and return
if (len <= 1 && rtb1.SelectionFont != null)
{
rtb1.SelectionFont =
new Font(rtb1.SelectionFont.FontFamily, fontSize, rtb1.SelectionFont.Style);
return;
}
// Step through the selected text one char at a time
rtbTemp.Rtf = rtb1.SelectedRtf;
for(int i = 0; i < len; ++i)
{
rtbTemp.Select(rtbTempStart + i, 1);
rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont.FontFamily, fontSize, rtbTemp.SelectionFont.Style);
}
// Replace & reselect
rtbTemp.Select(rtbTempStart,len);
rtb1.SelectedRtf = rtbTemp.SelectedRtf;
rtb1.Select(rtb1start,len);
return;
}
#endregion
#region Change font color
/// <summary>
/// Change the richtextbox font color for the current selection
/// </summary>
public void ChangeFontColor(Color newColor)
{
//This method should handle cases that occur when multiple fonts/styles are selected
// Parameters:-
// newColor - eg Color.Red
int rtb1start = rtb1.SelectionStart;
int len = rtb1.SelectionLength;
int rtbTempStart = 0;
//if len <= 1 and there is a selection font then just handle and return
if(len <= 1 && rtb1.SelectionFont != null)
{
rtb1.SelectionColor = newColor;
return;
}
// Step through the selected text one char at a time
rtbTemp.Rtf = rtb1.SelectedRtf;
for(int i = 0; i < len; ++i)
{
rtbTemp.Select(rtbTempStart + i, 1);
//change color
rtbTemp.SelectionColor = newColor;
}
// Replace & reselect
rtbTemp.Select(rtbTempStart,len);
rtb1.SelectedRtf = rtbTemp.SelectedRtf;
rtb1.Select(rtb1start,len);
return;
}
#endregion
#region Get Font Details
/// <summary>
/// Returns a Font with:
/// 1) The font applying to the entire selection, if none is the default font.
/// 2) The font size applying to the entire selection, if none is the size of the default font.
/// 3) A style containing the attributes that are common to the entire selection, default regular.
/// </summary>
///
public Font GetFontDetails()
{
//This method should handle cases that occur when multiple fonts/styles are selected
int rtb1start = rtb1.SelectionStart;
int len = rtb1.SelectionLength;
int rtbTempStart = 0;
if (len <= 1)
{
// Return the selection or default font
if (rtb1.SelectionFont != null)
return rtb1.SelectionFont;
else
return rtb1.Font;
}
// Step through the selected text one char at a time
// after setting defaults from first char
rtbTemp.Rtf = rtb1.SelectedRtf;
//Turn everything on so we can turn it off one by one
FontStyle replystyle =
FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout | FontStyle.Underline;
// Set reply font, size and style to that of first char in selection.
rtbTemp.Select(rtbTempStart, 1);
string replyfont = rtbTemp.SelectionFont.Name;
float replyfontsize = rtbTemp.SelectionFont.Size;
replystyle = replystyle & rtbTemp.SelectionFont.Style;
// Search the rest of the selection
for(int i = 1; i < len; ++i)
{
rtbTemp.Select(rtbTempStart + i, 1);
// Check reply for different style
replystyle = replystyle & rtbTemp.SelectionFont.Style;
// Check font
if (replyfont != rtbTemp.SelectionFont.FontFamily.Name)
replyfont = "";
// Check font size
if (replyfontsize != rtbTemp.SelectionFont.Size)
replyfontsize = (float)0.0;
}
// Now set font and size if more than one font or font size was selected
if (replyfont == "")
replyfont = rtbTemp.Font.FontFamily.Name;
if (replyfontsize == 0.0)
replyfontsize = rtbTemp.Font.Size;
// generate reply font
Font reply
= new Font(replyfont, replyfontsize, replystyle);
return reply;
}
#endregion
#region Keyboard Handler
private void rtb1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.Modifiers == Keys.Control)
{
ToolBarButton tbb = null;
switch (e.KeyCode)
{
case Keys.B:
tbb = this.tbbBold;
break;
case Keys.I:
tbb = this.tbbItalic;
break;
case Keys.S:
tbb = this.tbbStamp;
break;
case Keys.U:
tbb = this.tbbUnderline;
break;
case Keys.OemMinus:
tbb = this.tbbStrikeout;
break;
}
if (tbb != null)
{
if(e.KeyCode != Keys.S) tbb.Pushed = !tbb.Pushed;
tb1_ButtonClick(null, new ToolBarButtonClickEventArgs(tbb));
}
}
}
private void rtb1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if((int)e.KeyChar == 9)
e.Handled = true; // Stops Ctrl+I from inserting a tab (char HT) into the richtextbox
}
#endregion
} //end class
} //end namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -