?
Solved

Code example of capturing highlight text

Posted on 2011-09-05
5
Medium Priority
?
335 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 2000 total points
ID: 36486423
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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