Solved

Code example of capturing highlight text

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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