all groups > dotnet windows forms > september 2003 >
You're in the

dotnet windows forms

group:

Drag and Drop


Re: Drag and Drop Tom
9/12/2003 1:31:14 PM
dotnet windows forms:
JB,
Regarding your last question, I guess you're asking how to determine the
position of the mouse in the Textbox (so you can determine where to insert
the text.) Here's a snippet of code for you. The
GetTextPositionFromPoint() function returns the index into the TextBox.Text
that corresponds to the point passed in. HandleTextDragOver() does
something you may not want (it moves the text cursor to the point in the
textbox where the drop will take place), but it illustrates a use of the
method.

Tom Clement
Apptero, Inc.

public struct POINTL
{
public int x;
public int y;

public POINTL(Point pt) { x = pt.X; y = pt.Y; }
public POINTL(int x0, int y0) { x = x0; y = y0; }
public override string ToString() { return String.Format("POINTL
({0},{1})", x, y); }
}


[DllImport("user32.dll", EntryPoint="SendMessage", CharSet = CharSet.Auto)]
private static extern int SendMessageInt(IntPtr hWnd, UInt32 Msg, Int32
wParam, Int32 lParam);

[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet =
CharSet.Auto)]
private static extern int SendMessageRefPoint(IntPtr hWnd, UInt32 Msg, int
wParam, ref POINTL point);



// Obtain the position in the text string from the specified
// window location. The position is in client coordinates of
// the textbox or richtextbox
public static int GetTextPositionFromPoint(TextBoxBase control, Point pt)
{
if (control is TextBox)
{
int result = SendMessageInt(control.Handle, EM_CHARFROMPOS, 0, pt.X +
(pt.Y * 0x10000));
return result & 0xFFFF;
}
else
{
POINTL pointStruct = new POINTL(pt.X, pt.Y);
return SendMessageRefPoint(control.Handle, EM_CHARFROMPOS, 0, ref
pointStruct);
}
}


public static void HandleTextDragOver(TextBoxBase txtBox, DragEventArgs e)
{
Point pt = txtBox.PointToClient(new Point(e.X, e.Y));
int iDropPos = API.GetTextPositionFromPoint(txtBox, pt);
txtBox.Focus();
txtBox.SelectionStart = iDropPos;
txtBox.SelectionLength = 0;
}



[quoted text, click to view]

Re: Drag and Drop Tom
9/12/2003 5:03:51 PM
My mistake... here's the definition.
private const int EM_CHARFROMPOS = 0xD7;
Good luck Jason

[quoted text, click to view]

Drag and Drop JB
9/12/2003 7:43:21 PM
I can't seem to get exactly what I need. I have a list box as the source,
and a text box as the target. I'm wanting to pick one listbox item, change
it's showing text slightly and alow a drop any where in the mulitlined text
box. I have the textbox set to allow drop, but all I ever get is the NO
mouse when I move over the textbox. What should my sequence of events be
here?

Jason

Re: Drag and Drop JB
9/12/2003 8:03:53 PM
Also I need this text to go in the text box where the user drops it. How do
I determine this area....

[quoted text, click to view]

Re: Drag and Drop JB
9/12/2003 9:29:37 PM
OK, I don't fully understand it but I think I do enough to impliment it,
although I'm getting and error when I compile on the EM_CHARFROMPOS message
id, what do I need to do to have this defined?

[quoted text, click to view]

AddThis Social Bookmark Button