Solved

Code example of capturing highlight text

Posted on 2011-09-05
5
332 Views
Last Modified: 2012-05-12
Hello,

I am looking for a small example of highlighting text and putting it into variables or showing it as selected. for example: to have 3 boxes/variables called noun, verb, adjective. in the program, we put it text, then the user can highlight a noun and click on noun button and assign it to the noun variable. The same for verb, adjective, etc...

It would be great if it could do column/block selection also. i.e The user can select a column of text  instead of just a vertical line.
0
Comment
Question by:freshgrill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 36486423
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36486846
0
 

Author Comment

by:freshgrill
ID: 36492057
Sorry, I don't see how either of these:

1) Get the highlight text in my own program (not word under the cursor)
2) Allow me to highlight in block/column mode.

Please clarify, thanks
0
 

Author Comment

by:freshgrill
ID: 36492308
It can be in a web browser, so I have a very simple example. WHen I highlight text in the browser, it should put up a message box with the text. What is wrong in this example?
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;
using mshtml;

namespace tbrowse
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
                        //sets the default url box(textBox1.Text) to be http://www.yahoo.com/
            textBox1.Text = "http://www.yahoo.com/";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
              //Just checks to see if the url, which contains the value from the url box(textBox1.Text)
            //starts with http:// or https:// and if it doesn't, add it

            string url = textBox1.Text;

            if (!url.StartsWith("http://") &&
                !url.StartsWith("https://"))
            {
                url = "http://" + url;
            }

            webBrowser1.Navigate(new Uri(url));
        }
         //Update url Box.
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            textBox1.Text = webBrowser1.Url.ToString();
          IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
         IHTMLSelectionObject currentSelection= htmlDocument.selection;

         if (currentSelection != null)
         {
             IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;

             if (range != null)
             {
                 MessageBox.Show(range.text);
             }
         }

        }
        //End update url Box


        //Uses the hitting of the enter key as the same as clicking the Go! button
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string url2 = textBox1.Text;

                if (!url2.StartsWith("http://") &&
                 !url2.StartsWith("https://"))
                {
                    url2 = "http://" + url2;
                }

                webBrowser1.Navigate(new Uri(url2));
            }
        }

        private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

       
   


    }
}

Open in new window

0
 

Author Comment

by:freshgrill
ID: 36492339
Ok, I added another button and know it does it when I click on the bottom.

Only thing left:

How do I enable block/column selection/highlighting instead of line highlighting?
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;
using mshtml;

namespace tbrowse
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
                        //sets the default url box(textBox1.Text) to be http://www.yahoo.com/
            textBox1.Text = "http://www.yahoo.com/";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
              //Just checks to see if the url, which contains the value from the url box(textBox1.Text)
            //starts with http:// or https:// and if it doesn't, add it

            string url = textBox1.Text;

            if (!url.StartsWith("http://") &&
                !url.StartsWith("https://"))
            {
                url = "http://" + url;
            }

            webBrowser1.Navigate(new Uri(url));
        }
         //Update url Box.
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            textBox1.Text = webBrowser1.Url.ToString();
     

        }
        //End update url Box


        //Uses the hitting of the enter key as the same as clicking the Go! button
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string url2 = textBox1.Text;

                if (!url2.StartsWith("http://") &&
                 !url2.StartsWith("https://"))
                {
                    url2 = "http://" + url2;
                }

                webBrowser1.Navigate(new Uri(url2));
            }
        }

        private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
          

        }

        private void button2_Click(object sender, EventArgs e)
        {
            IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
            IHTMLSelectionObject currentSelection = htmlDocument.selection;

            if (currentSelection != null)
            {
                IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;

                if (range != null)
                {
                    MessageBox.Show(range.text);
                }
            }
        }

       
   


    }
}

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question