Solved

Code example of capturing highlight text

Posted on 2011-09-05
5
322 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now