Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGrid hitinfo text positioning
 

DataGrid hitinfo text positioning

I want the exact location within a datagrid cell based on the mouse position. For instance. the cell contains the string "The cow jumped over the moon" and the users cursor is on the o in over, I want to be able to split the string from that point, to "the cow jumped" and "over the moon".

Thanks
solardog  Thursday, September 03, 2009 8:28 AM
Could you be more specific on where do you intend to show/ place the splitted string?
And, do you intend for mouse over or mouse click?
Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
Abhijeet Lakra  Thursday, September 03, 2009 10:26 AM
Hi solardog,

Before we go any further, could you please answer the questions below:
1. Which DataGrid do you refer to, DataGrid or DataGridView?
2. What is the state of the cell when you want to get the strings, in editing or not?

As far as I know, we can achieve your goal in a DataGridView. We can get the inner TextBox in the EditingControlShowing event handler. Then we can call the GetCharIndexFromPosition method to get the index at the mouse position. This is a code snippet:
public class CustomDataGridView : DataGridView
{
    private TextBox _textBox = null;
    
    protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
    {
        base.OnEditingControlShowing(e);
        //Get the TextBox instance.
        if (e.Control is TextBox)
            _textBox = e.Control as TextBox;
    }

    //Get the split strings: string1, string2
    public void SplitString(out string string1, out string string2)
    {
        if (_textBox != null)
        {
            //Get the index of the char in the mouse position
            int index = _textBox.GetCharIndexFromPosition(_textBox.PointToClient(Control.MousePosition));
            string1 = _textBox.Text.Substring(0, index);
            string2 = _textBox.Text.Substring(index);
        }
        else
        {
            string1 = ""; string2 = "";
        }
    }
}


Let me know if this helps or not.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, September 04, 2009 12:18 PM

You can use google to search for other answers

Custom Search

More Threads

• Filtering data in a datagrid
• Strange problem with DataGridViewComboBoxCell
• Different SelectedIndex behavior of a Combobox inside a DataGridView whether bound in OnLoad or after form loaded
• BindingSource
• determine which columns have been modified
• Big data updates
• UserControl and MessageBox.Show - Does not show How do I do it ?
• New Row in the bound DataGridView
• Concurrency error with VB2005 and SQL2000
• Moving selected rows from datagridview to another datagridview