Click here to Skip to main content
15,889,096 members
Home / Discussions / C#
   

C#

 
AnswerRe: Can i add a background image to the Windows datagrid? Pin
Jim Matthews19-Apr-05 10:39
Jim Matthews19-Apr-05 10:39 
GeneralRe: Can i add a background image to the Windows datagrid? Pin
RajeshGuptha19-Apr-05 19:14
RajeshGuptha19-Apr-05 19:14 
GeneralRe: Can i add a background image to the Windows datagrid? Pin
Jim Matthews20-Apr-05 3:14
Jim Matthews20-Apr-05 3:14 
GeneralRe: Can i add a background image to the Windows datagrid? Pin
RajeshGuptha20-Apr-05 7:20
RajeshGuptha20-Apr-05 7:20 
GeneralTray Icons Pin
CStiefeling19-Apr-05 8:09
CStiefeling19-Apr-05 8:09 
GeneralRe: Tray Icons Pin
leppie19-Apr-05 19:11
leppie19-Apr-05 19:11 
QuestionDb2 connection?? Pin
trk_wakil19-Apr-05 7:38
trk_wakil19-Apr-05 7:38 
GeneralDragOver assistance needed... Pin
new_phoenix19-Apr-05 7:27
new_phoenix19-Apr-05 7:27 
Hi, everybody!!!

I have a control that I would like to drag on a form, but I would not like to drag the control with the top left hand corner showing the dragging rectangle at System.Drawing.Location(0, 0). Instead, I would like to drag the control by dragging at the point where the mouse is clicked on the control in the control's MouseDown event.

To accomplish this, I have declared a variable dragPoint at the form level, and then in the MouseDown event, I make the assignment to the position on the control where the mouse was clicked.

In the DragOver event, the control is simply repositioned based upon either a PointToClient or a RectangleToClient location assignment and then the control.Top and control.Left assignments are made for the new control's location. I would prefer to use the RectangleToClient approach because I need to be able to click on the control at any location of the control's bounds.

When using the RectangleToClient approach, I would like to assign the control.bounds to a rectangle object, because the RectangleToClient returns a rectangle. Then, assign the width and height of the boundingRect rectangle object so that the entire control is now able to be selected. Now, I need to be able to subtract the dragPoint.X from the boundingRect.X and then assign the result to the control.Left and the control.Top OR perhaps to the e.X and e.Y.

Here is the problem: I know that there should be a place where the dragPoint is subtracted from the boundingRect rectangle and then assigned to the control.Left and the control.Top properties, but when I try to do that using just integer numbers, the result no longer shows the dragging rectangle at the top left of the control while the control is being dragged. Instead, it shows a circle with a slash through it, thereby indicating that something is not mathematically correct. How would I set the control to drag from the dragPoint set in the MouseDown event, and then reposition itself in accordance with that dragPoint when the control is moved in the DragOver event.

The code is as follows:

<br />
// create the dragPoint variable<br />
private static System.Drawing.Point dragPoint;<br />
<br />
<br />
private void MouseDown(object sender, MouseDownEventArgs e)<br />
{<br />
  // assign the dragPoint in MouseDown<br />
  dragPoint.X = e.X;<br />
  dragPoint.Y = e.Y;<br />
}<br />
<br />
// approach using PointToClient but it only drags from the top left hand corner at System.Drawing.Point(0, 0)<br />
private void DragOver(object sender, DragEventArgs e)<br />
{ <br />
   // cthis is the form that the control is dragged over and ctrl is the control dragged<br />
   System.Drawing.Point NewLocation = cthis.PointToClient(new System.Drawing.Point(e.X, e.Y));<br />
   ctrl.Left = NewLocation.X + 2;<br />
   ctrl.Top = NewLocation.Y + 2;<br />
}<br />
<br />
// approach using RectangleToClient but it only drags from the top left hand corner at System.Drawing.Point(0, 0). Would like to use the control.bounds as a rectangle to compare the dragPoint before the new control location is made.<br />
<br />
private void DragOver(object sender, DragEventArgs e)<br />
{ <br />
   // cthis is the form that the control is dragged over and ctrl is the control dragged<br />
  Rectangle boundingRect = ctrl.Bounds;<br />
  boundingRect.X = e.X;   // boundingRect.X = e.X - dragPoint.X ???<br />
  boundingRect.Y = e.Y;   // boundingRect.Y = e.Y - dragPoint.Y ???<br />
  boundingRect.Width = ctrl.Width;<br />
  boundingRect.Height = ctrl.Height;<br />
  Rectangle NewLocation = cthis.RectangleToClient(new   Rectangle(boundingRect.X, boundingRect.Y, boundingRect.Width, boundingRect.Height));<br />
  ctrl.Left = NewLocation.X + 2;<br />
  ctrl.Top = NewLocation.Y + 2;<br />
}<br />
<br />
<br />
Would really appreciate some insight as to how to drag a control in the DragOver event where the control is dragged from the dragPoint that is set in the MouseDown event and the dragPoint variable is set at the form level.

GeneralRe: DragOver assistance needed... Pin
John Fisher19-Apr-05 8:27
John Fisher19-Apr-05 8:27 
GeneralRe: DragOver assistance needed... Pin
new_phoenix19-Apr-05 8:38
new_phoenix19-Apr-05 8:38 
GeneralRegex: Very basic question Pin
matthias s.19-Apr-05 5:42
matthias s.19-Apr-05 5:42 
GeneralRe: Regex: Very basic question Pin
dratcha19-Apr-05 6:52
dratcha19-Apr-05 6:52 
GeneralRe: Regex: Very basic question Pin
matthias s.19-Apr-05 12:23
matthias s.19-Apr-05 12:23 
GeneralRe: Regex: Very basic question Pin
leppie19-Apr-05 19:18
leppie19-Apr-05 19:18 
GeneralRe: Regex: Very basic question Pin
matthias s.20-Apr-05 0:16
matthias s.20-Apr-05 0:16 
GeneralRe: Regex: Very basic question Pin
leppie20-Apr-05 3:58
leppie20-Apr-05 3:58 
GeneralRe: Regex: Very basic question Pin
Member 163675520-Apr-05 23:39
Member 163675520-Apr-05 23:39 
GeneralRe: Regex: Very basic question Pin
leppie19-Apr-05 19:21
leppie19-Apr-05 19:21 
GeneralDatagrid formatting Pin
esjq19-Apr-05 4:55
esjq19-Apr-05 4:55 
GeneralRe: Datagrid formatting Pin
John Fisher19-Apr-05 8:14
John Fisher19-Apr-05 8:14 
GeneralRe: Datagrid formatting Pin
Luis Alonso Ramos19-Apr-05 9:17
Luis Alonso Ramos19-Apr-05 9:17 
GeneralRe: Datagrid formatting Pin
esjq19-Apr-05 19:58
esjq19-Apr-05 19:58 
QuestionHow to select a check box and add to cart in C# Pin
userid_userid19-Apr-05 3:43
userid_userid19-Apr-05 3:43 
Question&quot;Secure Electronic Transaction&quot; in C#? Pin
msdawy19-Apr-05 3:30
msdawy19-Apr-05 3:30 
GeneralComboBox problem Pin
vuthaianh19-Apr-05 2:44
vuthaianh19-Apr-05 2:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.