Tuesday, 16 July 2013

Show Richtextbox selected word in messagebox in winforms..

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

       private string RichWordOver(RichTextBox rch, int x, int y)
        {
       
            int pos = rch.GetCharIndexFromPosition(new Point(x, y));
            if (pos <= 0) return "";

       
            string txt = rch.Text;

            int start_pos;
            for (start_pos = pos; start_pos >= 0; start_pos--)
            {
             
                char ch = txt[start_pos];
                if (!char.IsLetterOrDigit(ch) && !(ch=='_')) break;
            }
            start_pos++;
            int end_pos;
            for (end_pos = pos; end_pos < txt.Length; end_pos++)
            {
                char ch = txt[end_pos];
                if (!char.IsLetterOrDigit(ch) && !(ch == '_')) break;
            }
            end_pos--;

         
            if (start_pos > end_pos) return "";
            return txt.Substring(start_pos, end_pos - start_pos + 1);
        }

     
        private void rchText_MouseMove(object sender, MouseEventArgs e)
        {
            MessageBox.Show(RichWordOver(rchText, e.X, e.Y));
        }
   
    }
}
           

No comments:

Post a Comment