C#初学者——第二个窗口小程序

来源:互联网 发布:辐射4捏脸数据导入 编辑:程序博客网 时间:2024/05/19 06:18

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Smaple_Text_Editor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


       


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


        }


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


        }


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


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


        private void textBox1_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;
                this.richTextBox1.Focus();


            }
        }


        private void ApplyTextSize(string txtSize)
        {
            float newSize = Convert.ToSingle(txtSize);
            FontFamily currentFontFamily;
            Font newFont;
            currentFontFamily = this.richTextBox1.SelectionFont.FontFamily;
            newFont = new Font(currentFontFamily,newSize);
            this.richTextBox1.SelectionFont = newFont;


            //throw new NotImplementedException();
        }


        private void textBox1_Validated(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            ApplyTextSize(txt.Text);
            this.richTextBox1.Focus();
        }


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


        private void buttonLoad_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.LoadFile("Test.rtf");
            }
            catch
            {
                MessageBox.Show("没有加载文件");
            }
        }


        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.SaveFile("Test.rtf");
            }
            catch (System.Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }


      
      
    }
}


原创粉丝点击