How to: Move a button like a scrollbar slider?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I've made a custom scrollbar which consists of three buttons and a panel. My problem is: How do I move it like a scrollbar slider? I've tried DoDragDrop, but it does not appear to be the same, all though I can move the button. I have also used the mouseMove event, so I can move the button, but it's completely out of control. Still I feel it's the right way to go. This is what I've written:

private void btnSecond_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (MouseButtons.Left == e.Button)
{
btnSecond.Top = e.Y;
PositionToValue(btnSecond.Top);
}
}

private int PositionToValue(int pos)
{
return (_maxValue / (panelArea.Height-btnSecond.Height)) * pos;
}

Anyone who can help me out?

Thanks, gmtongar
 
gmtongar said:
Hi,
I've made a custom scrollbar which consists of three buttons and a panel. My problem is: How do I move it like a scrollbar slider? I've tried DoDragDrop, but it does not appear to be the same, all though I can move the button. I have also used the mouseMove event, so I can move the button, but it's completely out of control. Still I feel it's the right way to go. This is what I've written:

private void btnSecond_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (MouseButtons.Left == e.Button)
{
btnSecond.Top = e.Y;
PositionToValue(btnSecond.Top);
}
}

private int PositionToValue(int pos)
{
return (_maxValue / (panelArea.Height-btnSecond.Height)) * pos;
}

Anyone who can help me out?

Thanks, gmtongar

Here is what I did to "drag drop" my form that had no title bar.


//First create three global variables
int curDifX,curDifY;
bool drag = false;

//now to the event handlers
private void button_MouseDown(..)
{
//not sure about button.Left here but it should be the position of your
button
curDifX = Cursor.Position.X-button.Left;
curDifY = Cursor.Position.Y-button.Top;
drag = true;
}

private void button_MouseMove(...)
{
if (drag)
{
button.Left = Cursor.Position.X-curDifX;
button.Top = Cursor.Position.Y-curDifY;
}
}

//and finally
private void button_MouseUp(...)
{
drag = false;
}

This will make it look like you are able to click hold, move and drop a
button. In your case, since it is a slider, you could only monitor the X
value of the mouse, which would only allow you to move the control
horizontally.

Hope I understood your question correctly.

Nick Z.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top