Drag and Drop

  • Thread starter Thread starter JB
  • Start date Start date
J

JB

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
 
Also I need this text to go in the text box where the user drops it. How do
I determine this area....
 
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;
}
 
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?

Tom said:
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;
}



JB said:
Also I need this text to go in the text box where the user drops it.
How
do
I determine this area....
 
My mistake... here's the definition.
private const int EM_CHARFROMPOS = 0xD7;
Good luck Jason

JB said:
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?

Tom said:
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;
}



JB said:
Also I need this text to go in the text box where the user drops it.
How
do
I determine this area....

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 events
be
 
Back
Top