Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

C#: Winforms Selected text and Highliight in RichtextBox

How do you ensure a line of text has been selected?

In the mock up below on pressing the button a Line of text is written to a text box

However if no line has been actually selected IE user has not clicked in the RichTextBox the fist line is written

How do you ensure a user  has actually chosen a line?

Also a line of text could be longer then the RichTextBox width is it possible, on hover, to show the whole line in some kind of pop up label?  

BTW: In the Real App the lines in the RichTextBox will be file paths so using word wrap not really an option

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace TestForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
            richTextBox1.AllowDrop = true;
            richTextBox1.AutoWordSelection = false;

        }

        private void richTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Rtf))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }
        private void richTextBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
 
            var  Files = (Array)e.Data.GetData(DataFormats.FileDrop);
            foreach (string Path in Files)
            //tring Path = Files[0].ToString();
            {
                if (File.Exists(Path))
                {
                    richTextBox1.AppendText(Environment.NewLine + Path  );
                    textBox1.Text = Path;
                }
            }

        }
        private void RichTextBox1_MouseClick(object sender, MouseEventArgs e)
        {
            int firstcharindex = richTextBox1.GetFirstCharIndexOfCurrentLine();

            int currentline = richTextBox1.GetLineFromCharIndex(firstcharindex);

            string currentlinetext = richTextBox1.Lines[currentline];

            richTextBox1.Select(firstcharindex, currentlinetext.Length);

        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar== (char)Keys.Escape)
            {
                 this.Close();            
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            int firstcharindex = richTextBox1.GetFirstCharIndexOfCurrentLine();
                int currentline = richTextBox1.GetLineFromCharIndex(firstcharindex);

                string currentlinetext = richTextBox1.Lines[currentline];
            if (richTextBox1.SelectedText == null )
            {
                MessageBox.Show("You need to select a file first!");

            }
            else
            {

                textBox1.Text = currentlinetext;
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
               //  This fires After KeyPres!;
                //this.Close();
            }
        }
    }
}

Open in new window


User generated image
Avatar of Norie
Norie

Are you sure you are checking the SelectedText property correctly?

Perhaps you could use the SelectionLength property instead?
            if (richTextBox1.SelectedLength == 0l )
            {
                MessageBox.Show("You need to select a file first!");

            }
            else
            {

                textBox1.Text = currentlinetext;
            }

Open in new window

Avatar of trevor1940

ASKER

RitchtextBox doesn't have a SelectedLength property
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK thanx that worked

How about the second part of the question

a line of text could be longer then the RichTextBox width is it possible, on hover, to show the whole line in some kind of pop up label?  
When would you want the popup to appear?
When you hover over a long line with the mouse

A similar  effect can be achieved in HTML / CSS

Example hover over the notifications icon on this page
When you hover over a long line with the mouse

A similar  effect can be achieved in HTML / CSS

Example hover over the notifications icon on this page