RichTextBox控件應用

 

代碼如下:

  public   partial   class  RichTextBox : Form
    
{
        
public RichTextBox()
        
{
            InitializeComponent();
            txtSize.KeyPress 
+= new KeyPressEventHandler(txtSize_KeyPress);
            txtSize.Validating 
+= new CancelEventHandler(txtSize_Validating);
            rtpText.LinkClicked 
+= new LinkClickedEventHandler(rtpText_LinkClicked);
        }


        
void rtpText_LinkClicked(object sender, LinkClickedEventArgs e)
        
{
            System.Diagnostics.Process.Start(e.LinkText);
        }


        
void txtSize_Validating(object sender, CancelEventArgs e)
        
{
            TextBox txt 
= (TextBox)sender;
            ApplyTextSize(txt.Text);
            rtpText.Focus();
        }


        
void txtSize_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
if ((e.KeyChar < 48 || e.KeyChar > 57&& e.KeyChar != 8 && e.KeyChar != 13)
            
{
                e.Handled 
= true;
            }

            
else if (e.KeyChar == 13)
            
{
                TextBox txt 
= (TextBox)sender;
                
if (txt.Text.Length > 0)
                    ApplyTextSize(txt.Text);
                e.Handled 
= true;
                rtpText.Focus();
            }

        }

        
void ApplyTextSize(string txt)
        
{
            
if (txtSize.Text.Trim().Length > 0)
            
{
                
float newSize = Convert.ToSingle(txtSize.Text.Trim());
                FontFamily currentFontFamily;
                Font newFont;
                currentFontFamily 
= rtpText.SelectionFont.FontFamily;
                newFont 
= new Font(currentFontFamily, newSize);
                rtpText.SelectionFont 
= newFont;
            }

        }

        
private void btnBold_Click(object sender, EventArgs e)
        
{
            
try
            
{
                Font oldFont;
                Font newFont;
                oldFont 
= rtpText.SelectionFont;
                
if (oldFont.Bold == true)
                    newFont 
= new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
                
else newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
                rtpText.SelectionFont 
= newFont;
                
this.rtpText.Focus();
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

        }


        
private void btnItalic_Click(object sender, EventArgs e)
        
{
            Font oldFont;
            Font newFont;
            oldFont 
= rtpText.SelectionFont;
            
if (oldFont.Italic == true)
                newFont 
= new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
            
else newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
            rtpText.SelectionFont 
= newFont;
            
this.rtpText.Focus();
        }


        
private void btnUnderline_Click(object sender, EventArgs e)
        
{
            Font oldFont;
            Font newFont;
            oldFont 
= rtpText.SelectionFont;
            
if (oldFont.Underline == true)
                newFont 
= new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
            
else newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
            rtpText.SelectionFont 
= newFont;
            
this.rtpText.Focus();
        }


        
private void btnCenter_Click(object sender, EventArgs e)
        
{
            
if (this.rtpText.SelectionAlignment == HorizontalAlignment.Center)
                rtpText.SelectionAlignment 
= HorizontalAlignment.Left;
            
else rtpText.SelectionAlignment = HorizontalAlignment.Center;
            rtpText.Focus();
        }


        
private void btnSave_Click(object sender, EventArgs e)
        
{
            
//string FilePath = Application.StartupPath + @"Test.rtf";
            
//bool exist = File.Exists(FilePath);
            
//if (exist == false)
            
//{
            
//    Directory.CreateDirectory(Application.StartupPath);
            
//    FileStream fs = new FileStream(FilePath, FileMode.Create);
            
//    fs.Close();
            
//}
            try
            
{
                rtpText.SaveFile(
"../../Test.rtf");
            }

            
catch (Exception err)
            
{
                MessageBox.Show(err.Message);
            }

        }


        
private void btnLoad_Click(object sender, EventArgs e)
        
{
            
try
            
{
                rtpText.LoadFile(
"../../Test.rtf");
            }

            
catch (System.IO.FileNotFoundException)
            
{
                MessageBox.Show(
"沒找到相關文件");
            }

        }

        
private void btnStart_Click(object sender, EventArgs e)
        
{
            System.Diagnostics.Process.Start(Application.StartupPath 
+ @"垃圾清理.bat");
        }

    }

 運行效果圖:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值