?
Solved

Visual C#, Get selected text from current cell in DataGridView, bye pass default context menu

Posted on 2009-05-11
6
Medium Priority
?
2,520 Views
Last Modified: 2013-12-17
Hi Friends,

I am Developing a Windows Application in VS 2008 using C#. I am using DataGridView to allow users to add comments and instructions in multiple rows. and I want to know;

1.  Is there any way to get selected text from current cell,  e.g.  "how are you doing" is a given text in a cell and user selected only word "doing". how to retrieve it programmatically.

2. how can we get the complete word on which click is made by user, e.g. "how are you doing" is a text in cell and user click anywhere/any character on/of "doing"  and we get the word "doing" as result.

3. How we can bye-pass default context menu which appears on Right click when Cell is in edit mode, i want to replace that menu with my own options.

thanks in advance,
Arun Vashist

0
Comment
Question by:ArunVashist
[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
  • 3
6 Comments
 
LVL 6

Expert Comment

by:RPCIT
ID: 24357061
you might need to mod this for your purposes, but it should point you in the right direction.
    public partial class Form1 : Form
    {
        DataGridViewTextBoxEditingControl editingControl = null;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(editingControl.SelectedText);
        }
 
        private void grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewTextBoxEditingControl)
            {
                editingControl = (DataGridViewTextBoxEditingControl)e.Control;
            }
        }
    }

Open in new window

0
 

Author Comment

by:ArunVashist
ID: 24357615
Hi RPCIT,

Thanks for your response,
point 1-2 is already fixed, can you guide me about point 3, context menu.

thanks,
0
 
LVL 6

Expert Comment

by:RPCIT
ID: 24358423
i believe you can do this by simply... (see code)

You will obviously need to actually create the contect menu, and wire it's events..

        private void grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewTextBoxEditingControl)
            {
                editingControl = (DataGridViewTextBoxEditingControl)e.Control;
                editingControl.ContextMenu = new ContextMenu(.....);
            }
        }

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:ArunVashist
ID: 24362385
Hi RPCIT,

it work, now I am able to attach customize context menu, but again it leads to another issue. The context menu attached is working fine I can now add/remove items in it but the issue here is Right Click event is not being raised anymore.

When I am in Editmode of DataGridView Cell the below given function "DiagIns_CellMouseClick" raises on Left Click only but Right Click never raises this event,

I want to add few more items to context menu when user click right button of mouse, but right click never happen to call this function. please guide.



private void dgDiagIns_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"].IsInEditMode == true)
            {              

                if (e.Button == MouseButtons.Right)
                {
                    DataGridViewCell cell = dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"];
                    dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"].ContextMenuStrip = new ContextMenuStrip();
                    TextBox tb = (DataGridViewTextBoxEditingControl)CurEditControl;
                   
                    int ClickIndex = tb.GetCharIndexFromPosition(e.Location);
                    int LastIndex = tb.Text.LastIndexOf(" ", ClickIndex) + 1;
                    if(tb.Text != "")
                    tb.Text += " ";
                    int NextIndex = tb.Text.IndexOf(" ", ClickIndex);

                    if ((NextIndex - LastIndex) > 0)
                    {

                        string eval = tb.Text.Substring(LastIndex, NextIndex - LastIndex);
                        cmsIcdCodes.Items.Clear();
                        cmsIcdCodes.Items.Add(eval + " 1");
                        cmsIcdCodes.Items.Add(eval + " 2");
                        cmsIcdCodes.Items.Add(eval + " 3");
                        cmsIcdCodes.Items.Add(eval + " 4");
                        cmsIcdCodes.Items.Add(eval + " 5");
                        //cmsIcdCodes.Show(CurEditControl, e.Location);
                        CurEditControl.ContextMenuStrip = cmsIcdCodes;
                    }
               
                }

            }

           
        }


private void dgDiagIns_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            this.CurEditControl = e.Control;
            cmsIcdCodes.Items.Clear();
            cmsIcdCodes.Items.Add("Add new here");
            CurEditControl.ContextMenuStrip = cmsIcdCodes;          
           
        }

        private void dgDiagIns_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"].IsInEditMode == true)
            {              

                if (e.Button == MouseButtons.Left)
                {
                    DataGridViewCell cell = dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"];
                    dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"].ContextMenuStrip = new ContextMenuStrip();
                    TextBox tb = (DataGridViewTextBoxEditingControl)CurEditControl;
                   
                    int ClickIndex = tb.GetCharIndexFromPosition(e.Location);
                    int LastIndex = tb.Text.LastIndexOf(" ", ClickIndex) + 1;
                    if(tb.Text != "")
                    tb.Text += " ";
                    int NextIndex = tb.Text.IndexOf(" ", ClickIndex);

                    if ((NextIndex - LastIndex) > 0)
                    {

                        string eval = tb.Text.Substring(LastIndex, NextIndex - LastIndex);
                        cmsIcdCodes.Items.Clear();
                        cmsIcdCodes.Items.Add(eval + " 1");
                        cmsIcdCodes.Items.Add(eval + " 2");
                        cmsIcdCodes.Items.Add(eval + " 3");
                        cmsIcdCodes.Items.Add(eval + " 4");
                        cmsIcdCodes.Items.Add(eval + " 5");
                        //cmsIcdCodes.Show(CurEditControl, e.Location);
                        CurEditControl.ContextMenuStrip = cmsIcdCodes;
                    }
               
                }

            }

           
        }

        private void dgDiagIns_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            this.CurEditControl = e.Control;
            cmsIcdCodes.Items.Clear();
            cmsIcdCodes.Items.Add("Add new here");
            CurEditControl.ContextMenuStrip = cmsIcdCodes;
               
        }

0
 
LVL 6

Accepted Solution

by:
RPCIT earned 900 total points
ID: 24367188
if you are wanting to fire the context meny on rightclick.. I think you will want to use the MouseDown event.. and test for e.button = mousebuttons.right..  or something like that.

I'm doing this from memory.. I don't remember the exact syntax to check the button.
0
 

Author Closing Comment

by:ArunVashist
ID: 31580224
Thanks RPCIT
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
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