Thursday, 28 April 2011

ColorDialog with RichtextBox in Win Forms


ColorDialog with RichtextBox in Win Forms 



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 opendialog1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdlg = new OpenFileDialog();

            ofdlg.InitialDirectory = @"C:\";
            ofdlg.RestoreDirectory = true;
            ofdlg.Title = "Open Text Files";
            ofdlg.DefaultExt = "txt";
            ofdlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            ofdlg.FilterIndex = 2;
            ofdlg.CheckFileExists = true;
            ofdlg.CheckPathExists = true;

            if (ofdlg.ShowDialog() == DialogResult.OK)
            {

                richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.LoadFile(ofdlg.FileName, RichTextBoxStreamType.PlainText);
                this.Text = ofdlg.FileName.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            richTextBox1.ResetText();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            colorDlg.AllowFullOpen = false;
            colorDlg.AnyColor = true;
            colorDlg.SolidColorOnly = false;
            colorDlg.Color = Color.Red;

            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.ForeColor = colorDlg.Color;
                richTextBox1.BackColor = colorDlg.Color;
               
            }
  
        }
        
    }
}

1 comment: